class VCR::RequestMatcher

Constants

DEFAULT_MATCH_ATTRIBUTES
VALID_MATCH_ATTRIBUTES

Attributes

match_attributes[R]
request[R]

Public Class Methods

new(request = nil, match_attributes = []) click to toggle source
# File lib/vcr/request_matcher.rb, line 10
def initialize(request = nil, match_attributes = [])
  if (match_attributes - VALID_MATCH_ATTRIBUTES).size > 0
    raise ArgumentError.new("The only valid match_attributes options are: #{VALID_MATCH_ATTRIBUTES.inspect}.  You passed: #{match_attributes.inspect}.")
  end

  @request, self.match_attributes = request, match_attributes
end

Public Instance Methods

==(other) click to toggle source
# File lib/vcr/request_matcher.rb, line 62
def ==(other)
  %w( class match_attributes method uri headers body ).all? do |attr|
    send(attr) == other.send(attr)
  end
end
body() click to toggle source
# File lib/vcr/request_matcher.rb, line 50
def body
  request.body if match_requests_on?(:body)
end
eql?(other) click to toggle source
# File lib/vcr/request_matcher.rb, line 58
def eql?(other)
  self == other
end
hash() click to toggle source
# File lib/vcr/request_matcher.rb, line 68
def hash
  # on Ruby 1.8.6, identical sets have different hash values,
  # but identical arrays have the same hash values,
  # so we convert match_attributes to an array here.
  [match_attributes.to_a, method, uri, sorted_header_array, body].hash
end
headers() click to toggle source
# File lib/vcr/request_matcher.rb, line 46
def headers
  request.headers if match_requests_on?(:headers)
end
match_attributes=(attributes) click to toggle source
# File lib/vcr/request_matcher.rb, line 18
def match_attributes=(attributes)
  # Unfortunately, 1.9.2 doesn't give the same hash code
  # for two sets of the same elements unless they are ordered
  # the same, so we sort the attributes here.
  attributes = attributes.sort { |a, b| a.to_s <=> b.to_s }
  @match_attributes = set(attributes)
end
match_requests_on?(attribute) click to toggle source
# File lib/vcr/request_matcher.rb, line 54
def match_requests_on?(attribute)
  match_attributes.include?(attribute)
end
method() click to toggle source
# File lib/vcr/request_matcher.rb, line 42
def method
  request.method if match_requests_on?(:method)
end
uri() click to toggle source
# File lib/vcr/request_matcher.rb, line 26
def uri
  return request.uri unless request.uri.is_a?(String)
  uri_matchers = match_attributes.to_a & [:uri, :host, :path]

  case set(uri_matchers)
    when set then %r.*/
    when set(:uri) then request.uri
    when set(:host) then VCR::Regexes.url_regex_for_hosts([URI(request.uri).host])
    when set(:path) then VCR::Regexes.url_regex_for_path(URI(request.uri).path)
    when set(:host, :path)
      uri = URI(request.uri)
      VCR::Regexes.url_regex_for_host_and_path(uri.host, uri.path)
    else raise ArgumentError.new("match_attributes cannot include #{uri_matchers.join(' and ')}")
  end
end