Class: DHC::Endpoint
- Inherits:
-
Object
- Object
- DHC::Endpoint
- Defined in:
- lib/dhc/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
-
#options ⇒ Object
Endpoint options are immutable.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.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 == depay.fi/contracts/1.
-
.placeholders(template) ⇒ Object
Returns all placeholders found in the url-template.
-
.values_as_params(template, url) ⇒ Object
Extracts the values from url and creates params according to template.
Instance Method Summary collapse
- #compile(params) ⇒ Object
-
#find_value(name, mapping) ⇒ Object
Checks if the name has a match in the current context.
-
#initialize(url, options = nil) ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#match?(url) ⇒ Boolean
Compares a concrete url with a template Returns true if concrete url is covered by the template Example: +datastore/contracts/id == depay.fi/contracts/1.
-
#placeholders ⇒ Object
Returns all placeholders found in the url-template.
-
#remove_interpolated_params!(params) ⇒ Object
Removes keys from provided params hash when they are used for interpolation.
- #uri ⇒ Object
-
#values_as_params(url) ⇒ Object
Extracts the values from url and creates params according to template.
Constructor Details
#initialize(url, options = nil) ⇒ Endpoint
Returns a new instance of Endpoint.
11 12 13 14 |
# File 'lib/dhc/endpoint.rb', line 11 def initialize(url, = nil) self.url = url self. = end |
Instance Attribute Details
#options ⇒ Object
Endpoint options are immutable
33 34 35 |
# File 'lib/dhc/endpoint.rb', line 33 def @options.deep_dup end |
#url ⇒ Object
Returns the value of attribute url.
8 9 10 |
# File 'lib/dhc/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 == depay.fi/contracts/1
92 93 94 |
# File 'lib/dhc/endpoint.rb', line 92 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.
98 99 100 |
# File 'lib/dhc/endpoint.rb', line 98 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
104 105 106 107 |
# File 'lib/dhc/endpoint.rb', line 104 def self.values_as_params(template, url) raise("#{url} does not match the template: #{template}") unless 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/dhc/endpoint.rb', line 20 def compile(params) context = DHC.config.placeholders.deep_dup context.merge!(params) if params.is_a?(Hash) = uri.(context) if .variables.empty? .pattern else raise("Compilation incomplete. Unable to find value for #{.variables.join(', ')}.") end end |
#find_value(name, mapping) ⇒ Object
Checks if the name has a match in the current context
82 83 84 85 86 87 |
# File 'lib/dhc/endpoint.rb', line 82 def find_value(name, mapping) context = DHC.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 == depay.fi/contracts/1
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dhc/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 |
#placeholders ⇒ Object
Returns all placeholders found in the url-template. They are alphabetically sorted.
49 50 51 |
# File 'lib/dhc/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/dhc/endpoint.rb', line 39 def remove_interpolated_params!(params) params ||= {} removed = params.slice(*placeholders) params.except!(*placeholders) removed end |
#uri ⇒ Object
16 17 18 |
# File 'lib/dhc/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
75 76 77 78 79 |
# File 'lib/dhc/endpoint.rb', line 75 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 |