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
-
#adapter_metadata ⇒ Hash
Additional metadata used by a specific VCR adapter.
-
#body ⇒ String
the response body.
-
#headers ⇒ Hash{String => Array<String>}
the response headers.
-
#http_version ⇒ nil, String
the HTTP version.
-
#status ⇒ ResponseStatus
the status of the response.
Class Method Summary collapse
-
.decompress(body, type) ⇒ Object
Decode string compressed with gzip or deflate.
-
.from_hash(hash) ⇒ Response
Constructs a new instance from a hash.
Instance Method Summary collapse
-
#compressed? ⇒ Boolean
Checks if the type of encoding is one of "gzip" or "deflate".
-
#content_encoding ⇒ String
The type of encoding.
-
#decompress ⇒ Object
Decodes the compressed body and deletes evidence that it was ever compressed.
-
#initialize(*args) ⇒ Response
constructor
A new instance of Response.
-
#recompress ⇒ Object
Recompresses the decompressed body according to adapter metadata.
-
#to_hash ⇒ Hash
Builds a serializable hash from the response data.
-
#update_content_length_header ⇒ Object
Updates the Content-Length response header so that it is accurate for the response body.
-
#vcr_decompressed? ⇒ Boolean
Checks if VCR decompressed the response body.
Constructor Details
#initialize(*args) ⇒ Response
Returns a new instance of Response.
335 336 337 338 |
# File 'lib/vcr/structs.rb', line 335 def initialize(*args) super(*args) self. ||= {} end |
Instance Attribute Details
#adapter_metadata ⇒ Hash
Additional metadata used by a specific VCR adapter.
331 332 333 |
# File 'lib/vcr/structs.rb', line 331 def @adapter_metadata end |
#body ⇒ String
the response body
331 332 333 |
# File 'lib/vcr/structs.rb', line 331 def body @body end |
#headers ⇒ Hash{String => Array<String>}
the response headers
331 332 333 |
# File 'lib/vcr/structs.rb', line 331 def headers @headers end |
#http_version ⇒ nil, String
the HTTP version
331 332 333 |
# File 'lib/vcr/structs.rb', line 331 def http_version @http_version end |
#status ⇒ ResponseStatus
the status of the response
331 332 333 |
# File 'lib/vcr/structs.rb', line 331 def status @status end |
Class Method Details
.decompress(body, type) ⇒ Object
Decode string compressed with gzip or deflate
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# 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' = {} [:encoding] = 'ASCII-8BIT' if ''.respond_to?(:encoding) yield Zlib::GzipReader.new(StringIO.new(body), **).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.
360 361 362 363 364 365 366 |
# 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 |
Instance Method Details
#compressed? ⇒ Boolean
Checks if the type of encoding is one of "gzip" or "deflate".
382 383 384 |
# File 'lib/vcr/structs.rb', line 382 def compressed? %w[ gzip deflate ].include? content_encoding end |
#content_encoding ⇒ String
The type of encoding.
377 378 379 |
# File 'lib/vcr/structs.rb', line 377 def content_encoding enc = get_header('Content-Encoding') and enc.first end |
#decompress ⇒ Object
Decodes the compressed body and deletes evidence that it was ever compressed.
396 397 398 399 400 401 402 403 404 |
# 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 ['vcr_decompressed'] = content_encoding delete_header('Content-Encoding') } return self end |
#recompress ⇒ Object
Recompresses the decompressed body according to adapter metadata.
410 411 412 413 414 415 416 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 410 def recompress type = ['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 ⇒ Hash
Builds a serializable hash from the response data.
345 346 347 348 349 350 351 352 353 354 |
# File 'lib/vcr/structs.rb', line 345 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'] = unless .empty? end end |
#update_content_length_header ⇒ Object
Updates the Content-Length response header so that it is accurate for the response body.
370 371 372 |
# 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? ⇒ Boolean
Checks if VCR decompressed the response body
387 388 389 |
# File 'lib/vcr/structs.rb', line 387 def vcr_decompressed? ['vcr_decompressed'] end |