Module: StubRequests::API
Overview
This module can either be used by its class methods or included in say RSpec
Module API abstraction to reduce the amount of WebMock.stub_request
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#__stub_endpoint(endpoint_id, route_params = {}) ⇒ Object
:nodoc:.
-
#define_stubs(service_id, receiver:) ⇒ void
Define stub methods for service in the receiver.
-
#print_stubs(service_id) ⇒ void
Print stub method definitions to manually add to a module or class.
-
#register_callback(service_id, endpoint_id, verb, callback) ⇒ void
Subscribe to notifications for a service endpoint.
-
#register_service(service_id, service_uri, &block) ⇒ Service
(also: #register_service2)
Register a service in the service registry.
-
#stub_endpoint(endpoint_id, route_params = {}, &callback) ⇒ WebMock::RequestStub
Stub a request to a registered service endpoint.
-
#unregister_callback(service_id, endpoint_id, verb) ⇒ void
Unsubscribe from notifications for a service endpoint.
Class Method Details
.included(base) ⇒ Object
:nodoc:
24 25 26 |
# File 'lib/stub_requests/api.rb', line 24 def self.included(base) base.send(:extend, self) end |
Instance Method Details
#__stub_endpoint(endpoint_id, route_params = {}) ⇒ Object
:nodoc:
83 84 85 86 87 88 89 |
# File 'lib/stub_requests/api.rb', line 83 def __stub_endpoint(endpoint_id, route_params = {}) endpoint = EndpointRegistry.instance.find!(endpoint_id) uri = URI::Builder.build(endpoint.uri, route_params) endpoint_stub = WebMock::Builder.build(endpoint.verb, uri) ::WebMock::StubRegistry.instance.register_request_stub(endpoint_stub) end |
#define_stubs(service_id, receiver:) ⇒ void
This method returns an undefined value.
Define stub methods for service in the receiver
102 103 104 |
# File 'lib/stub_requests/api.rb', line 102 def define_stubs(service_id, receiver:) DSL.new(service_id, receiver: receiver).define_stubs end |
#print_stubs(service_id) ⇒ void
This method returns an undefined value.
Print stub method definitions to manually add to a module or class
115 116 117 |
# File 'lib/stub_requests/api.rb', line 115 def print_stubs(service_id) DSL.new(service_id).print_stubs end |
#register_callback(service_id, endpoint_id, verb, callback) ⇒ void
This method returns an undefined value.
Subscribe to notifications for a service endpoint
129 130 131 |
# File 'lib/stub_requests/api.rb', line 129 def register_callback(service_id, endpoint_id, verb, callback) StubRequests::CallbackRegistry.instance.register(service_id, endpoint_id, verb, callback) end |
#register_service(service_id, service_uri, &block) ⇒ Service Also known as: register_service2
Register a service in the service registry
45 46 47 48 49 |
# File 'lib/stub_requests/api.rb', line 45 def register_service(service_id, service_uri, &block) service = ServiceRegistry.instance.register(service_id, service_uri) Docile.dsl_eval(service, &block) if block.present? service end |
#stub_endpoint(endpoint_id, route_params = {}, &callback) ⇒ WebMock::RequestStub
the kind of timeout error raised by webmock is depending on the HTTP client used
Stub a request to a registered service endpoint
73 74 75 76 77 78 79 80 |
# File 'lib/stub_requests/api.rb', line 73 def stub_endpoint(endpoint_id, route_params = {}, &callback) endpoint = EndpointRegistry.instance.find!(endpoint_id) uri = URI::Builder.build(endpoint.uri, route_params) webmock_stub = WebMock::Builder.build(endpoint.verb, uri, &callback) StubRegistry.instance.record(endpoint.id, webmock_stub) ::WebMock::StubRegistry.instance.register_request_stub(webmock_stub) end |
#unregister_callback(service_id, endpoint_id, verb) ⇒ void
This method returns an undefined value.
Unsubscribe from notifications for a service endpoint
141 142 143 |
# File 'lib/stub_requests/api.rb', line 141 def unregister_callback(service_id, endpoint_id, verb) StubRequests::CallbackRegistry.instance.unregister(service_id, endpoint_id, verb) end |