-
-
Save spiegela/5114126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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