class StubRequests::EndpointRegistry

Class Endpoints manages a collection of endpoints

@author Mikael Henriksson <mikael@zoolutions.se>

Attributes

endpoints[R]

@!attribute [rw] endpoints

@return [Concurrent::Map<Symbol, Endpoint>] a map with endpoints

Public Class Methods

[](service_id) click to toggle source

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
new() click to toggle source

Initialize is used by Singleton

# File lib/stub_requests/endpoint_registry.rb, line 49
def initialize
  reset
end

Public Instance Methods

count()
Alias for: size
endpoints_string() click to toggle source

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
find(endpoint_id) click to toggle source

@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
find!(endpoint_id) click to toggle source

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
register(endpoint) click to toggle source

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
reset() click to toggle source

Resets the endpoints array (used for testing)

# File lib/stub_requests/endpoint_registry.rb, line 57
def reset
  @endpoints = Concurrent::Map.new
end
size() click to toggle source

The size of the endpoints array

@return [Integer]

# File lib/stub_requests/endpoint_registry.rb, line 67
def size
  keys.size
end
Also aliased as: count
suggestions(endpoint_id) click to toggle source

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
to_s() click to toggle source

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

endpoints_as_string() click to toggle source
# File lib/stub_requests/endpoint_registry.rb, line 153
def endpoints_as_string
  endpoints.values.map(&:to_s).join(",") if endpoints.size.positive?
end