class VCR::Cassette::HTTPInteractionList

@private

Attributes

allow_playback_repeats[R]
interactions[R]
parent_list[R]
request_matchers[R]

Public Class Methods

new(interactions, request_matchers, allow_playback_repeats = false, parent_list = NullList, log_prefix = '') click to toggle source
# 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
  @mutex                  = Mutex.new

  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

Public Instance Methods

assert_no_unused_interactions!() click to toggle source

Checks if there are no unused interactions left.

@raise [VCR::Errors::UnusedHTTPInteractionError] if not all interactions were played back.

# File lib/vcr/cassette/http_interaction_list.rb, line 65
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) click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 48
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) click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 54
def has_used_interaction_matching?(request)
  @used_interactions.any? { |i| interaction_matches_request?(request, i) }
end
remaining_unused_interaction_count() click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 58
def remaining_unused_interaction_count
  @interactions.size
end
response_for(request) click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 31
def response_for(request)
  # Without this mutex, under threaded access, the wrong response may be removed
  # out of the (remaining) interactions list (and other problems).
  @mutex.synchronize do
    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
end

Private Instance Methods

has_unused_interactions?() click to toggle source

@return [Boolean] Whether or not there are unused interactions left in the list.

# File lib/vcr/cassette/http_interaction_list.rb, line 79
def has_unused_interactions?
  @interactions.size > 0
end
interaction_matches_request?(request, interaction) click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 96
def interaction_matches_request?(request, interaction)
  log "Checking if #{request_summary(request)} matches #{request_summary(interaction.request)} using #{@request_matchers.inspect}", 1
  @request_matchers.all? do |matcher_name|
    matcher = VCR.request_matchers[matcher_name]
    matcher.matches?(request, interaction.request).tap do |matched|
      matched = matched ? 'matched' : 'did not match'
      log "#{matcher_name} (#{matched}): current request #{request_summary(request)} vs #{request_summary(interaction.request)}", 2
    end
  end
end
log_prefix() click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 107
def log_prefix
  @log_prefix
end
matching_interaction_index_for(request) click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 87
def matching_interaction_index_for(request)
  @interactions.index { |i| interaction_matches_request?(request, i) }
end
matching_used_interaction_for(request) click to toggle source
# File lib/vcr/cassette/http_interaction_list.rb, line 91
def matching_used_interaction_for(request)
  return nil unless @allow_playback_repeats
  @used_interactions.find { |i| interaction_matches_request?(request, i) }
end
request_summary(request) click to toggle source
Calls superclass method VCR::Logger::Mixin#request_summary
# File lib/vcr/cassette/http_interaction_list.rb, line 83
def request_summary(request)
  super(request, @request_matchers)
end