Class: StubRequests::ServiceRegistry

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

Overview

Class Registry provides registration of services

Author:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServiceRegistry

Initialize a new instance (used by Singleton)

Since:

  • 0.1.0



38
39
40
# File 'lib/stub_requests/service_registry.rb', line 38

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

Instance Attribute Details

#servicesObject

Since:

  • 0.1.0



32
33
34
# File 'lib/stub_requests/service_registry.rb', line 32

def services
  @services
end

Instance Method Details

#find(service_id) ⇒ Service

Fetches a service from the registry

Parameters:

  • service_id (Symbol)

    id of the service to remove

Returns:

Since:

  • 0.1.0



99
100
101
# File 'lib/stub_requests/service_registry.rb', line 99

def find(service_id)
  self[service_id]
end

#find!(service_id) ⇒ Service

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



113
114
115
# File 'lib/stub_requests/service_registry.rb', line 113

def find!(service_id)
  self[service_id] || raise(ServiceNotFound, service_id)
end

#register(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



71
72
73
74
75
76
77
# File 'lib/stub_requests/service_registry.rb', line 71

def register(service_id, service_uri)
  service = Service.new(service_id, service_uri)
  StubRequests.logger.warn("Service already registered #{service}") if self[service_id]

  self[service_id] = service
  service
end

#remove(service_id) ⇒ Object

Removes a service from the registry

Parameters:

  • service_id (Symbol)

    the service_id to remove

Raises:

Since:

  • 0.1.0



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

def remove(service_id)
  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



58
59
60
# File 'lib/stub_requests/service_registry.rb', line 58

def reset
  services.clear
end

#sizeInteger Also known as: count

Returns the size of the registry

Returns:

  • (Integer)

Since:

  • 0.1.0



48
49
50
# File 'lib/stub_requests/service_registry.rb', line 48

def size
  keys.size
end