File: compressed.rb

package info (click to toggle)
ruby-vcr 6.0.0%2Breally5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,320 kB
  • sloc: ruby: 8,456; sh: 177; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,345 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'zlib'
require 'vcr/cassette/serializers'

module VCR
  class Cassette
    class Serializers
      # The compressed serializer. This serializer wraps the YAML serializer
      # to write compressed cassettes to disk.
      #
      # Cassettes containing responses with JSON data often compress at greater
      # than 10:1. The tradeoff is that cassettes will not diff nicely or be
      # easily inspectable or editable.
      #
      # @see YAML
      module Compressed
        extend self

        # The file extension to use for this serializer.
        #
        # @return [String] "zz"
        def file_extension
          'zz'
        end

        # Serializes the given hash using YAML and Zlib.
        #
        # @param [Hash] hash the object to serialize
        # @return [String] the compressed cassette data
        def serialize(hash)
          string = VCR::Cassette::Serializers::YAML.serialize(hash)
          Zlib::Deflate.deflate(string)
        end

        # Deserializes the given compressed cassette data.
        #
        # @param [String] string the compressed YAML cassette data
        # @return [Hash] the deserialized object
        def deserialize(string)
          yaml = Zlib::Inflate.inflate(string)
          VCR::Cassette::Serializers::YAML.deserialize(yaml)
        end
      end
    end
  end
end