Class: HTTP::Features::AutoInflate

Inherits:
HTTP::Feature show all
Defined in:
lib/http/features/auto_inflate.rb,
sig/http.rbs

Overview

Automatically decompresses response bodies

Constant Summary collapse

SUPPORTED_ENCODING =

Returns:

  • (Set[String])
Set.new(%w[deflate gzip x-gzip]).freeze

Instance Method Summary collapse

Methods inherited from HTTP::Feature

#around_request, #on_error, #on_request, #wrap_request

Instance Method Details

#inflated_response_options(response) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build options hash for an inflated response

Parameters:

Returns:

  • (Hash)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/http/features/auto_inflate.rb', line 42

def inflated_response_options(response)
  {
    status:        response.status,
    version:       response.version,
    headers:       response.headers,
    proxy_headers: response.proxy_headers,
    connection:    response.connection,
    body:          stream_for(response.connection, encoding: response.body.encoding),
    request:       response.request
  }
end

#stream_for(connection, encoding: Encoding::BINARY) ⇒ HTTP::Response::Body

Returns an inflating body stream for a connection

Examples:

feature.stream_for(connection)

Parameters:

  • connection (HTTP::Connection)
  • encoding (Encoding) (defaults to: Encoding::BINARY)

    encoding to use for the inflated body

  • encoding: (Encoding) (defaults to: Encoding::BINARY)

Returns:



33
34
35
# File 'lib/http/features/auto_inflate.rb', line 33

def stream_for(connection, encoding: Encoding::BINARY)
  Response::Body.new(Response::Inflater.new(connection), encoding: encoding)
end

#supported_encoding?(response) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the response encoding is supported

Parameters:

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/http/features/auto_inflate.rb', line 56

def supported_encoding?(response)
  content_encoding = response.headers.get(Headers::CONTENT_ENCODING).first
  content_encoding && SUPPORTED_ENCODING.include?(content_encoding)
end

#wrap_response(response) ⇒ HTTP::Response

Wraps a response with an auto-inflating body

Examples:

feature.wrap_response(response)

Parameters:

Returns:



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

def wrap_response(response)
  return response unless supported_encoding?(response)

  Response.new(**inflated_response_options(response)) # steep:ignore
end