Class: HTTParty::Decompressor Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/httparty/decompressor.rb

Overview

This class is abstract.

Read the HTTP Compression section for more information.

Decompresses the response body based on the Content-Encoding header.

Net::HTTP automatically decompresses Content-Encoding values “gzip” and “deflate”. This class will handle “br” (Brotli) and “compress” (LZW) if the requisite gems are installed. Otherwise, it returns nil if the body data cannot be decompressed.

Constant Summary collapse

SupportedEncodings =

“gzip” and “deflate” are handled by Net::HTTP hence they do not need to be handled by HTTParty

{
  'none'     => :none,
  'identity' => :none,
  'br'       => :brotli,
  'compress' => :lzw,
  'zstd'     => :zstd
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, encoding) ⇒ Decompressor

Returns a new instance of Decompressor.

Parameters:

  • body (String)
    • the response body of the request

  • encoding (Symbol)
    • the Content-Encoding algorithm used to encode the body



34
35
36
37
# File 'lib/httparty/decompressor.rb', line 34

def initialize(body, encoding)
  @body = body
  @encoding = encoding
end

Instance Attribute Details

#bodyString (readonly)

The response body of the request

Returns:

  • (String)


26
27
28
# File 'lib/httparty/decompressor.rb', line 26

def body
  @body
end

#encodingSymbol (readonly)

The Content-Encoding algorithm used to encode the body

Returns:

  • (Symbol)

    e.g. :gzip



30
31
32
# File 'lib/httparty/decompressor.rb', line 30

def encoding
  @encoding
end

Instance Method Details

#decompressString?

Perform decompression on the response body

Returns:

  • (String)

    the decompressed body

  • (nil)

    when the response body is nil or cannot decompressed



42
43
44
45
46
47
48
49
50
51
# File 'lib/httparty/decompressor.rb', line 42

def decompress
  return nil if body.nil?
  return body if encoding.nil? || encoding.strip.empty?

  if supports_encoding?
    decompress_supported_encoding
  else
    nil
  end
end