class VCR::Response
The response of an {HTTPInteraction}.
@attr [ResponseStatus] status the status of the response @attr [Hash{String => Array<String>}] headers the response headers @attr [String] body the response body @attr [nil, String] http_version the HTTP version
Constants
- HAVE_ZLIB
Public Class Methods
Decode string compressed with gzip or deflate
@raise [VCR::Errors::UnknownContentEncodingError] if the content encoding
is not a known encoding.
# File lib/vcr/structs.rb, line 417 def self.decompress(body, type) unless HAVE_ZLIB warn "VCR: cannot decompress response; Zlib not available" return end case type when 'gzip' args = [StringIO.new(body)] args << { :encoding => 'ASCII-8BIT' } if ''.respond_to?(:encoding) yield Zlib::GzipReader.new(*args).read when 'deflate' yield Zlib::Inflate.inflate(body) when 'identity', NilClass return else raise Errors::UnknownContentEncodingError, "unknown content encoding: #{type}" end end
Constructs a new instance from a hash.
@param [Hash] hash the hash to use to construct the instance. @return [Response] the response
# File lib/vcr/structs.rb, line 366 def self.from_hash(hash) new ResponseStatus.from_hash(hash.fetch('status', {})), hash['headers'], body_from(hash['body']), hash['http_version'] end
Public Instance Methods
Checks if the type of encoding is one of “gzip” or “deflate”.
# File lib/vcr/structs.rb, line 387 def compressed? %w[ gzip deflate ].include? content_encoding end
The type of encoding.
@return [String] encoding type
# File lib/vcr/structs.rb, line 382 def content_encoding enc = get_header('Content-Encoding') and enc.first end
Decodes the compressed body and deletes evidence that it was ever compressed.
@return self @raise [VCR::Errors::UnknownContentEncodingError] if the content encoding
is not a known encoding.
# File lib/vcr/structs.rb, line 396 def decompress self.class.decompress(body, content_encoding) { |new_body| self.body = new_body update_content_length_header delete_header('Content-Encoding') } return self end
Builds a serializable hash from the response data.
@return [Hash] hash that represents this response
and can be easily serialized.
@see ::from_hash
# File lib/vcr/structs.rb, line 353 def to_hash { 'status' => status.to_hash, 'headers' => headers, 'body' => serializable_body, 'http_version' => http_version }.tap { |h| OrderedHashSerializer.apply_to(h, members) } end
Updates the Content-Length response header so that it is accurate for the response body.
# File lib/vcr/structs.rb, line 375 def update_content_length_header edit_header('Content-Length') { body ? body.bytesize.to_s : '0' } end