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 hidden or 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 |
👍 especially since it's on the roadmap wiki page!
Should this not send an Accept-Encoding request header?
Yeah, good point. I'm setting the request headers myself. But for this to be feature complete, I need to add it to middleware. And then it should be added with :use rather then :response
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks good. Are you going to submit a pull-request to faraday_middleware?