lib/vcr/structs.rb in vcr-4.0.0 vs lib/vcr/structs.rb in vcr-5.0.0
- old
+ new
@@ -165,29 +165,10 @@
end
end
end
end
- # @private
- module OrderedHashSerializer
- def each
- @ordered_keys.each do |key|
- yield key, self[key] if has_key?(key)
- end
- end
-
- if RUBY_VERSION.to_f > 1.8
- # 1.9+ hashes are already ordered.
- def self.apply_to(*args); end
- else
- def self.apply_to(hash, keys)
- hash.instance_variable_set(:@ordered_keys, keys)
- hash.extend self
- end
- end
- end
-
# The request of an {HTTPInteraction}.
#
# @attr [Symbol] method the HTTP method (i.e. :head, :options, :get, :post, :put, :patch or :delete)
# @attr [String] uri the request URI
# @attr [String, nil] body the request body
@@ -217,11 +198,11 @@
{
'method' => method.to_s,
'uri' => uri,
'body' => serializable_body,
'headers' => headers
- }.tap { |h| OrderedHashSerializer.apply_to(h, members) }
+ }
end
# Constructs a new instance from a hash.
#
# @param [Hash] hash the hash to use to construct the instance.
@@ -367,11 +348,10 @@
'headers' => headers,
'body' => serializable_body,
'http_version' => http_version
}.tap do |hash|
hash['adapter_metadata'] = adapter_metadata unless adapter_metadata.empty?
- OrderedHashSerializer.apply_to(hash, members)
end
end
# Constructs a new instance from a hash.
#
@@ -401,24 +381,61 @@
# Checks if the type of encoding is one of "gzip" or "deflate".
def compressed?
%w[ gzip deflate ].include? content_encoding
end
+ # Checks if VCR decompressed the response body
+ def vcr_decompressed?
+ adapter_metadata['vcr_decompressed']
+ 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.
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
+ 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
+
begin
require 'zlib'
require 'stringio'
HAVE_ZLIB = true
rescue LoadError
@@ -461,11 +478,11 @@
# and can be easily serialized.
# @see ResponseStatus.from_hash
def to_hash
{
'code' => code, 'message' => message
- }.tap { |h| OrderedHashSerializer.apply_to(h, members) }
+ }
end
# Constructs a new instance from a hash.
#
# @param [Hash] hash the hash to use to construct the instance.
@@ -494,12 +511,10 @@
def to_hash
{
'request' => request.to_hash,
'response' => response.to_hash,
'recorded_at' => recorded_at.httpdate
- }.tap do |hash|
- OrderedHashSerializer.apply_to(hash, members)
- end
+ }
end
# Constructs a new instance from a hash.
#
# @param [Hash] hash the hash to use to construct the instance.