Class: ServiceContractWebmock::ServiceMock
- Inherits:
-
Object
- Object
- ServiceContractWebmock::ServiceMock
- Defined in:
- lib/service_contract_webmock/service_mock.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#contract ⇒ Object
readonly
Returns the value of attribute contract.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
-
#initialize(name, resources, contract, base_url) ⇒ ServiceMock
constructor
A new instance of ServiceMock.
- #stub_index_request ⇒ Object
- #stub_request(method, path) ⇒ Object
- #stub_search_request ⇒ Object
- #stub_show_request ⇒ Object
- #webmock! {|_self| ... } ⇒ Object
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_url ⇒ Object (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 |
#contract ⇒ Object (readonly)
Returns the value of attribute contract.
5 6 7 |
# File 'lib/service_contract_webmock/service_mock.rb', line 5 def contract @contract end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/service_contract_webmock/service_mock.rb', line 5 def name @name end |
#resources ⇒ Object (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_request ⇒ Object
25 26 27 28 29 |
# File 'lib/service_contract_webmock/service_mock.rb', line 25 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
21 22 23 |
# File 'lib/service_contract_webmock/service_mock.rb', line 21 def stub_request(method, path) WebMock.stub_request(method, %r{#{base_url}/#{path}}) end |
#stub_search_request ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/service_contract_webmock/service_mock.rb', line 54 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_request ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/service_contract_webmock/service_mock.rb', line 31 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! {|_self| ... } ⇒ Object
14 15 16 17 18 19 |
# File 'lib/service_contract_webmock/service_mock.rb', line 14 def webmock! stub_index_request stub_search_request stub_show_request yield self if block_given? end |