Class: StubRequests::ServiceRegistry

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Singleton
Defined in:
lib/stub_requests/service_registry.rb

Overview

Class ServiceRegistry provides registration of services

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServiceRegistry

Returns a new instance of ServiceRegistry

Since:

  • 0.1.0



25
26
27
# File 'lib/stub_requests/service_registry.rb', line 25

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

Instance Attribute Details

#servicesObject

Since:

  • 0.1.0



23
24
25
# File 'lib/stub_requests/service_registry.rb', line 23

def services
  @services
end

Instance Method Details

#each { ... } ⇒ Concurrent::Map<Symbol, Service>

Required by Enumerable

Yields:

  • used by Enumerable

Returns:

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

    an map with services

Since:

  • 0.1.0



46
47
48
# File 'lib/stub_requests/service_registry.rb', line 46

def each(&block)
  services.each(&block)
end

#get_service(service_id) ⇒ Service

Fetches a service from the registry

Parameters:

  • service_id (Symbol)

    id of the service to remove

Returns:

Since:

  • 0.1.0



87
88
89
# File 'lib/stub_requests/service_registry.rb', line 87

def get_service(service_id)
  services[service_id]
end

#get_service!(service_id) ⇒ Endpoint

Fetches a service from the registry or raises StubRequests::ServiceNotFound

Parameters:

  • service_id (Symbol)

    the id of a service

Returns:

Raises:

Since:

  • 0.1.0



101
102
103
# File 'lib/stub_requests/service_registry.rb', line 101

def get_service!(service_id)
  get_service(service_id) || raise(ServiceNotFound, service_id)
end

#register_service(service_id, service_uri) ⇒ Service

Registers a service in the registry

Parameters:

  • service_id (Symbol)

    a symbolic id of the service

  • service_uri (String)

    a string with a base_uri to the service

Returns:

  • (Service)

    the service that was just registered

Since:

  • 0.1.0



59
60
61
62
63
64
65
# File 'lib/stub_requests/service_registry.rb', line 59

def register_service(service_id, service_uri)
  if (service = get_service(service_id))
    StubRequests.logger.warn("Service already registered #{service}")
    raise ServiceHaveEndpoints, service if service.endpoints?
  end
  services[service_id] = Service.new(service_id, service_uri)
end

#remove_service(service_id) ⇒ Object

Removes a service from the registry

Parameters:

  • service_id (Symbol)

    the service_id to remove

Raises:

Since:

  • 0.1.0



75
76
77
# File 'lib/stub_requests/service_registry.rb', line 75

def remove_service(service_id)
  services.delete(service_id) || raise(ServiceNotFound, service_id)
end

#resetObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the map with registered services

Since:

  • 0.1.0



34
35
36
# File 'lib/stub_requests/service_registry.rb', line 34

def reset
  services.clear
end