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 @attr [Hash] adapter_metadata Additional metadata used by a specific VCR adapter.

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 449
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 360
def self.from_hash(hash)
  new ResponseStatus.from_hash(hash.fetch('status', {})),
      hash['headers'],
      body_from(hash['body']),
      hash['http_version'],
      hash['adapter_metadata']
end
new(*args) click to toggle source
Calls superclass method VCR::Normalizers::Body::new
# File lib/vcr/structs.rb, line 335
def initialize(*args)
  super(*args)
  self.adapter_metadata ||= {}
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 382
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 377
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
    adapter_metadata['vcr_decompressed'] = content_encoding
    delete_header('Content-Encoding')
  }
  return self
end
recompress() click to toggle source

Recompresses the decompressed body according to adapter metadata.

@raise [VCR::Errors::UnknownContentEncodingError] if the content encoding

stored in the adapter metadata is unknown
# File lib/vcr/structs.rb, line 410
def recompress
  type = adapter_metadata['vcr_decompressed']
  new_body = begin
    case type
    when 'gzip'
      body_str = ''
      args = [StringIO.new(body_str)]
      args << { :encoding => 'ASCII-8BIT' } if ''.respond_to?(:encoding)
      writer = Zlib::GzipWriter.new(*args)
      writer.write(body)
      writer.close
      body_str
    when 'deflate'
      Zlib::Deflate.inflate(body)
    when 'identity', NilClass
      nil
    else
      raise Errors::UnknownContentEncodingError, "unknown content encoding: #{type}"
    end
  end
  if new_body
    self.body = new_body
    update_content_length_header
    headers['Content-Encoding'] = type
  end
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 Response.from_hash

# File lib/vcr/structs.rb, line 345
def to_hash
  {
    'status'       => status.to_hash,
    'headers'      => headers,
    'body'         => serializable_body,
    'http_version' => http_version
  }.tap do |hash|
    hash['adapter_metadata'] = adapter_metadata unless adapter_metadata.empty?
  end
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 370
def update_content_length_header
  edit_header('Content-Length') { body ? body.bytesize.to_s : '0' }
end
vcr_decompressed?() click to toggle source

Checks if VCR decompressed the response body

# File lib/vcr/structs.rb, line 387
def vcr_decompressed?
  adapter_metadata['vcr_decompressed']
end