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
}.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


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

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

Instance Attribute Details

#bodyString (readonly)

The response body of the request

Returns:

  • (String)

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

def body
  @body
end

#encodingSymbol (readonly)

The Content-Encoding algorithm used to encode the body

Returns:

  • (Symbol)

    e.g. :gzip


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

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


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

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