Module: LHS::Service::Endpoints

Extended by:
ActiveSupport::Concern
Included in:
LHS::Service
Defined in:
lib/lhs/concerns/service/endpoints.rb

Overview

An endpoint is an url that leads to a backend resource. A service can contain multiple endpoints. The endpoint that is used to request data is choosen based on the provided parameters.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointsObject

Returns the value of attribute endpoints.



12
13
14
# File 'lib/lhs/concerns/service/endpoints.rb', line 12

def endpoints
  @endpoints
end

Instance Method Details

#compute_url!(params) ⇒ Object

Computes the url from params by identifiying endpoint and compiles it if necessary. Id in params is threaded in a special way.



58
59
60
61
62
63
# File 'lib/lhs/concerns/service/endpoints.rb', line 58

def compute_url!(params)
  endpoint = find_endpoint(params)
  url = endpoint.compile(params)
  endpoint.remove_interpolated_params!(params)
  url
end

#find_endpoint(params = {}) ⇒ Object

Find an endpoint based on the provided parameters. If no parameters are provided it finds the base endpoint otherwise it finds the endpoint that matches the parameters best.



43
44
45
46
47
# File 'lib/lhs/concerns/service/endpoints.rb', line 43

def find_endpoint(params = {})
  endpoint = find_best_endpoint(params) if params && params.keys.count > 0
  endpoint ||= find_base_endpoint
  endpoint
end

#initializeObject



35
36
37
38
# File 'lib/lhs/concerns/service/endpoints.rb', line 35

def initialize
  self.endpoints = []
  super
end

#sanity_check(endpoint) ⇒ Object

Prevent clashing endpoints.



50
51
52
53
# File 'lib/lhs/concerns/service/endpoints.rb', line 50

def sanity_check(endpoint)
  placeholders = endpoint.placeholders
  fail 'Clashing endpoints.' if endpoints.any? { |e| e.placeholders.sort == placeholders.sort }
end