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
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 452 def self.decompress(body, type) unless HAVE_ZLIB warn "VCR: cannot decompress response; Zlib not available" return end case type when 'gzip' gzip_reader_options = {} gzip_reader_options[:encoding] = 'ASCII-8BIT' if ''.respond_to?(:encoding) yield Zlib::GzipReader.new(StringIO.new(body), **gzip_reader_options).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 363 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
VCR::Normalizers::Body::new
# File lib/vcr/structs.rb, line 338 def initialize(*args) super(*args) self.adapter_metadata ||= {} end
Public Instance Methods
Checks if the type of encoding is one of “gzip” or “deflate”.
# File lib/vcr/structs.rb, line 385 def compressed? %w[ gzip deflate ].include? content_encoding end
The type of encoding.
@return [String] encoding type
# File lib/vcr/structs.rb, line 380 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 399 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
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 413 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
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 348 def to_hash { 'status' => status.to_hash, 'headers' => headers, 'body' => serializable_body }.tap do |hash| hash['http_version'] = http_version if http_version hash['adapter_metadata'] = adapter_metadata unless adapter_metadata.empty? end end
Updates the Content-Length response header so that it is accurate for the response body.
# File lib/vcr/structs.rb, line 373 def update_content_length_header edit_header('Content-Length') { body ? body.bytesize.to_s : '0' } end
Checks if VCR
decompressed the response body
# File lib/vcr/structs.rb, line 390 def vcr_decompressed? adapter_metadata['vcr_decompressed'] end