Module: LHS::Record::Endpoints::ClassMethods

Defined in:
lib/lhs/concerns/record/endpoints.rb

Instance Method Summary collapse

Instance Method Details

#compute_url(params, url = nil) ⇒ Object



66
67
68
69
# File 'lib/lhs/concerns/record/endpoints.rb', line 66

def compute_url(params, url = nil)
  find_endpoint(params, url)
    .compile(params)
end

#compute_url!(params) ⇒ Object

Computes the url from params by identifiying endpoint and compiles it if necessary.



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

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

#endpoint(url, options = nil) ⇒ Object

Adds the endpoint to the list of endpoints.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lhs/concerns/record/endpoints.rb', line 16

def endpoint(url, options = nil)
  class_attribute :endpoints unless defined? endpoints
  self.endpoints ||= []
  self.endpoints = endpoints.clone
  validates_deprecation_check!(options)
  endpoint = LHC::Endpoint.new(url, options)
  sanity_check(endpoint)
  endpoints.push(endpoint)
  LHS::Record::Endpoints.all ||= {}
  LHS::Record::Endpoints.all[url] = self
end

#find_endpoint(params = {}, url = nil) ⇒ 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.



39
40
41
42
43
44
45
# File 'lib/lhs/concerns/record/endpoints.rb', line 39

def find_endpoint(params = {}, url = nil)
  endpoint = find_best_endpoint(params) if params && params.keys.count > 0
  endpoint ||= find_endpoint_by_url(url) if url.present?
  endpoint ||= LHC::Endpoint.new(url) if url.present?
  endpoint ||= find_base_endpoint
  endpoint
end

#for_url(url) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/lhs/concerns/record/endpoints.rb', line 28

def for_url(url)
  return unless url
  _template, record = LHS::Record::Endpoints.all.detect do |template, _|
    LHC::Endpoint.match?(url, template)
  end
  record
end

#sanity_check(new_endpoint) ⇒ Object

Prevent ambiguous endpoints



48
49
50
51
52
53
54
55
# File 'lib/lhs/concerns/record/endpoints.rb', line 48

def sanity_check(new_endpoint)
  endpoints.each do |existing_endpoint|
    invalid = existing_endpoint.placeholders.sort == new_endpoint.placeholders.sort &&
      existing_endpoint.url != new_endpoint.url
    next unless invalid
    raise "Ambiguous endpoints! Cannot differentiate between #{existing_endpoint.url} and #{new_endpoint.url}"
  end
end