Class: Typhoeus::Expectation
- Inherits:
-
Object
- Object
- Typhoeus::Expectation
- Defined in:
- lib/typhoeus/expectation.rb
Overview
This class represents an expectation. It is part of the stubbing mechanism. An expectation contains an url and options, like a request. They were compared to the request url and options in order to evaluate wether they match. If thats the case, the attached responses were returned one by one.
Instance Attribute Summary collapse
- #base_url ⇒ Object readonly private
- #from ⇒ Object readonly private
- #options ⇒ Object readonly private
Class Method Summary collapse
-
.all ⇒ Array<Typhoeus::Expectation>
Returns all expectations.
-
.clear ⇒ Object
Clears expectations.
-
.find_by(request) ⇒ Expectation
private
Returns expecation matching the provided request.
Instance Method Summary collapse
-
#and_return(response) ⇒ void
Specify what should be returned, when this expectation is hit.
-
#initialize(base_url, options = {}) ⇒ Expectation
constructor
private
Creates an expectation.
-
#matches?(request) ⇒ Boolean
private
Checks wether this expectation matches the provided request.
-
#response ⇒ Response
private
Return the response.
-
#responses ⇒ Array<Typhoeus::Response>
private
Return canned responses.
-
#stubbed_from(value) ⇒ Expectation
private
Set from value to mark an expectaion.
Constructor Details
#initialize(base_url, options = {}) ⇒ Expectation
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an expectation.
74 75 76 77 78 79 |
# File 'lib/typhoeus/expectation.rb', line 74 def initialize(base_url, = {}) @base_url = base_url @options = @response_counter = 0 @from = nil end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
20 21 22 |
# File 'lib/typhoeus/expectation.rb', line 20 def base_url @base_url end |
#from ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
26 27 28 |
# File 'lib/typhoeus/expectation.rb', line 26 def from @from end |
#options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
23 24 25 |
# File 'lib/typhoeus/expectation.rb', line 23 def @options end |
Class Method Details
.all ⇒ Array<Typhoeus::Expectation>
Returns all expectations.
36 37 38 |
# File 'lib/typhoeus/expectation.rb', line 36 def all @expectations ||= [] end |
.clear ⇒ Object
Clears expectations. This is handy while testing and you want to make sure, that you don’t get canned responses.
46 47 48 |
# File 'lib/typhoeus/expectation.rb', line 46 def clear all.clear end |
.find_by(request) ⇒ Expectation
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns expecation matching the provided request.
59 60 61 62 63 |
# File 'lib/typhoeus/expectation.rb', line 59 def find_by(request) all.find do |expectation| expectation.matches?(request) end end |
Instance Method Details
#and_return(response) ⇒ void
This method returns an undefined value.
Specify what should be returned, when this expectation is hit.
104 105 106 |
# File 'lib/typhoeus/expectation.rb', line 104 def and_return(response) responses << response end |
#matches?(request) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks wether this expectation matches the provided request.
119 120 121 |
# File 'lib/typhoeus/expectation.rb', line 119 def matches?(request) url_match?(request.base_url) && (request) end |
#response ⇒ Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return the response. When there are multiple responses, they were returned one by one.
145 146 147 148 149 150 |
# File 'lib/typhoeus/expectation.rb', line 145 def response response = responses.fetch(@response_counter, responses.last) @response_counter += 1 response.mock = @from || true response end |
#responses ⇒ Array<Typhoeus::Response>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return canned responses.
131 132 133 |
# File 'lib/typhoeus/expectation.rb', line 131 def responses @responses ||= [] end |
#stubbed_from(value) ⇒ Expectation
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set from value to mark an expectaion. Useful for other libraries, eg. webmock.
92 93 94 95 |
# File 'lib/typhoeus/expectation.rb', line 92 def stubbed_from(value) @from = value self end |