class StubRequests::EndpointRegistry
Class
Endpoints manages a collection of endpoints
@author Mikael Henriksson <mikael@zoolutions.se>
Attributes
@!attribute [rw] endpoints
@return [Concurrent::Map<Symbol, Endpoint>] a map with endpoints
Public Class Methods
Return all endpoints for a service
@param [Symbol] service_id the id of a registered service
@return [Array<Endpoint>] the endpoints for the service
# File lib/stub_requests/endpoint_registry.rb, line 36 def self.[](service_id) instance.values.select { |ep| ep.service_id == service_id } end
Initialize is used by Singleton
# File lib/stub_requests/endpoint_registry.rb, line 49 def initialize reset end
Public Instance Methods
Returns a nicely formatted string with an array of endpoints
@return [<type>] <description>
# File lib/stub_requests/endpoint_registry.rb, line 147 def endpoints_string "[#{endpoints_as_string}]" end
@param [Symbol] endpoint_id the id of the endpoint
@return [Endpoint, nil]
# 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
Fetches an endpoint from the collection or raises an error
@param [Symbol] endpoint_id the id of the endpoint
@raise [EndpointNotFound] when an endpoint couldn't be found
@return [Endpoint]
# 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
Registers an endpoint in the collection
@param [Endpoint] endpoint the endpoint to register
@return [Endpoint]
# 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
Resets the endpoints array (used for testing)
# File lib/stub_requests/endpoint_registry.rb, line 57 def reset @endpoints = Concurrent::Map.new end
The size of the endpoints array
@return [Integer]
# File lib/stub_requests/endpoint_registry.rb, line 67 def size keys.size end
Returns an array of potential alternatives
@param [Symbol] endpoint_id the id of an endpoint
@return [Array<Symbol>] an array of endpoint_id's
# File lib/stub_requests/endpoint_registry.rb, line 123 def suggestions(endpoint_id) Utils::Fuzzy.match(endpoint_id, keys) end
Returns a descriptive string with all endpoints in the collection
@return [String]
# File lib/stub_requests/endpoint_registry.rb, line 133 def to_s [ +"#<#{self.class} endpoints=", +endpoints_string, +">", ].join("") end
Private Instance Methods
# File lib/stub_requests/endpoint_registry.rb, line 153 def endpoints_as_string endpoints.values.map(&:to_s).join(",") if endpoints.size.positive? end