Class: FaradayMiddleware::Adknowledge

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/faraday_middleware/adknowledge.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/faraday_middleware/adknowledge.rb', line 9

def on_complete env
  encoding = env[:response_headers]['content-encoding'].to_s.downcase

  return unless env[:body].is_a? String

  case encoding
  when 'gzip'
    env[:body] = Zlib::GzipReader.new(StringIO.new(env[:body]), encoding: 'ASCII-8BIT').read
    env[:response_headers].delete 'content-encoding'
  when 'deflate'
    env[:body] = Zlib::Inflate.inflate env[:body]
    env[:response_headers].delete 'content-encoding'
  end

  begin
    env[:body] = ::MultiXml.parse(env[:body])['result']
  rescue Faraday::Error::ParsingError
    env[:body]
  end
end