Class: StubRequests::StubRegistry

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

Overview

Class Registry maintains a registry of stubbed endpoints. Also allows provides querying capabilities for said entities.

Author:

Since:

  • 0.1.2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStubRegistry

Initialize a new registry

Since:

  • 0.1.2



40
41
42
# File 'lib/stub_requests/stub_registry.rb', line 40

def initialize
  reset
end

Instance Attribute Details

#stubsObject (readonly)

Since:

  • 0.1.2



34
35
36
# File 'lib/stub_requests/stub_registry.rb', line 34

def stubs
  @stubs
end

Instance Method Details

#find_by_webmock_stub(webmock_stub) ⇒ RequestStub

Finds a RequestStub amongst the endpoint stubs

Parameters:

  • webmock_stub (WebMock::RequestStub)

    a stubbed webmock response

Returns:

  • (RequestStub)

    the request_stubbed matching the request stub

Since:

  • 0.1.2



93
94
95
# 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) ⇒ void

Note:

Called when webmock responds successfully

This method returns an undefined value.

Mark a RequestStub as having responded

Parameters:

  • webmock_stub (WebMock::RequestStub)

    the stubbed webmock request

Since:

  • 0.1.2



77
78
79
80
81
82
83
# 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) ⇒ RequestStub

Records a WebMock::RequestStub as stubbed

Parameters:

  • webmock_stub (WebMock::RequestStub)

Returns:

Since:

  • 0.1.2



60
61
62
63
64
65
66
# 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

#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 stubbed endpoints

Since:

  • 0.1.2



49
50
51
# File 'lib/stub_requests/stub_registry.rb', line 49

def reset
  @stubs = Concurrent::Array.new
end