Skip to content

Instantly share code, notes, and snippets.

@spiegela
Forked from romanbsd/gzip.rb
Created March 8, 2013 04:00
Show Gist options
  • Save spiegela/5114126 to your computer and use it in GitHub Desktop.
Save spiegela/5114126 to your computer and use it in GitHub Desktop.
require 'faraday'
require 'zlib'
module FaradayMiddleware
class Gzip < Faraday::Response::Middleware
def on_complete(env)
return unless env[:body].is_a? String
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 => FaradayMiddleware::Gzip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment