Class: VCR::Cassette

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

Overview

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

Defined Under Namespace

Classes: Persisters, Serializers

Constant Summary

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/vcr/cassette.rb', line 45

def initialize(name, options = {})
  @name    = name
  @options = VCR.configuration.default_cassette_options.merge(options)

  assert_valid_options!
  extract_options
  raise_error_unless_valid_record_mode

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

Instance Attribute Details

#erbBoolean, Hash (readonly)



34
35
36
# File 'lib/vcr/cassette.rb', line 34

def erb
  @erb
end

#match_requests_onArray<Symbol, #call> (readonly)



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

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.

See Also:



21
22
23
# File 'lib/vcr/cassette.rb', line 21

def name
  @name
end

#re_record_intervalInteger? (readonly)



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

def re_record_interval
  @re_record_interval
end

#record_modeSymbol (readonly)



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

def record_mode
  @record_mode
end

#tagsArray<Symbol> (readonly)



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

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.



59
60
61
62
# File 'lib/vcr/cassette.rb', line 59

def eject
  write_recorded_interactions_to_disk
  http_interactions.assert_no_unused_interactions! unless @allow_unused_http_interactions
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.

Raises:

  • (NotImplementedError)

    if the configured cassette persister does not support resolving file paths.



89
90
91
92
93
94
# File 'lib/vcr/cassette.rb', line 89

def file
  unless @persister.respond_to?(:absolute_path_to_file)
    raise NotImplementedError, "The configured cassette persister does not support resolving file paths"
  end
  @persister.absolute_path_to_file(storage_key)
end

#recording?Boolean



97
98
99
100
101
102
103
# File 'lib/vcr/cassette.rb', line 97

def recording?
  case record_mode
    when :none; false
    when :once; raw_cassette_bytes.to_s.empty?
    else true
  end
end

#serializable_hashHash



106
107
108
109
110
111
# File 'lib/vcr/cassette.rb', line 106

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