Class: Frodo::Middleware::Gzip
- Inherits:
-
Frodo::Middleware
- Object
- Faraday::Middleware
- Frodo::Middleware
- Frodo::Middleware::Gzip
- Defined in:
- lib/frodo/middleware/gzip.rb
Overview
Middleware to uncompress GZIP compressed responses from Salesforce.
Constant Summary collapse
- ACCEPT_ENCODING_HEADER =
'Accept-Encoding'- CONTENT_ENCODING_HEADER =
'Content-Encoding'- ENCODING =
'gzip'
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#decompress(body) ⇒ Object
Internal: Decompresses a gzipped string.
-
#gzipped?(env) ⇒ Boolean
Internal: Returns true if the response is gzipped.
- #on_complete(env) ⇒ Object
Methods inherited from Frodo::Middleware
#client, #connection, #initialize
Constructor Details
This class inherits a constructor from Frodo::Middleware
Instance Method Details
#call(env) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/frodo/middleware/gzip.rb', line 12 def call(env) env[:request_headers][ACCEPT_ENCODING_HEADER] = ENCODING if @options[:compress] @app.call(env).on_complete do |environment| on_complete(environment) end end |
#decompress(body) ⇒ Object
Internal: Decompresses a gzipped string.
29 30 31 |
# File 'lib/frodo/middleware/gzip.rb', line 29 def decompress(body) Zlib::GzipReader.new(StringIO.new(body)).read end |
#gzipped?(env) ⇒ Boolean
Internal: Returns true if the response is gzipped.
24 25 26 |
# File 'lib/frodo/middleware/gzip.rb', line 24 def gzipped?(env) env[:response_headers][CONTENT_ENCODING_HEADER] == ENCODING end |
#on_complete(env) ⇒ Object
19 20 21 |
# File 'lib/frodo/middleware/gzip.rb', line 19 def on_complete(env) env[:body] = decompress(env[:body]) if gzipped?(env) end |