Class: VCR::Response
- Inherits:
-
Struct
- Object
- Struct
- VCR::Response
- Defined in:
- lib/vcr/structs.rb
Overview
The response of an HTTPInteraction.
Instance Attribute Summary (collapse)
-
- (String) body
the response body.
-
- (Hash{String => Array<String>}) headers
the response headers.
-
- (nil, String) http_version
the HTTP version.
-
- (ResponseStatus) status
the status of the response.
Class Method Summary (collapse)
-
+ (Object) decompress(body, type)
Decode string compressed with gzip or deflate.
-
+ (Response) from_hash(hash)
Constructs a new instance from a hash.
Instance Method Summary (collapse)
-
- (Boolean) compressed?
Checks if the type of encoding is one of "gzip" or "deflate".
-
- (String) content_encoding
The type of encoding.
-
- (Object) decompress
Decodes the compressed body and deletes evidence that it was ever compressed.
-
- (Hash) to_hash
Builds a serializable hash from the response data.
-
- (Object) update_content_length_header
Updates the Content-Length response header so that it is accurate for the response body.
Instance Attribute Details
- (String) body
the response body
344 345 346 |
# File 'lib/vcr/structs.rb', line 344 def body @body end |
- (Hash{String => Array<String>}) headers
the response headers
344 345 346 |
# File 'lib/vcr/structs.rb', line 344 def headers @headers end |
- (nil, String) http_version
the 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
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
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.
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".
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.
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.
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.
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 |