Class: VCR::Cassette::HTTPInteractionList

Inherits:
Object
  • Object
show all
Includes:
Logger::Mixin
Defined in:
lib/vcr/cassette/http_interaction_list.rb

Defined Under Namespace

Modules: NullList

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger::Mixin

#log, #response_summary

Constructor Details

#initialize(interactions, request_matchers, allow_playback_repeats = false, parent_list = NullList, log_prefix = '') ⇒ HTTPInteractionList

Returns a new instance of HTTPInteractionList.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vcr/cassette/http_interaction_list.rb', line 18

def initialize(interactions, request_matchers, allow_playback_repeats = false, parent_list = NullList, log_prefix = '')
  @interactions           = interactions.dup
  @request_matchers       = request_matchers
  @allow_playback_repeats = allow_playback_repeats
  @parent_list            = parent_list
  @used_interactions      = []
  @log_prefix             = log_prefix

  interaction_summaries = interactions.map { |i| "#{request_summary(i.request)} => #{response_summary(i.response)}" }
  log "Initialized HTTPInteractionList with request matchers #{request_matchers.inspect} and #{interactions.size} interaction(s): { #{interaction_summaries.join(', ')} }", 1
end

Instance Attribute Details

#allow_playback_repeatsObject (readonly)

Returns the value of attribute allow_playback_repeats.



16
17
18
# File 'lib/vcr/cassette/http_interaction_list.rb', line 16

def allow_playback_repeats
  @allow_playback_repeats
end

#interactionsObject (readonly)

Returns the value of attribute interactions.



16
17
18
# File 'lib/vcr/cassette/http_interaction_list.rb', line 16

def interactions
  @interactions
end

#parent_listObject (readonly)

Returns the value of attribute parent_list.



16
17
18
# File 'lib/vcr/cassette/http_interaction_list.rb', line 16

def parent_list
  @parent_list
end

#request_matchersObject (readonly)

Returns the value of attribute request_matchers.



16
17
18
# File 'lib/vcr/cassette/http_interaction_list.rb', line 16

def request_matchers
  @request_matchers
end

Instance Method Details

#assert_no_unused_interactions!Object

Checks if there are no unused interactions left.

Raises:



60
61
62
63
64
65
66
67
68
69
# File 'lib/vcr/cassette/http_interaction_list.rb', line 60

def assert_no_unused_interactions!
  return unless has_unused_interactions?
  logger = Logger.new(nil)

  descriptions = @interactions.map do |i|
    "  - #{logger.request_summary(i.request, @request_matchers)} => #{logger.response_summary(i.response)}"
  end.join("\n")

  raise Errors::UnusedHTTPInteractionError, "There are unused HTTP interactions left in the cassette:\n#{descriptions}"
end

#has_interaction_matching?(request) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_interaction_matching?(request)
  !!matching_interaction_index_for(request) ||
  !!matching_used_interaction_for(request) ||
  @parent_list.has_interaction_matching?(request)
end

#has_used_interaction_matching?(request) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/vcr/cassette/http_interaction_list.rb', line 49

def has_used_interaction_matching?(request)
  @used_interactions.any? { |i| interaction_matches_request?(request, i) }
end

#remaining_unused_interaction_countObject



53
54
55
# File 'lib/vcr/cassette/http_interaction_list.rb', line 53

def remaining_unused_interaction_count
  @interactions.size
end

#response_for(request) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vcr/cassette/http_interaction_list.rb', line 30

def response_for(request)
  if index = matching_interaction_index_for(request)
    interaction = @interactions.delete_at(index)
    @used_interactions.unshift interaction
    log "Found matching interaction for #{request_summary(request)} at index #{index}: #{response_summary(interaction.response)}", 1
    interaction.response
  elsif interaction = matching_used_interaction_for(request)
    interaction.response
  else
    @parent_list.response_for(request)
  end
end