Class: StubRequests::Service

Inherits:
Object
  • Object
show all
Includes:
Comparable, ArgumentValidation
Defined in:
lib/stub_requests/service.rb

Overview

Class Service provides details for a registered service

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArgumentValidation

#validate!

Constructor Details

#initialize(service_id, service_uri) ⇒ Service

Initializes a new instance of a Service

Parameters:

  • service_id (Symbol)

    the id of this service

  • service_uri (String)

    the base uri to reach the service

Since:

  • 0.1.0



35
36
37
38
39
40
41
42
# File 'lib/stub_requests/service.rb', line 35

def initialize(service_id, service_uri)
  validate! service_id,  is_a: Symbol
  validate! service_uri, is_a: String

  @id        = service_id
  @uri       = service_uri
  @endpoint_registry = EndpointRegistry.new
end

Instance Attribute Details

#endpoint_registryObject

Since:

  • 0.1.0



27
28
29
# File 'lib/stub_requests/service.rb', line 27

def endpoint_registry
  @endpoint_registry
end

#idObject

Since:

  • 0.1.0



19
20
21
# File 'lib/stub_requests/service.rb', line 19

def id
  @id
end

#uriObject

Since:

  • 0.1.0



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

def uri
  @uri
end

Instance Method Details

#<=>(other) ⇒ Object

Since:

  • 0.1.0



108
109
110
# File 'lib/stub_requests/service.rb', line 108

def <=>(other)
  id <=> other.id
end

#endpoints?true, false

Check if the endpoint registry has endpoints

Returns:

  • (true, false)

Since:

  • 0.1.0



65
66
67
# File 'lib/stub_requests/service.rb', line 65

def endpoints?
  endpoint_registry.any?
end

#get_endpoint(endpoint_id) ⇒ Endpoint?

Gets an endpoint from the #endpoint_registry collection

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint

Returns:

Since:

  • 0.1.0



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

def get_endpoint(endpoint_id)
  endpoint_registry.get(endpoint_id)
end

#get_endpoint!(endpoint_id) ⇒ Endpoint

Gets an endpoint from the #endpoint_registry collection

Parameters:

  • endpoint_id (Symbol)

    the id of the endpoint

Returns:

Raises:

Since:

  • 0.1.0



78
79
80
# File 'lib/stub_requests/service.rb', line 78

def get_endpoint!(endpoint_id)
  endpoint_registry.get!(endpoint_id)
end

#hashObject

Since:

  • 0.1.0



112
113
114
# File 'lib/stub_requests/service.rb', line 112

def hash
  [id, self.class].hash
end

#register_endpoint(endpoint_id, verb, uri_template, default_options = {}) ⇒ Endpoint

Registers a new endpoint or updates an existing one

:reek:LongParameterList { max_params: 5 }

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:

  • (Endpoint)

    either the new endpoint or the updated one

Since:

  • 0.1.0



56
57
58
# File 'lib/stub_requests/service.rb', line 56

def register_endpoint(endpoint_id, verb, uri_template, default_options = {})
  endpoint_registry.register(endpoint_id, verb, uri_template, default_options)
end

#to_sString

Returns a nicely formatted string with this service

Returns:

Since:

  • 0.1.0



98
99
100
101
102
103
104
105
106
# File 'lib/stub_requests/service.rb', line 98

def to_s
  [
    +"#<#{self.class}",
    +" id=#{id}",
    +" uri=#{uri}",
    +" endpoints=#{endpoint_registry.endpoints_string}",
    +">",
  ].join("")
end