Class: StubRequests::EndpointRegistry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/stub_requests/endpoint_registry.rb

Overview

Class EndpointRegistry holds a collection of Endpoint

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEndpointRegistry

Returns a new instance of EndpointRegistry

Since:

  • 0.1.0



21
22
23
# File 'lib/stub_requests/endpoint_registry.rb', line 21

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

Instance Attribute Details

#endpointsObject

Since:

  • 0.1.0



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

Yields:

  • used by Enumerable

Returns:

  • (Concurrent::Map<Symbol, Service>)

    a map with endpoints

Since:

  • 0.1.0



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

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

#get(endpoint_id) ⇒ Endpoint

Fetches an endpoint from the collection

Parameters:

  • endpoint_id (<type>)

Returns:

Since:

  • 0.1.0



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

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint

Returns:

Raises:

Since:

  • 0.1.0



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 }

Parameters:

  • endpoint_id (Symbol)

    the id of this Endpoint

  • verb (Symbol)

    a HTTP verb

  • uri_template (String)

    the URI to reach the endpoint

  • default_options (optional, Hash<Symbol>) (defaults to: {})

    default options

Returns:

Since:

  • 0.1.0



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, default_options = {})
  endpoint =
    if (endpoint = get(endpoint_id))
      StubRequests.logger.warn("Endpoint already registered: #{endpoint}")
      endpoint.update(verb, uri_template, default_options)
    else
      Endpoint.new(endpoint_id, verb, uri_template, default_options)
    end

  endpoints[endpoint.id] = endpoint
  endpoint
end

#registered?(endpoint_id) ⇒ true, false

Check if an endpoint is registered

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint

Returns:

  • (true, false)

Since:

  • 0.1.0



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

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint, :file_service

Returns:

  • (Endpoint)

    the endpoint that was removed

Since:

  • 0.1.0



100
101
102
# File 'lib/stub_requests/endpoint_registry.rb', line 100

def remove(endpoint_id)
  endpoints.delete(endpoint_id)
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

#update(endpoint_id, verb, uri_template, default_options) ⇒ Endpoint

Updates an endpoint

:reek:LongParameterList { max_params: 4 }

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint

  • verb (Symbol)

    a HTTP verb

  • uri_template (String)

    how to reach the endpoint

  • default_options (optional, Hash<Symbol>)

Options Hash (default_options):

  • :request (optional, Hash<Symbol>)

    request options

  • :response (optional, Hash<Symbol>)

    options

  • :error (optional, Array, Exception, StandardError, String)

    to raise

  • :timeout (optional, TrueClass)

    raise a timeout error?

Returns:

  • (Endpoint)

    returns the updated endpoint

Raises:

Since:

  • 0.1.0



88
89
90
91
# File 'lib/stub_requests/endpoint_registry.rb', line 88

def update(endpoint_id, verb, uri_template, default_options)
  endpoint = get!(endpoint_id)
  endpoint.update(verb, uri_template, default_options)
end