Class: FakeWebMatcher::RequestMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_web_matcher/request_matcher.rb

Overview

Matcher class, following RSpec’s expectations. Used to confirm whether a request has been made on a given method and URI.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RequestMatcher

Create a new matcher.

Parameters:

  • method (Symbol)

    The HTTP method. Defaults to :any if not supplied.

  • uri (String)

    The URI to check for



13
14
15
# File 'lib/fake_web_matcher/request_matcher.rb', line 13

def initialize(*args)
  @method, @url = args_split(*args)
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



6
7
8
# File 'lib/fake_web_matcher/request_matcher.rb', line 6

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/fake_web_matcher/request_matcher.rb', line 6

def url
  @url
end

Instance Method Details

#failure_messageString

Failure message if the URI should have been requested.

Returns:

  • (String)

    failure message



34
35
36
37
38
39
40
# File 'lib/fake_web_matcher/request_matcher.rb', line 34

def failure_message
  if @method == :any
    "The URL #{@url} was not requested."
  else
    "The URL #{@url} was not requested using #{formatted_method}."
  end
end

#matches?(fakeweb) ⇒ Boolean

Indication of whether there’s a match on the URI from given requests.

Parameters:

  • FakeWeb (Module)

    Module, necessary for RSpec, although not required internally.

Returns:

  • (Boolean)

    true if the URI was requested, otherwise false.



23
24
25
26
27
28
# File 'lib/fake_web_matcher/request_matcher.rb', line 23

def matches?(fakeweb)
  !FakeWeb::Registry.instance.requests.detect { |req|
    method, url = args_split(*req)
    match_method(method) && url == @url
  }.nil?
end

#negative_failure_messageString

Failure message if the URI should not have been requested.

Returns:

  • (String)

    failure message



46
47
48
49
50
51
52
# File 'lib/fake_web_matcher/request_matcher.rb', line 46

def negative_failure_message
  if @method == :any
    "The URL #{@url} was requested and should not have been."
  else
    "The URL #{@url} was requested using #{formatted_method} and should not have been."
  end
end