Class: StubRequests::EndpointRegistry
- Extended by:
- Forwardable
- Includes:
- Enumerable, Singleton
- Defined in:
- lib/stub_requests/endpoint_registry.rb
Overview
Class Endpoints manages a collection of endpoints
Instance Attribute Summary collapse
Class Method Summary collapse
-
.[](service_id) ⇒ Array<Endpoint>
Return all endpoints for a service.
Instance Method Summary collapse
-
#endpoints_string ⇒ <type>
Returns a nicely formatted string with an array of endpoints.
- #find(endpoint_id) ⇒ Endpoint?
-
#find!(endpoint_id) ⇒ Endpoint
Fetches an endpoint from the collection or raises an error.
-
#initialize ⇒ EndpointRegistry
constructor
Initialize is used by Singleton.
-
#register(endpoint) ⇒ Endpoint
Registers an endpoint in the collection.
-
#reset ⇒ Object
Resets the endpoints array (used for testing).
-
#size ⇒ Integer
(also: #count)
The size of the endpoints array.
-
#suggestions(endpoint_id) ⇒ Array<Symbol>
Returns an array of potential alternatives.
-
#to_s ⇒ String
Returns a descriptive string with all endpoints in the collection.
Constructor Details
#initialize ⇒ EndpointRegistry
Initialize is used by Singleton
49 50 51 |
# File 'lib/stub_requests/endpoint_registry.rb', line 49 def initialize reset end |
Instance Attribute Details
#endpoints ⇒ Object
43 44 45 |
# File 'lib/stub_requests/endpoint_registry.rb', line 43 def endpoints @endpoints end |
Class Method Details
Instance Method Details
#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 |
#find(endpoint_id) ⇒ Endpoint?
111 112 113 114 |
# File 'lib/stub_requests/endpoint_registry.rb', line 111 def find(endpoint_id) endpoint_id = endpoint_id.id if endpoint_id.is_a?(Endpoint) self[endpoint_id] end |
#find!(endpoint_id) ⇒ Endpoint
Fetches an endpoint from the collection or raises an error
96 97 98 99 100 101 |
# File 'lib/stub_requests/endpoint_registry.rb', line 96 def find!(endpoint_id) endpoint = find(endpoint_id) return endpoint if endpoint raise EndpointNotFound, id: endpoint_id, suggestions: suggestions(endpoint_id) end |
#register(endpoint) ⇒ Endpoint
Registers an endpoint in the collection
79 80 81 82 83 84 |
# File 'lib/stub_requests/endpoint_registry.rb', line 79 def register(endpoint) StubRequests.logger.warn("Endpoint already registered: #{endpoint}") if find(endpoint) self[endpoint.id] = endpoint endpoint end |
#reset ⇒ Object
Resets the endpoints array (used for testing)
57 58 59 |
# File 'lib/stub_requests/endpoint_registry.rb', line 57 def reset @endpoints = Concurrent::Map.new end |
#size ⇒ Integer Also known as: count
The size of the endpoints array
67 68 69 |
# File 'lib/stub_requests/endpoint_registry.rb', line 67 def size keys.size end |
#suggestions(endpoint_id) ⇒ Array<Symbol>
Returns an array of potential alternatives
123 124 125 |
# File 'lib/stub_requests/endpoint_registry.rb', line 123 def suggestions(endpoint_id) Utils::Fuzzy.match(endpoint_id, keys) 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 |