Class: StubRequests::EndpointRegistry
- Includes:
- Enumerable
- Defined in:
- lib/stub_requests/endpoint_registry.rb
Overview
Class EndpointRegistry holds a collection of Endpoint
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#each { ... } ⇒ Concurrent::Map<Symbol, Service>
Required by Enumerable.
-
#endpoints_string ⇒ <type>
Returns a nicely formatted string with an array of endpoints.
-
#get(endpoint_id) ⇒ Endpoint
Fetches an endpoint from the collection.
-
#get!(endpoint_id) ⇒ Endpoint?
Fetches an endpoint from the collection or raises an error.
-
#initialize ⇒ EndpointRegistry
constructor
A new instance of EndpointRegistry.
-
#register(endpoint_id, verb, uri_template, default_options = {}) ⇒ Endpoint
Registers an endpoint in the collection.
-
#registered?(endpoint_id) ⇒ true, false
Check if an endpoint is registered.
-
#remove(endpoint_id) ⇒ Endpoint
Removes an endpoint from the collection.
-
#to_s ⇒ String
Returns a descriptive string with all endpoints in the collection.
-
#update(endpoint_id, verb, uri_template, default_options) ⇒ Endpoint
Updates an endpoint.
Constructor Details
#initialize ⇒ EndpointRegistry
Returns a new instance of EndpointRegistry
21 22 23 |
# File 'lib/stub_requests/endpoint_registry.rb', line 21 def initialize @endpoints = Concurrent::Map.new end |
Instance Attribute Details
#endpoints ⇒ Object
19 20 21 |
# File 'lib/stub_requests/endpoint_registry.rb', line 19 def endpoints @endpoints end |
Instance Method Details
#each { ... } ⇒ Concurrent::Map<Symbol, Service>
Required by Enumerable
32 33 34 |
# File 'lib/stub_requests/endpoint_registry.rb', line 32 def each(&block) endpoints.each(&block) end |
#endpoints_string ⇒ <type>
Returns a nicely formatted string with an array of endpoints
147 148 149 |
# File 'lib/stub_requests/endpoint_registry.rb', line 147 def endpoints_string "[#{endpoints_as_string}]" end |
#get(endpoint_id) ⇒ Endpoint
Fetches an endpoint from the collection
111 112 113 |
# File 'lib/stub_requests/endpoint_registry.rb', line 111 def get(endpoint_id) endpoints[endpoint_id] end |
#get!(endpoint_id) ⇒ Endpoint?
Fetches an endpoint from the collection or raises an error
124 125 126 |
# File 'lib/stub_requests/endpoint_registry.rb', line 124 def get!(endpoint_id) get(endpoint_id) || raise(EndpointNotFound, "Couldn't find an endpoint with id=:#{endpoint_id}") end |
#register(endpoint_id, verb, uri_template, default_options = {}) ⇒ Endpoint
Registers an endpoint in the collection
:reek:LongParameterList { max_params: 4 }
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/stub_requests/endpoint_registry.rb', line 47 def register(endpoint_id, verb, uri_template, = {}) endpoint = if (endpoint = get(endpoint_id)) StubRequests.logger.warn("Endpoint already registered: #{endpoint}") endpoint.update(verb, uri_template, ) else Endpoint.new(endpoint_id, verb, uri_template, ) end endpoints[endpoint.id] = endpoint endpoint end |
#registered?(endpoint_id) ⇒ true, false
Check if an endpoint is registered
67 68 69 |
# File 'lib/stub_requests/endpoint_registry.rb', line 67 def registered?(endpoint_id) endpoints[endpoint_id].present? end |
#remove(endpoint_id) ⇒ Endpoint
Removes an endpoint from the collection
100 101 102 |
# File 'lib/stub_requests/endpoint_registry.rb', line 100 def remove(endpoint_id) endpoints.delete(endpoint_id) end |
#to_s ⇒ String
Returns a descriptive string with all endpoints in the collection
133 134 135 136 137 138 139 |
# File 'lib/stub_requests/endpoint_registry.rb', line 133 def to_s [ +"#<#{self.class} endpoints=", +endpoints_string, +">", ].join("") end |
#update(endpoint_id, verb, uri_template, default_options) ⇒ Endpoint
Updates an endpoint
:reek:LongParameterList { max_params: 4 }
88 89 90 91 |
# File 'lib/stub_requests/endpoint_registry.rb', line 88 def update(endpoint_id, verb, uri_template, ) endpoint = get!(endpoint_id) endpoint.update(verb, uri_template, ) end |