Class: Dupe::Network

Inherits:
Object
  • Object
show all
Includes:
RestValidation
Defined in:
lib/superdupe/log.rb,
lib/superdupe/mock.rb,
lib/superdupe/mock.rb,
lib/superdupe/mock.rb,
lib/superdupe/mock.rb,
lib/superdupe/mock.rb,
lib/superdupe/network.rb,
lib/superdupe/rest_validation.rb

Overview

:nodoc:

Defined Under Namespace

Modules: RestValidation Classes: DeleteMock, GetMock, Log, Mock, PostMock, PutMock, RequestNotFoundError, UnknownRestVerbError

Constant Summary collapse

VERBS =

:nodoc:

[:get, :post, :put, :delete]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RestValidation

#validate_request_type

Constructor Details

#initializeNetwork

Returns a new instance of Network.



9
10
11
12
13
# File 'lib/superdupe/network.rb', line 9

def initialize
  @mocks = {}
  @log = Dupe::Network::Log.new
  VERBS.each { |verb| @mocks[verb] = [] }
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



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

def log
  @log
end

#mocksObject (readonly)

Returns the value of attribute mocks.



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

def mocks
  @mocks
end

Instance Method Details

#define_service_mock(verb, url_pattern, response_proc = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/superdupe/network.rb', line 20

def define_service_mock(verb, url_pattern, response_proc=nil)
  validate_request_type verb
  case verb
  when :get
    GetMock.new(url_pattern, response_proc).tap do |mock|
      @mocks[verb] << mock
    end
  when :post
    PostMock.new(url_pattern, response_proc).tap do |mock|
      @mocks[verb].unshift mock
    end
  when :put
    PutMock.new(url_pattern, response_proc).tap do |mock|
      @mocks[verb].unshift mock
    end
  when :delete
    DeleteMock.new(url_pattern, response_proc).tap do |mock|
      @mocks[verb].unshift mock
    end
  else
    raise StandardError, "Dupe does not support mocking #{verb.to_s.upcase} requests."
  end
end

#request(verb, url, body = nil) ⇒ Object



15
16
17
18
# File 'lib/superdupe/network.rb', line 15

def request(verb, url, body=nil)
  validate_request_type verb
  match(verb, url).mocked_response(url, body)
end