Class: HTTP::Features::AutoDeflate

Inherits:
HTTP::Feature show all
Defined in:
lib/http/features/auto_deflate.rb

Defined Under Namespace

Classes: CompressedBody, DeflatedBody, GzippedBody

Constant Summary collapse

VALID_METHODS =
Set.new(%w[gzip deflate]).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from HTTP::Feature

#on_error, #wrap_response

Constructor Details

#initialize(method: "gzip") ⇒ AutoDeflate

Returns a new instance of AutoDeflate.

Raises:



16
17
18
19
20
21
22
# File 'lib/http/features/auto_deflate.rb', line 16

def initialize(method: "gzip")
  super()

  @method = method.to_s

  raise Error, "Only gzip and deflate methods are supported" unless VALID_METHODS.include?(@method)
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



14
15
16
# File 'lib/http/features/auto_deflate.rb', line 14

def method
  @method
end

Instance Method Details

#deflated_body(body) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/http/features/auto_deflate.rb', line 43

def deflated_body(body)
  case method
  when "gzip"
    GzippedBody.new(body)
  when "deflate"
    DeflatedBody.new(body)
  end
end

#wrap_request(request) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/http/features/auto_deflate.rb', line 24

def wrap_request(request)
  return request unless method
  return request if request.body.size.zero?

  # We need to delete Content-Length header. It will be set automatically by HTTP::Request::Writer
  request.headers.delete(Headers::CONTENT_LENGTH)
  request.headers[Headers::CONTENT_ENCODING] = method

  Request.new(
    version:        request.version,
    verb:           request.verb,
    uri:            request.uri,
    headers:        request.headers,
    proxy:          request.proxy,
    body:           deflated_body(request.body),
    uri_normalizer: request.uri_normalizer
  )
end