Class: HTTP::Features::AutoDeflate
- Inherits:
-
HTTP::Feature
- Object
- HTTP::Feature
- HTTP::Features::AutoDeflate
- Defined in:
- lib/http/features/auto_deflate.rb
Defined Under Namespace
Classes: CompressedBody, DeflatedBody, GzippedBody
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Instance Method Summary collapse
- #deflated_body(body) ⇒ Object
-
#initialize ⇒ AutoDeflate
constructor
A new instance of AutoDeflate.
- #wrap_request(request) ⇒ Object
Methods inherited from HTTP::Feature
Constructor Details
#initialize ⇒ AutoDeflate
Returns a new instance of AutoDeflate.
13 14 15 16 17 18 19 |
# File 'lib/http/features/auto_deflate.rb', line 13 def initialize(**) super @method = @opts.key?(:method) ? @opts[:method].to_s : "gzip" raise Error, "Only gzip and deflate methods are supported" unless %w[gzip deflate].include?(@method) end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
11 12 13 |
# File 'lib/http/features/auto_deflate.rb', line 11 def method @method end |
Instance Method Details
#deflated_body(body) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/http/features/auto_deflate.rb', line 40 def deflated_body(body) case method when "gzip" GzippedBody.new(body) when "deflate" DeflatedBody.new(body) end end |
#wrap_request(request) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/http/features/auto_deflate.rb', line 21 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 |