Class: ServiceContractWebmock::ServiceMock

Inherits:
Object
  • Object
show all
Defined in:
lib/service_contract_webmock/service_mock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, resources, contract, base_url) ⇒ ServiceMock

Returns a new instance of ServiceMock.



7
8
9
10
11
12
# File 'lib/service_contract_webmock/service_mock.rb', line 7

def initialize(name, resources, contract, base_url)
  @name = name
  @resources = resources
  @contract = contract
  @base_url = base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



5
6
7
# File 'lib/service_contract_webmock/service_mock.rb', line 5

def base_url
  @base_url
end

#contractObject (readonly)

Returns the value of attribute contract.



5
6
7
# File 'lib/service_contract_webmock/service_mock.rb', line 5

def contract
  @contract
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/service_contract_webmock/service_mock.rb', line 5

def name
  @name
end

#resourcesObject (readonly)

Returns the value of attribute resources.



5
6
7
# File 'lib/service_contract_webmock/service_mock.rb', line 5

def resources
  @resources
end

Instance Method Details

#stub_index_requestObject



24
25
26
27
28
# File 'lib/service_contract_webmock/service_mock.rb', line 24

def stub_index_request
  stub_request(:get, "#{name}\\.json\\??$")
    .to_return(body: { name => resources }.to_json,
              headers: { 'Content-Type' => 'application/json' })
end

#stub_request(method, path) ⇒ Object



20
21
22
# File 'lib/service_contract_webmock/service_mock.rb', line 20

def stub_request(method, path)
  WebMock.stub_request(method, %r{#{base_url}/#{path}})
end

#stub_search_requestObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/service_contract_webmock/service_mock.rb', line 53

def stub_search_request
  matcher = ContractMatcher.new(contract.endpoint("index"), resources)
  stub_request(:get, "#{name}\\.json\\?#{matcher.to_regex}").
    to_return do |request|
      {
        body: { name => matcher.found(request.uri.query) }.to_json,
        headers: { 'Content-Type' => 'application/json' }
      }
    end
end

#stub_show_requestObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/service_contract_webmock/service_mock.rb', line 30

def stub_show_request
  return if contract.endpoint("show").nil?
  key = contract.endpoint("show").parameters.first.name
  stub_request(:get, "#{name}/\\d+\\.json").
    to_return do |request|
      id = request.uri.path.scan(/\/(\d+)\.json/)[0][0].to_i
      found = resources.select {|r| r[key] == id}

      if found.present?
        {
          body: { name => found }.to_json,
          headers: { 'Content-Type' => 'application/json' }
        }
      else
        {
          body: { meta: { status: 404, errors: ['resource not found'] } }.to_json,
          headers: { 'Content-Type' => 'application/json' },
          status: 404
        }
      end
    end
end

#webmock!Object



14
15
16
17
18
# File 'lib/service_contract_webmock/service_mock.rb', line 14

def webmock!
  stub_index_request
  stub_search_request
  stub_show_request
end