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

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:

Since:

  • 0.1.0



22
23
24
# File 'lib/stub_requests/api.rb', line 22

def self.included(base)
  base.send(:extend, self)
end

Instance Method Details

#register_service(service_id, service_uri, &block) ⇒ Service

Register a service in the service registry

:reek:UtilityFunction

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



34
35
36
37
38
# File 'lib/stub_requests/api.rb', line 34

def register_service(service_id, service_uri, &block)
  service = ServiceRegistry.instance.register_service(service_id, service_uri)
  Docile.dsl_eval(service.endpoint_registry, &block) if block.present?
  service
end

#stub_endpoint(service_id, endpoint_id, uri_replacements = {}, options = {}, &callback) ⇒ WebMock::RequestStub

Stub a request to a registered service endpoint

:reek:UtilityFunction :reek:LongParameterList { max_params: 5 }

Examples:

Stub a request to a registered service endpoint

register_stub(
  :google_api,
  :get_map_location,
  { request: { headers: { "Accept" => "application/json" }}},
  { response: { body: { id: "abyasdjasd", status: "successful" }}}
)

Parameters:

  • service_id (Symbol)

    the id of a registered service

  • endpoint_id (Symbol)

    the id of a registered endpoint

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

    a list of uri replacements

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

Options Hash (options):

  • :request (optional, Hash<Symbol>)

    webmock request options

  • :response (optional, Hash<Symbol>)

    webmock response options

  • :error (optional, Array, Exception, StandardError, String)

    webmock error to raise

  • :timeout (optional, TrueClass)

    set to truthy to raise timeeout with webmock

Returns:

  • (WebMock::RequestStub)

    a mocked request

See Also:

  • #stub_http_request

Since:

  • 0.1.0



65
66
67
68
69
70
71
# File 'lib/stub_requests/api.rb', line 65

def stub_endpoint(service_id, endpoint_id, uri_replacements = {}, options = {}, &callback)
  service  = ServiceRegistry.instance.get_service!(service_id)
  endpoint = service.get_endpoint!(endpoint_id)
  uri      = URI::Builder.build(service.uri, endpoint.uri_template, uri_replacements)

  StubRequests::WebMockBuilder.build(endpoint.verb, uri, options, &callback)
end