Class: VCR::Response

Inherits:
Struct
  • Object
show all
Defined in:
lib/vcr/structs.rb

Overview

The response of an HTTPInteraction.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (String) body

the response body

Returns:

  • (String)

    the current value of body



344
345
346
# File 'lib/vcr/structs.rb', line 344

def body
  @body
end

- (Hash{String => Array<String>}) headers

the response headers

Returns:

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

    the current value of headers



344
345
346
# File 'lib/vcr/structs.rb', line 344

def headers
  @headers
end

- (nil, String) http_version

the HTTP version

Returns:

  • (nil, String)

    the current value of http_version



344
345
346
# File 'lib/vcr/structs.rb', line 344

def http_version
  @http_version
end

- (ResponseStatus) status

the status of the response

Returns:



344
345
346
# File 'lib/vcr/structs.rb', line 344

def status
  @status
end

Class Method Details

+ (Object) decompress(body, type)

Decode string compressed with gzip or deflate

Raises:



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# 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

+ (Response) from_hash(hash)

Constructs a new instance from a hash.

Parameters:

  • hash (Hash)

    the hash to use to construct the instance.

Returns:



366
367
368
369
370
371
# 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

Instance Method Details

- (Boolean) compressed?

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

Returns:

  • (Boolean)


387
388
389
# File 'lib/vcr/structs.rb', line 387

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

- (String) content_encoding

The type of encoding.

Returns:

  • (String)

    encoding type



382
383
384
# File 'lib/vcr/structs.rb', line 382

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

- (Object) decompress

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

Returns:

  • self

Raises:



396
397
398
399
400
401
402
403
# 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

- (Hash) to_hash

Builds a serializable hash from the response data.

Returns:

  • (Hash)

    hash that represents this response and can be easily serialized.

See Also:



353
354
355
356
357
358
359
360
# 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

- (Object) update_content_length_header

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



375
376
377
# File 'lib/vcr/structs.rb', line 375

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