class StubRequests::StubRegistry

Class Registry maintains a registry of stubbed endpoints.

Also allows provides querying capabilities for said entities.

@author Mikael Henriksson <mikael@zoolutions.se> @since 0.1.2

Attributes

stubs[R]

@!attribute [r] stubs

@return [Concurrent::Array] a collection of {RequestStub}

Public Class Methods

new() click to toggle source

Initialize a new registry

# File lib/stub_requests/stub_registry.rb, line 40
def initialize
  reset
end

Public Instance Methods

find_by_webmock_stub(webmock_stub) click to toggle source

Finds a {RequestStub} amongst the endpoint stubs

@param [WebMock::RequestStub] webmock_stub a stubbed webmock response

@return [RequestStub] the request_stubbed matching the request stub

# File lib/stub_requests/stub_registry.rb, line 93
def find_by_webmock_stub(webmock_stub)
  find { |stub| stub.webmock_stub == webmock_stub }
end
mark_as_responded(webmock_stub) click to toggle source

Mark a {RequestStub} as having responded

@note Called when webmock responds successfully

@param [WebMock::RequestStub] webmock_stub the stubbed webmock request

@return [void]

# File lib/stub_requests/stub_registry.rb, line 77
def mark_as_responded(webmock_stub)
  return unless (request_stub = find_by_webmock_stub(webmock_stub))

  request_stub.mark_as_responded
  CallbackRegistry.instance.invoke_callbacks(request_stub)
  request_stub
end
record(endpoint_id, webmock_stub) click to toggle source

Records a WebMock::RequestStub as stubbed

@param [WebMock::RequestStub] webmock_stub <description>

@return [RequestStub]

# File lib/stub_requests/stub_registry.rb, line 60
def record(endpoint_id, webmock_stub)
  return unless StubRequests.config.record_stubs?

  request_stub = RequestStub.new(endpoint_id, webmock_stub)
  concat([request_stub])
  request_stub
end
reset() click to toggle source

Resets the map with stubbed endpoints

@api private

# File lib/stub_requests/stub_registry.rb, line 49
def reset
  @stubs = Concurrent::Array.new
end