Class: HTTParty::Decompressor Abstract
- Inherits:
-
Object
- Object
- HTTParty::Decompressor
- Defined in:
- lib/httparty/decompressor.rb
Overview
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
-
#body ⇒ String
readonly
The response body of the request.
-
#encoding ⇒ Symbol
readonly
The Content-Encoding algorithm used to encode the body.
Instance Method Summary collapse
-
#decompress ⇒ String?
Perform decompression on the response body.
-
#initialize(body, encoding) ⇒ Decompressor
constructor
A new instance of Decompressor.
Constructor Details
#initialize(body, encoding) ⇒ Decompressor
Returns a new instance of Decompressor.
34 35 36 37 |
# File 'lib/httparty/decompressor.rb', line 34 def initialize(body, encoding) @body = body @encoding = encoding end |
Instance Attribute Details
#body ⇒ String (readonly)
The response body of the request
26 27 28 |
# File 'lib/httparty/decompressor.rb', line 26 def body @body end |
#encoding ⇒ Symbol (readonly)
The Content-Encoding algorithm used to encode the body
30 31 32 |
# File 'lib/httparty/decompressor.rb', line 30 def encoding @encoding end |
Instance Method Details
#decompress ⇒ String?
Perform decompression on the response body
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 |