Skip to content

Instantly share code, notes, and snippets.

@kapkaev
Last active September 13, 2018 17:10
Show Gist options
  • Save kapkaev/5081027 to your computer and use it in GitHub Desktop.
Save kapkaev/5081027 to your computer and use it in GitHub Desktop.
Faraday::Response::Gzip middleware
require 'faraday'
require 'zlib'
module Faraday
class Response::Gzip < Response::Middleware
def on_complete(env)
encoding = env[:response_headers]['content-encoding'].to_s.downcase
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
end
end
end
Faraday::Response.register_middleware :gzip => Faraday::Response::Gzip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment