Class: VCR::Cassette
- Inherits:
-
Object
- Object
- VCR::Cassette
- Includes:
- Logger::Mixin
- 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/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,
lib/vcr/cassette/persisters/file_system.rb
Overview
The media VCR uses to store HTTP interactions for later re-use.
Defined Under Namespace
Modules: EncodingErrorHandling Classes: ERBRenderer, HTTPInteractionList, Migrator, Persisters, 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
-
#erb ⇒ Boolean, Hash
readonly
The cassette’s ERB option.
-
#match_requests_on ⇒ Array<Symbol, #call>
readonly
List of request matchers.
-
#name ⇒ #to_s
readonly
The name of the cassette.
-
#re_record_interval ⇒ Integer?
readonly
How frequently (in seconds) the cassette should be re-recorded.
-
#record_mode ⇒ Symbol
readonly
The record mode.
-
#tags ⇒ Array<Symbol>
readonly
If set, VCR::Configuration#before_record and VCR::Configuration#before_playback hooks with a corresponding tag will apply.
Class Method Summary collapse
Instance Method Summary collapse
-
#eject(options = {}) ⇒ Object
Ejects the current cassette.
-
#file ⇒ String
The file for this cassette.
- #http_interactions ⇒ Object
-
#initialize(name, options = {}) ⇒ Cassette
constructor
A new instance of Cassette.
- #new_recorded_interactions ⇒ Object
-
#originally_recorded_at ⇒ Time?
The ‘recorded_at` time of the first HTTP interaction or nil if the cassette has no prior HTTP interactions.
- #record_http_interaction(interaction) ⇒ Object
-
#recording? ⇒ Boolean
Whether or not the cassette is recording.
-
#serializable_hash ⇒ Hash
The hash that will be serialized when the cassette is written to disk.
Methods included from Logger::Mixin
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, = {}) @name = name = VCR.configuration..merge() raise_error_unless_valid_record_mode log "Initialized with options: #{@options.inspect}" end |
Instance Attribute Details
#erb ⇒ Boolean, 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.
34 35 36 |
# File 'lib/vcr/cassette.rb', line 34 def erb @erb end |
#match_requests_on ⇒ Array<Symbol, #call> (readonly)
Returns List of request matchers. Used to find a response from an existing HTTP interaction to play back.
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.
21 22 23 |
# File 'lib/vcr/cassette.rb', line 21 def name @name end |
#re_record_interval ⇒ Integer? (readonly)
Returns How frequently (in seconds) the cassette should be re-recorded.
37 38 39 |
# File 'lib/vcr/cassette.rb', line 37 def re_record_interval @re_record_interval end |
#record_mode ⇒ Symbol (readonly)
Returns The record mode. Determines whether the cassette records HTTP interactions, plays them back, or does both.
25 26 27 |
# File 'lib/vcr/cassette.rb', line 25 def record_mode @record_mode end |
#tags ⇒ Array<Symbol> (readonly)
Returns If set, VCR::Configuration#before_record and VCR::Configuration#before_playback hooks with a corresponding tag will apply.
41 42 43 |
# File 'lib/vcr/cassette.rb', line 41 def end |
Class Method Details
.const_missing(const) ⇒ Object
17 18 19 20 21 |
# File 'lib/vcr/deprecations.rb', line 17 def Cassette.const_missing(const) return super unless const == :MissingERBVariableError warn "WARNING: `VCR::Cassette::MissingERBVariableError` is deprecated. Use `VCR::Errors::MissingERBVariableError` instead." Errors::MissingERBVariableError end |
Instance Method Details
#eject(options = {}) ⇒ Object
This is not intended to be called directly. Use ‘VCR.eject_cassette` instead.
Ejects the current cassette. The cassette will no longer be used. In addition, any newly recorded HTTP interactions will be written to disk.
64 65 66 67 68 69 70 |
# File 'lib/vcr/cassette.rb', line 64 def eject( = {}) write_recorded_interactions_to_disk if should_assert_no_unused_interactions? && ![:skip_no_unused_interactions_assertion] http_interactions.assert_no_unused_interactions! end end |
#file ⇒ String
VCR will take care of sanitizing the cassette name to make it a valid file name.
Returns The file for this cassette.
97 98 99 100 101 102 |
# File 'lib/vcr/cassette.rb', line 97 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 |
#http_interactions ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/vcr/cassette.rb', line 73 def http_interactions @http_interactions ||= HTTPInteractionList.new \ should_stub_requests? ? previously_recorded_interactions : [], match_requests_on, @allow_playback_repeats, @parent_list, log_prefix end |
#new_recorded_interactions ⇒ Object
89 90 91 |
# File 'lib/vcr/cassette.rb', line 89 def new_recorded_interactions @new_recorded_interactions ||= [] end |
#originally_recorded_at ⇒ Time?
Returns The ‘recorded_at` time of the first HTTP interaction or nil if the cassette has no prior HTTP interactions.
131 132 133 |
# File 'lib/vcr/cassette.rb', line 131 def originally_recorded_at @originally_recorded_at ||= previously_recorded_interactions.map(&:recorded_at).min end |
#record_http_interaction(interaction) ⇒ Object
83 84 85 86 |
# File 'lib/vcr/cassette.rb', line 83 def record_http_interaction(interaction) log "Recorded HTTP interaction #{request_summary(interaction.request)} => #{response_summary(interaction.response)}" new_recorded_interactions << interaction end |
#recording? ⇒ Boolean
Returns Whether or not the cassette is recording.
105 106 107 108 109 110 111 |
# File 'lib/vcr/cassette.rb', line 105 def recording? case record_mode when :none; false when :once; raw_cassette_bytes.to_s.empty? else true end end |
#serializable_hash ⇒ Hash
Returns The hash that will be serialized when the cassette is written to disk.
114 115 116 117 118 119 |
# File 'lib/vcr/cassette.rb', line 114 def serializable_hash { "http_interactions" => interactions_to_record.map(&:to_hash), "recorded_with" => "VCR #{VCR.version}" } end |