Module: VCR::Cassette::Serializers::YAML

Extended by:
EncodingErrorHandling, YAML, VCR::Cassette::SyntaxErrorHandling
Included in:
YAML
Defined in:
lib/vcr/cassette/serializers/yaml.rb

Overview

The YAML serializer. This will use either Psych or Syck, which ever your ruby interpreter defaults to. You can also force VCR to use Psych or Syck by using one of those serializers.

See Also:

Constant Summary collapse

ENCODING_ERRORS =
[ArgumentError]
SYNTAX_ERRORS =
[::Psych::SyntaxError]

Instance Method Summary collapse

Methods included from EncodingErrorHandling

handle_encoding_errors

Methods included from VCR::Cassette::SyntaxErrorHandling

handle_syntax_errors

Instance Method Details

#deserialize(string) ⇒ Hash

Deserializes the given string using YAML.

Parameters:

  • string (String)

    the YAML string

Returns:

  • (Hash)

    the deserialized object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vcr/cassette/serializers/yaml.rb', line 47

def deserialize(string)
  handle_encoding_errors do
    handle_syntax_errors do
      if ::YAML.respond_to?(:unsafe_load)
        ::YAML.unsafe_load(string)
      else
        ::YAML.load(string)
      end
    end
  end
end

#file_extensionString

The file extension to use for this serializer.

Returns:

  • (String)

    “yml”



27
28
29
# File 'lib/vcr/cassette/serializers/yaml.rb', line 27

def file_extension
  "yml"
end

#serialize(hash) ⇒ String

Serializes the given hash using YAML.

Parameters:

  • hash (Hash)

    the object to serialize

Returns:

  • (String)

    the YAML string



35
36
37
38
39
40
41
# File 'lib/vcr/cassette/serializers/yaml.rb', line 35

def serialize(hash)
  handle_encoding_errors do
    result = ::YAML.dump(hash)
    result.gsub!(": \n", ": null\n") # set canonical null value in order to avoid trailing whitespaces
    result
  end
end