Class: StubRequests::CallbackRegistry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Singleton
Defined in:
lib/stub_requests/callback_registry.rb

Overview

Class Registry handles callbacks to webmock requests

Author:

Since:

  • 0.1.3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallbackRegistry

Used by Singleton

Since:

  • 0.1.3



39
40
41
# File 'lib/stub_requests/callback_registry.rb', line 39

def initialize
  @callbacks = Concurrent::Array.new
end

Instance Attribute Details

#callbacksObject (readonly)

Since:

  • 0.1.3



33
34
35
# File 'lib/stub_requests/callback_registry.rb', line 33

def callbacks
  @callbacks
end

Instance Method Details

#invoke_callbacks(request_stub) ⇒ void

This method returns an undefined value.

Notifies subscribers that a request was made

Parameters:

Since:

  • 0.1.3



95
96
97
98
99
# File 'lib/stub_requests/callback_registry.rb', line 95

def invoke_callbacks(request_stub)
  return unless (callback = find_by(request_stub.service_id, request_stub.endpoint_id, request_stub.verb))

  callback.call(request_stub)
end

#register(service_id, endpoint_id, verb, block) ⇒ Callback

Register to a service endpoint call

Parameters:

  • service_id (Symbol)

    the id of a service

  • endpoint_id (Symbol)

    the id of an endpoint

  • verb (optional, Symbol)

    the HTTP verb to subscribe to

  • block (proc)

    the callback to use for when.a request was made

Returns:

Since:

  • 0.1.3



63
64
65
66
67
68
69
70
# File 'lib/stub_requests/callback_registry.rb', line 63

def register(service_id, endpoint_id, verb, block)
  callback = find_by(service_id, endpoint_id, verb)
  return callback if callback

  callback = Callback.new(service_id, endpoint_id, verb, block)
  callbacks.push(callback)
  callback
end

#resetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the map with registered services

Since:

  • 0.1.3



48
49
50
# File 'lib/stub_requests/callback_registry.rb', line 48

def reset
  callbacks.clear
end

#unregister(service_id, endpoint_id, verb) ⇒ Callback

Unregister to a service endpoint call

Parameters:

  • service_id (Symbol)

    the id of a service

  • endpoint_id (Symbol)

    the id of an endpoint

  • verb (optional, Symbol)

    the HTTP verb to subscribe to

Returns:

Since:

  • 0.1.3



82
83
84
85
86
# File 'lib/stub_requests/callback_registry.rb', line 82

def unregister(service_id, endpoint_id, verb)
  return unless (callback = find_by(service_id, endpoint_id, verb))

  callbacks.delete(callback)
end