Class: Force::Middleware::Gzip

Inherits:
Force::Middleware show all
Defined in:
lib/force/middleware/gzip.rb

Overview

Middleware to uncompress GZIP compressed responses from Salesforce.

Constant Summary collapse

ACCEPT_ENCODING_HEADER =
'Accept-Encoding'.freeze
CONTENT_ENCODING_HEADER =
'Content-Encoding'.freeze
ENCODING =
'gzip'.freeze

Instance Method Summary collapse

Methods inherited from Force::Middleware

#client, #connection, #initialize

Constructor Details

This class inherits a constructor from Force::Middleware

Instance Method Details

#call(env) ⇒ Object


10
11
12
13
14
15
# File 'lib/force/middleware/gzip.rb', line 10

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.


27
28
29
# File 'lib/force/middleware/gzip.rb', line 27

def decompress(body)
  Zlib::GzipReader.new(StringIO.new(body)).read
end

#gzipped?(env) ⇒ Boolean

Internal: Returns true if the response is gzipped.

Returns:

  • (Boolean)

22
23
24
# File 'lib/force/middleware/gzip.rb', line 22

def gzipped?(env)
  env[:response_headers][CONTENT_ENCODING_HEADER] == ENCODING
end

#on_complete(env) ⇒ Object


17
18
19
# File 'lib/force/middleware/gzip.rb', line 17

def on_complete(env)
  env[:body] = decompress(env[:body]) if gzipped?(env)
end