Class: FaradayMiddleware::Gzip
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- FaradayMiddleware::Gzip
- Defined in:
- lib/dacpclient/faraday/gzip.rb
Overview
A middleware that ensures that the client requests are sent with the headers that encourage servers to send compressed data, and then uncompresses it. The Content-Length will reflect the actual body length.
Constant Summary collapse
- ACCEPT_ENCODING =
'Accept-Encoding'.freeze
- ENCODINGS =
'gzip,deflate'.freeze
Instance Method Summary collapse
- #call(curenv) ⇒ Object
-
#initialize(app) ⇒ Gzip
constructor
A new instance of Gzip.
Constructor Details
#initialize(app) ⇒ Gzip
Returns a new instance of Gzip.
12 13 14 |
# File 'lib/dacpclient/faraday/gzip.rb', line 12 def initialize(app) @app = app end |
Instance Method Details
#call(curenv) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dacpclient/faraday/gzip.rb', line 16 def call(curenv) (curenv[:request_headers] ||= {})[ACCEPT_ENCODING] = ENCODINGS @app.call(curenv).on_complete do |env| encoding = env[:response_headers]['content-encoding'].to_s.downcase if %w(gzip deflate).include?(encoding) case encoding when 'gzip' env[:body] = uncompress_gzip(env[:body]) when 'deflate' env[:body] = Zlib::Inflate.inflate(env[:body]) end env[:response_headers].delete('content-encoding') env[:response_headers]['content-length'] = env[:body].length end end end |