class HTTParty::Decompressor

@abstract Read the HTTP Compression section for more information.
decompressed.
gems are installed. Otherwise, it returns nil if the body data cannot be
This class will handle “br” (Brotli) and “compress” (LZW) if the requisite
Net::HTTP automatically decompresses Content-Encoding values “gzip” and “deflate”.
Decompresses the response body based on the Content-Encoding header.

def brotli

def brotli
  return nil unless defined?(::Brotli)
  begin
    ::Brotli.inflate(body)
  rescue StandardError
    nil
  end
end

def decompress

Returns:
  • (nil) - when the response body is nil or cannot decompressed
  • (String) - the decompressed body
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

def decompress_supported_encoding

def decompress_supported_encoding
  method = SupportedEncodings[encoding]
  if respond_to?(method, true)
    send(method)
  else
    raise NotImplementedError, "#{self.class.name} has not implemented a decompression method for #{encoding.inspect} encoding."
  end
end

def initialize(body, encoding)

Parameters:
  • encoding (Symbol) -- - the Content-Encoding algorithm used to encode the body
  • body (String) -- - the response body of the request
def initialize(body, encoding)
  @body = body
  @encoding = encoding
end

def lzw

def lzw
  begin
    if defined?(::LZWS::String)
      ::LZWS::String.decompress(body)
    elsif defined?(::LZW::Simple)
      ::LZW::Simple.new.decompress(body)
    end
  rescue StandardError
    nil
  end
end

def none

def none
  body
end

def supports_encoding?

def supports_encoding?
  SupportedEncodings.keys.include?(encoding)
end

def zstd

def zstd
  return nil unless defined?(::Zstd)
  begin
    ::Zstd.decompress(body)
  rescue StandardError
    nil
  end
end