Class: VCR::Response

Inherits:
Struct
  • Object
show all
Includes:
Normalizers::Body, Normalizers::Header
Defined in:
lib/vcr/structs.rb

Overview

The response of an HTTPInteraction.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalizers::Body

included

Constructor Details

#initialize(*args) ⇒ Response

Returns a new instance of Response.



354
355
356
357
# File 'lib/vcr/structs.rb', line 354

def initialize(*args)
  super(*args)
  self. ||= {}
end

Instance Attribute Details

#adapter_metadataHash

Additional metadata used by a specific VCR adapter.

Returns:

  • (Hash)

    the current value of adapter_metadata



350
351
352
# File 'lib/vcr/structs.rb', line 350

def 
  
end

#bodyString

the response body

Returns:

  • (String)

    the current value of body



350
351
352
# File 'lib/vcr/structs.rb', line 350

def body
  @body
end

#headersHash{String => Array<String>}

the response headers

Returns:

  • (Hash{String => Array<String>})

    the current value of headers



350
351
352
# File 'lib/vcr/structs.rb', line 350

def headers
  @headers
end

#http_versionnil, String

the HTTP version

Returns:

  • (nil, String)

    the current value of http_version



350
351
352
# File 'lib/vcr/structs.rb', line 350

def http_version
  @http_version
end

#statusResponseStatus

the status of the response

Returns:



350
351
352
# File 'lib/vcr/structs.rb', line 350

def status
  @status
end

Class Method Details

.decompress(body, type) ⇒ Object

Decode string compressed with gzip or deflate

Raises:



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/vcr/structs.rb', line 432

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) ⇒ Response

Constructs a new instance from a hash.

Parameters:

  • hash (Hash)

    the hash to use to construct the instance.

Returns:



380
381
382
383
384
385
386
# File 'lib/vcr/structs.rb', line 380

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

Instance Method Details

#compressed?Boolean

Checks if the type of encoding is one of “gzip” or “deflate”.

Returns:

  • (Boolean)


402
403
404
# File 'lib/vcr/structs.rb', line 402

def compressed?
  %w[ gzip deflate ].include? content_encoding
end

#content_encodingString

The type of encoding.

Returns:

  • (String)

    encoding type



397
398
399
# File 'lib/vcr/structs.rb', line 397

def content_encoding
  enc = get_header('Content-Encoding') and enc.first
end

#decompressObject

Decodes the compressed body and deletes evidence that it was ever compressed.

Returns:

  • self

Raises:



411
412
413
414
415
416
417
418
# File 'lib/vcr/structs.rb', line 411

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_hashHash

Builds a serializable hash from the response data.

Returns:

  • (Hash)

    hash that represents this response and can be easily serialized.

See Also:



364
365
366
367
368
369
370
371
372
373
374
# File 'lib/vcr/structs.rb', line 364

def to_hash
  {
    'status'       => status.to_hash,
    'headers'      => headers,
    'body'         => serializable_body,
    'http_version' => http_version
  }.tap do |hash|
    hash['adapter_metadata'] =  unless .empty?
    OrderedHashSerializer.apply_to(hash, members)
  end
end

#update_content_length_headerObject

Updates the Content-Length response header so that it is accurate for the response body.



390
391
392
# File 'lib/vcr/structs.rb', line 390

def update_content_length_header
  edit_header('Content-Length') { body ? body.bytesize.to_s : '0' }
end