Class: VCR::Cassette

Inherits:
Object
  • Object
show all
Defined in:
lib/vcr/cassette.rb,
lib/vcr/cassette/reader.rb,
lib/vcr/cassette/migrator.rb,
lib/vcr/cassette/serializers.rb,
lib/vcr/cassette/serializers/json.rb,
lib/vcr/cassette/serializers/syck.rb,
lib/vcr/cassette/serializers/yaml.rb,
lib/vcr/cassette/serializers/psych.rb,
lib/vcr/cassette/http_interaction_list.rb

Overview

The media VCR uses to store HTTP interactions for later re-use.

Defined Under Namespace

Classes: Serializers

Constant Summary collapse

VALID_RECORD_MODES =

The supported record modes.

  • :all -- Record every HTTP interactions; do not play any back.
  • :none -- Do not record any HTTP interactions; play them back.
  • :new_episodes -- Playback previously recorded HTTP interactions and record new ones.
  • :once -- Record the HTTP interactions if the cassette has not already been recorded; otherwise, playback the HTTP interactions.
[:all, :none, :new_episodes, :once]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Cassette

Returns a new instance of Cassette.

Parameters:

  • name (#to_s)

    The name of the cassette. VCR will sanitize this to ensure it is a valid file name.

  • options (Hash) (defaults to: {})

    The cassette options. The given options will be merged with the configured default_cassette_options.

See Also:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vcr/cassette.rb', line 48

def initialize(name, options = {})
  options = VCR.configuration.default_cassette_options.merge(options)
  invalid_options = options.keys - [
    :record, :erb, :match_requests_on, :re_record_interval, :tag, :tags,
    :update_content_length_header, :allow_playback_repeats, :exclusive,
    :serialize_with, :preserve_exact_body_bytes, :decode_compressed_response
  ]

  if invalid_options.size > 0
    raise ArgumentError.new("You passed the following invalid options to VCR::Cassette.new: #{invalid_options.inspect}.")
  end

  @name                         = name
  @record_mode                  = options[:record]
  @erb                          = options[:erb]
  @match_requests_on            = options[:match_requests_on]
  @re_record_interval           = options[:re_record_interval]
  @tags                         = Array(options.fetch(:tags) { options[:tag] })
  @tags                         << :update_content_length_header if options[:update_content_length_header]
  @tags                         << :preserve_exact_body_bytes if options[:preserve_exact_body_bytes]
  @tags                         << :decode_compressed_response if options[:decode_compressed_response]
  @allow_playback_repeats       = options[:allow_playback_repeats]
  @exclusive                    = options[:exclusive]
  @serializer                   = VCR.cassette_serializers[options[:serialize_with]]
  @record_mode                  = :all if should_re_record?
  @parent_list                  = @exclusive ? HTTPInteractionList::NullList : VCR.http_interactions

  raise_error_unless_valid_record_mode

  log "Initialized with options: #{options.inspect}"
end

Instance Attribute Details

#erbBoolean, Hash (readonly)

Returns The cassette's ERB option. The file will be treated as an ERB template if this has a truthy value. A hash, if provided, will be used as local variables for the ERB template.

Returns:

  • (Boolean, Hash)

    The cassette's ERB option. The file will be treated as an ERB template if this has a truthy value. A hash, if provided, will be used as local variables for the ERB template.



37
38
39
# File 'lib/vcr/cassette.rb', line 37

def erb
  @erb
end

#match_requests_onArray<Symbol, #call> (readonly)

Returns List of request matchers. Used to find a response from an existing HTTP interaction to play back.

Returns:

  • (Array<Symbol, #call>)

    List of request matchers. Used to find a response from an existing HTTP interaction to play back.



32
33
34
# File 'lib/vcr/cassette.rb', line 32

def match_requests_on
  @match_requests_on
end

#name#to_s (readonly)

Returns The name of the cassette. Used to determine the cassette's file name.

Returns:

  • (#to_s)

    The name of the cassette. Used to determine the cassette's file name.

See Also:



24
25
26
# File 'lib/vcr/cassette.rb', line 24

def name
  @name
end

#re_record_intervalInteger? (readonly)

Returns How frequently (in seconds) the cassette should be re-recorded.

Returns:

  • (Integer, nil)

    How frequently (in seconds) the cassette should be re-recorded.



40
41
42
# File 'lib/vcr/cassette.rb', line 40

def re_record_interval
  @re_record_interval
end

#record_modeSymbol (readonly)

Returns The record mode. Determines whether the cassette records HTTP interactions, plays them back, or does both.

Returns:

  • (Symbol)

    The record mode. Determines whether the cassette records HTTP interactions, plays them back, or does both.



28
29
30
# File 'lib/vcr/cassette.rb', line 28

def record_mode
  @record_mode
end

#tagsArray<Symbol> (readonly)

Returns If set, VCR::Configuration#before_record and VCR::Configuration#before_playback hooks with a corresponding tag will apply.

Returns:



44
45
46
# File 'lib/vcr/cassette.rb', line 44

def tags
  @tags
end

Instance Method Details

#ejectObject

Ejects the current cassette. The cassette will no longer be used. In addition, any newly recorded HTTP interactions will be written to disk.



83
84
85
# File 'lib/vcr/cassette.rb', line 83

def eject
  write_recorded_interactions_to_disk
end

#fileString

Note:

VCR will take care of sanitizing the cassette name to make it a valid file name.

Returns The file for this cassette.

Returns:

  • (String)

    The file for this cassette.



110
111
112
113
# File 'lib/vcr/cassette.rb', line 110

def file
  return nil unless VCR.configuration.cassette_library_dir
  File.join(VCR.configuration.cassette_library_dir, "#{sanitized_name}.#{@serializer.file_extension}")
end

#recording?Boolean

Returns Whether or not the cassette is recording.

Returns:

  • (Boolean)

    Whether or not the cassette is recording.



116
117
118
119
120
121
122
# File 'lib/vcr/cassette.rb', line 116

def recording?
  case record_mode
    when :none; false
    when :once; file.nil? || !File.size?(file)
    else true
  end
end

#serializable_hashHash

Returns The hash that will be serialized when the cassette is written to disk.

Returns:

  • (Hash)

    The hash that will be serialized when the cassette is written to disk.



125
126
127
128
129
130
# File 'lib/vcr/cassette.rb', line 125

def serializable_hash
  {
    "http_interactions" => interactions_to_record.map(&:to_hash),
    "recorded_with"     => "VCR #{VCR.version}"
  }
end