Class: ProtoPharm::StubRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/proto_pharm/stub_registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStubRegistry

Returns a new instance of StubRegistry.



7
8
9
# File 'lib/proto_pharm/stub_registry.rb', line 7

def initialize
  @request_stubs = {}
end

Instance Attribute Details

#request_stubsObject (readonly)

Returns the value of attribute request_stubs.



5
6
7
# File 'lib/proto_pharm/stub_registry.rb', line 5

def request_stubs
  @request_stubs
end

Instance Method Details

#all_requests_matching(path, request = nil) ⇒ Array<ProtoPharm::RequestStub>

Returns Array of all matching request stubs, if any. See RequestPattern#match? for matching logic.

Parameters:

  • path (String)
  • request (Object) (defaults to: nil)

    Optional; specify a request object to match against. Default: nil.

Returns:



25
26
27
# File 'lib/proto_pharm/stub_registry.rb', line 25

def all_requests_matching(path, request = nil)
  request_stubs[path]&.select { |stub| stub.match?(path, request) } || []
end

#find_request_matching(path, request) ⇒ ProtoPharm::RequestStub

Returns RequestStub matching the given path/request, if found.

Parameters:

  • path (String)
  • request (Object)

Returns:



32
33
34
# File 'lib/proto_pharm/stub_registry.rb', line 32

def find_request_matching(path, request)
  request_stubs[path]&.find { |stub| stub.match?(path, request) }
end

#register_request_stub(stub) ⇒ Object

Parameters:



16
17
18
19
20
# File 'lib/proto_pharm/stub_registry.rb', line 16

def register_request_stub(stub)
  request_stubs[stub.path] ||= []
  request_stubs[stub.path].unshift(stub)
  stub
end

#reset!Object



11
12
13
# File 'lib/proto_pharm/stub_registry.rb', line 11

def reset!
  @request_stubs = {}
end