Module: StubRequests::API

Extended by:
API
Included in:
StubRequests, API
Defined in:
lib/stub_requests/api.rb

Overview

Note:

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

Author:

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:

Since:

  • 0.1.0



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:

Since:

  • 0.1.0



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

Parameters:

  • service_id (Symbol)

    the id of a registered service

  • receiver (Module)

    the receiver of the stub methods

See Also:

Since:

  • 0.1.0



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

This method returns an undefined value.

Print stub method definitions to manually add to a module or class

Parameters:

  • service_id (Symbol)

    the id of a registered service

See Also:

Since:

  • 0.1.0



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

Parameters:

  • service_id (Symbol)

    the id of a service

  • endpoint_id (Symbol)

    the id of an endpoint

  • verb (Symbol)

    an HTTP verb/method

  • callback (Proc)

    a Proc to call when receiving response

Since:

  • 0.1.0



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

Examples:

Register a service with endpoints

register_service(:documents, "https://company.com/api/v1") do
  get    "documents/:id", as: :show
  get    "documents",     as: :index
  post   "documents",     as: :create
  patch  "documents/:id", as: :update
  delete "documents/:id", as: :destroy
end

Parameters:

  • service_id (Symbol)

    a descriptive id for the service

  • service_uri (Symbol)

    the uri used to call the service

Returns:

  • (Service)

    a new service or a previously registered service

Since:

  • 0.1.0



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

Note:

the kind of timeout error raised by webmock is depending on the HTTP client used

Stub a request to a registered service endpoint

Examples:

Stub a request to a registered service endpoint

stub_endpoint(:get_map_location, id: 1)
 .to_return(body: "No content", status: 204)

Stub a request to a registered service endpoint using block

stub_endpoint(:documents_index) do
  with(headers: { "Accept" => "application/json" }}})
  to_return(body: "No content", status: 204)
end

Parameters:

  • endpoint_id (Symbol)

    the id of a registered endpoint

  • route_params (Hash<Symbol>) (defaults to: {})

    a map with route parameters

Returns:

  • (WebMock::RequestStub)

    a mocked request

Since:

  • 0.1.0



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

Parameters:

  • service_id (Symbol)

    the id of a service

  • endpoint_id (Symbol)

    the id of an endpoint

Since:

  • 0.1.0



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