class HTTP::Features::AutoDeflate

Attributes

method[R]

Public Class Methods

new(**) click to toggle source
Calls superclass method HTTP::Feature::new
# 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

Public Instance Methods

deflated_body(body) click to toggle source
# 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) click to toggle source
# 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