Class: Dupe::Network::Mock
- Inherits:
-
Object
- Object
- Dupe::Network::Mock
- Defined in:
- lib/superdupe/mock.rb
Overview
:nodoc:
Direct Known Subclasses
Defined Under Namespace
Classes: ResourceNotFoundError
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#url_pattern ⇒ Object
readonly
Returns the value of attribute url_pattern.
Instance Method Summary collapse
-
#initialize(url_pattern, response_proc = nil) ⇒ Mock
constructor
A new instance of Mock.
- #match?(url) ⇒ Boolean
- #mocked_response(url, body = nil) ⇒ Object
- #process_response(resp, url) ⇒ Object
Constructor Details
#initialize(url_pattern, response_proc = nil) ⇒ Mock
Returns a new instance of Mock.
9 10 11 12 13 14 15 16 17 |
# File 'lib/superdupe/mock.rb', line 9 def initialize(url_pattern, response_proc=nil) raise( ArgumentError, "The URL pattern parameter must be a type of regular expression." ) unless url_pattern.kind_of?(Regexp) @response = response_proc || proc {} @url_pattern = url_pattern end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/superdupe/mock.rb', line 7 def response @response end |
#url_pattern ⇒ Object (readonly)
Returns the value of attribute url_pattern.
6 7 8 |
# File 'lib/superdupe/mock.rb', line 6 def url_pattern @url_pattern end |
Instance Method Details
#match?(url) ⇒ Boolean
19 20 21 |
# File 'lib/superdupe/mock.rb', line 19 def match?(url) url_pattern =~ url ? true : false end |
#mocked_response(url, body = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/superdupe/mock.rb', line 23 def mocked_response(url, body = nil) raise( StandardError, "Tried to mock a response to a non-matched url! This should never occur. My pattern: #{@url_pattern}. Url: #{url}" ) unless match?(url) grouped_results = url_pattern.match(url)[1..-1] grouped_results << body if body resp = @response.call *grouped_results process_response(resp, url) end |
#process_response(resp, url) ⇒ Object
34 35 36 |
# File 'lib/superdupe/mock.rb', line 34 def process_response(resp, url) raise NotImplementedError, "When you extend Dupe::Network::Mock, you must implement the #process_response instance method." end |