Created
October 15, 2012 13:11
-
-
Save romanbsd/3892387 to your computer and use it in GitHub Desktop.
Faraday gzip response middleware
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) | |
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
lostisland/faraday_middleware#84