Class: Dupe::Network::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/superdupe/mock.rb

Overview

:nodoc:

Direct Known Subclasses

DeleteMock, GetMock, PostMock, PutMock

Defined Under Namespace

Classes: ResourceNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_pattern, response_proc = nil) ⇒ Mock

Returns a new instance of Mock.

Raises:

  • (ArgumentError)


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

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/superdupe/mock.rb', line 7

def response
  @response
end

#url_patternObject (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

Returns:

  • (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

Raises:

  • (StandardError)


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

Raises:

  • (NotImplementedError)


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