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

decompress(body, type) { |gzip_reader(*args).read| ... } click to toggle source

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
from_hash(hash) click to toggle source

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

compressed?() click to toggle source

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
content_encoding() click to toggle source

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
decompress() click to toggle source

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
to_hash() click to toggle source

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
update_content_length_header() click to toggle source

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