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.

Constant Summary collapse

PLACEHOLDER =
/\:[A-Z,a-z,_,-]+/

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.



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

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

Instance Attribute Details

#optionsObject

Endpoint options are immutable



26
27
28
# File 'lib/lhc/endpoint.rb', line 26

def options
  @options
end

#urlObject

Returns the value of attribute url.



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

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

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# File 'lib/lhc/endpoint.rb', line 61

def self.match?(url, template)
  regexp = template
  LHC::Endpoint.placeholders(template).each do |placeholder|
    regexp = regexp.gsub(placeholder, '.*')
  end
  regexp = regexp.gsub('/', '(?<!/)/(?!/)')
  url.match(Regexp.new("^#{regexp}$"))
end

.placeholders(template) ⇒ Object

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



72
73
74
# File 'lib/lhc/endpoint.rb', line 72

def self.placeholders(template)
  template.scan(PLACEHOLDER).sort
end

.values_as_params(template, url) ⇒ Object

Extracts the values from url and creates params according to template



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/lhc/endpoint.rb', line 78

def self.values_as_params(template, url)
  params = {}
  regexp = template
  LHC::Endpoint.placeholders(template).each do |placeholder|
    name = placeholder.gsub(":", '')
    regexp = regexp.gsub(placeholder, "(?<#{name}>.*)")
  end
  matchdata = url.match(Regexp.new("^#{regexp}$"))
  LHC::Endpoint.placeholders(template).each do |placeholder|
    name = placeholder.gsub(':', '')
    params[name.to_sym] = matchdata[name]
  end
  params
end

Instance Method Details

#compile(params) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/lhc/endpoint.rb', line 14

def compile(params)
  url.gsub(PLACEHOLDER) do |match|
    replacement = if params.is_a? Proc
      params.call(match)
    else
      find_value(match, params)
    end
    replacement || fail("Compilation incomplete. Unable to find value for #{match.gsub(':', '')}.")
  end
end

#find_value(match, params) ⇒ Object

Find a value for a placeholder either in the configuration or in the provided params.



53
54
55
56
57
# File 'lib/lhc/endpoint.rb', line 53

def find_value(match, params)
  params ||= {}
  match = match.gsub(/^\:/, '').to_sym
  params[match] || LHC.config.placeholders[match]
end

#placeholdersObject

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



47
48
49
# File 'lib/lhc/endpoint.rb', line 47

def placeholders
  LHC::Endpoint.placeholders(url)
end

#remove_interpolated_params!(params) ⇒ Object

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



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lhc/endpoint.rb', line 32

def remove_interpolated_params!(params)
  params ||= {}
  removed = {}
  url.scan(PLACEHOLDER) do |match|
    match = match.gsub(/^\:/, '')
    if value = find_value(match, params)
      removed[match.to_sym] = value
      params.delete(match.to_sym)
    end
  end
  removed
end