Class: LHC::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/lhc/endpoint.rb

Overview

An endpoint is an url that leads to a backend resource. The url can also be an url-template (tools.ietf.org/html/rfc6570).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = nil) ⇒ Endpoint

Returns a new instance of Endpoint.



11
12
13
14
# File 'lib/lhc/endpoint.rb', line 11

def initialize(url, options = nil)
  self.url = url
  self.options = options
end

Instance Attribute Details

#optionsObject

Endpoint options are immutable



33
34
35
# File 'lib/lhc/endpoint.rb', line 33

def options
  @options.deep_dup
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/lhc/endpoint.rb', line 8

def url
  @url
end

Class Method Details

.match?(url, template) ⇒ Boolean

Compares a concrete url with a template Returns true if concrete url is covered by the template Example: +datastore/contracts/id == local.ch/contracts/1

Returns:

  • (Boolean)


94
95
96
# File 'lib/lhc/endpoint.rb', line 94

def self.match?(url, template)
  new(template).match?(url)
end

.placeholders(template) ⇒ Object

Returns all placeholders found in the url-template. They are alphabetically sorted.



100
101
102
# File 'lib/lhc/endpoint.rb', line 100

def self.placeholders(template)
  new(template).placeholders
end

.values_as_params(template, url) ⇒ Object

Extracts the values from url and creates params according to template



106
107
108
109
110
# File 'lib/lhc/endpoint.rb', line 106

def self.values_as_params(template, url)
  raise("#{url} does not match the template: #{template}") if !match?(url, template)

  new(template).values_as_params(url)
end

Instance Method Details

#compile(params) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lhc/endpoint.rb', line 20

def compile(params)
  context = LHC.config.placeholders.deep_dup
  context.merge!(params) if params.is_a?(Hash)
  expanded = uri.partial_expand(context)

  if expanded.variables.empty?
    expanded.pattern
  else
    raise("Compilation incomplete. Unable to find value for #{expanded.variables.join(', ')}.")
  end
end

#find_value(name, mapping) ⇒ Object

Checks if the name has a match in the current context



84
85
86
87
88
89
# File 'lib/lhc/endpoint.rb', line 84

def find_value(name, mapping)
  context = LHC.config.placeholders.deep_dup
  context.merge!(mapping)

  context[name]
end

#match?(url) ⇒ Boolean

Compares a concrete url with a template Returns true if concrete url is covered by the template Example: +datastore/contracts/id == local.ch/contracts/1

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lhc/endpoint.rb', line 56

def match?(url)
  return true if url == uri.pattern

  match_data = match_data(url)
  return false if match_data.nil?

  first = match_data.values.first
  last = match_data.values.last
  url_format = File.extname(url)

  # eliminate false positives in form:
  # http://host/feedbacks matches {+service}/{entry_id}/feedbacks (service: http:/, entry_id: host)
  first&.match(%{https?:/$}).nil? &&
    # eliminates false positives in form:
    # http://host/entries/123.json matches {+service}/entries/{id} (service: http://host, id: 123.json)
    (url_format.blank? || !last&.ends_with?(url_format))
end

#placeholdersObject

Returns all placeholders found in the url-template. They are alphabetically sorted.



49
50
51
# File 'lib/lhc/endpoint.rb', line 49

def placeholders
  uri.variables.sort.map(&:to_sym)
end

#remove_interpolated_params!(params) ⇒ Object

Removes keys from provided params hash when they are used for interpolation.



39
40
41
42
43
44
45
# File 'lib/lhc/endpoint.rb', line 39

def remove_interpolated_params!(params)
  params ||= {}
  removed = params.slice(*placeholders)
  params.except!(*placeholders)

  removed
end

#uriObject



16
17
18
# File 'lib/lhc/endpoint.rb', line 16

def uri
  @uri ||= Addressable::Template.new(url)
end

#values_as_params(url) ⇒ Object

Extracts the values from url and creates params according to template



76
77
78
79
80
81
# File 'lib/lhc/endpoint.rb', line 76

def values_as_params(url)
  match_data = match_data(url)
  return if match_data.nil?

  Hash[match_data.variables.map(&:to_sym).zip(match_data.values)]
end