Class: Typhoeus::HydraMock
- Inherits:
-
Object
- Object
- Typhoeus::HydraMock
- Defined in:
- lib/typhoeus/hydra_mock.rb
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#requests ⇒ Object
readonly
Returns the value of attribute requests.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #add_request(request) ⇒ Object
- #and_return(val) ⇒ Object
- #body ⇒ Object
- #body? ⇒ Boolean
- #headers ⇒ Object
- #headers? ⇒ Boolean
-
#initialize(url, method, options = {}) ⇒ HydraMock
constructor
A new instance of HydraMock.
- #matches?(request) ⇒ Boolean
- #response ⇒ Object
Constructor Details
#initialize(url, method, options = {}) ⇒ HydraMock
Returns a new instance of HydraMock.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/typhoeus/hydra_mock.rb', line 5 def initialize(url, method, = {}) @url = url @uri = URI.parse(url) if url.kind_of?(String) @method = method @requests = [] @options = if @options[:headers] @options[:headers] = Typhoeus::Header.new(@options[:headers]) end @current_response_index = 0 end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
3 4 5 |
# File 'lib/typhoeus/hydra_mock.rb', line 3 def method @method end |
#requests ⇒ Object (readonly)
Returns the value of attribute requests.
3 4 5 |
# File 'lib/typhoeus/hydra_mock.rb', line 3 def requests @requests end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
3 4 5 |
# File 'lib/typhoeus/hydra_mock.rb', line 3 def uri @uri end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
3 4 5 |
# File 'lib/typhoeus/hydra_mock.rb', line 3 def url @url end |
Instance Method Details
#add_request(request) ⇒ Object
34 35 36 |
# File 'lib/typhoeus/hydra_mock.rb', line 34 def add_request(request) @requests << request end |
#and_return(val) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/typhoeus/hydra_mock.rb', line 38 def and_return(val) if val.respond_to?(:each) @responses = val else @responses = [val] end # make sure to mark them as a mock. @responses.each { |r| r.mock = true } val end |
#body ⇒ Object
18 19 20 |
# File 'lib/typhoeus/hydra_mock.rb', line 18 def body @options[:body] end |
#body? ⇒ Boolean
22 23 24 |
# File 'lib/typhoeus/hydra_mock.rb', line 22 def body? @options.has_key?(:body) end |
#headers ⇒ Object
26 27 28 |
# File 'lib/typhoeus/hydra_mock.rb', line 26 def headers @options[:headers] end |
#headers? ⇒ Boolean
30 31 32 |
# File 'lib/typhoeus/hydra_mock.rb', line 30 def headers? @options.has_key?(:headers) end |
#matches?(request) ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/typhoeus/hydra_mock.rb', line 61 def matches?(request) if !method_matches?(request) or !url_matches?(request) return false end if body? return false unless body_matches?(request) end if headers? return false unless headers_match?(request) end true end |
#response ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/typhoeus/hydra_mock.rb', line 51 def response if @current_response_index == (@responses.length - 1) @responses.last else value = @responses[@current_response_index] @current_response_index += 1 value end end |