Class: StubRequests::EndpointRegistry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Singleton
Defined in:
lib/stub_requests/endpoint_registry.rb

Overview

Class Endpoints manages a collection of endpoints

Author:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEndpointRegistry

Initialize is used by Singleton

Since:

  • 0.1.0



49
50
51
# File 'lib/stub_requests/endpoint_registry.rb', line 49

def initialize
  reset
end

Instance Attribute Details

#endpointsObject

Since:

  • 0.1.0



43
44
45
# File 'lib/stub_requests/endpoint_registry.rb', line 43

def endpoints
  @endpoints
end

Class Method Details

.[](service_id) ⇒ Array<Endpoint>

Return all endpoints for a service

Parameters:

  • service_id (Symbol)

    the id of a registered service

Returns:

Since:

  • 0.1.0



36
37
38
# File 'lib/stub_requests/endpoint_registry.rb', line 36

def self.[](service_id)
  instance.values.select { |ep| ep.service_id == service_id }
end

Instance Method Details

#endpoints_string<type>

Returns a nicely formatted string with an array of endpoints

Returns:

  • (<type>)

Since:

  • 0.1.0



147
148
149
# File 'lib/stub_requests/endpoint_registry.rb', line 147

def endpoints_string
  "[#{endpoints_as_string}]"
end

#find(endpoint_id) ⇒ Endpoint?

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint

Returns:

Since:

  • 0.1.0



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

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint

Returns:

Raises:

Since:

  • 0.1.0



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

Parameters:

  • endpoint (Endpoint)

    the endpoint to register

Returns:

Since:

  • 0.1.0



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

#resetObject

Resets the endpoints array (used for testing)

Since:

  • 0.1.0



57
58
59
# File 'lib/stub_requests/endpoint_registry.rb', line 57

def reset
  @endpoints = Concurrent::Map.new
end

#sizeInteger Also known as: count

The size of the endpoints array

Returns:

  • (Integer)

Since:

  • 0.1.0



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

Parameters:

  • endpoint_id (Symbol)

    the id of an endpoint

Returns:

Since:

  • 0.1.0



123
124
125
# File 'lib/stub_requests/endpoint_registry.rb', line 123

def suggestions(endpoint_id)
  Utils::Fuzzy.match(endpoint_id, keys)
end

#to_sString

Returns a descriptive string with all endpoints in the collection

Returns:

Since:

  • 0.1.0



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