module LHS::Record::Endpoints::ClassMethods
Public Instance Methods
compute_url(params, url = nil)
click to toggle source
# File lib/lhs/concerns/record/endpoints.rb, line 61 def compute_url(params, url = nil) find_endpoint(params, url) .compile(params) end
compute_url!(params)
click to toggle source
Computes the url from params by identifiying endpoint and compiles it if necessary.
# File lib/lhs/concerns/record/endpoints.rb, line 54 def compute_url!(params) endpoint = find_endpoint(params) url = endpoint.compile(params) endpoint.remove_interpolated_params!(params) url end
endpoint(url, options = nil)
click to toggle source
Adds the endpoint to the list of endpoints.
# File lib/lhs/concerns/record/endpoints.rb, line 24 def endpoint(url, options = nil) self.endpoints = endpoints.clone validates_deprecation_check!(options) endpoint = LHC::Endpoint.new(url, options) endpoints.push(endpoint) LHS::Record::Endpoints.all ||= {} LHS::Record::Endpoints.all[url] ||= self end
find_endpoint(params = {}, url = nil)
click to toggle source
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.
# File lib/lhs/concerns/record/endpoints.rb, line 44 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)
click to toggle source
# File lib/lhs/concerns/record/endpoints.rb, line 33 def for_url(url) return unless url _template, record = LHS::Record::Endpoints.all.dup.detect do |template, _| LHC::Endpoint.match?(url, template) end record end
Private Instance Methods
find_base_endpoint()
click to toggle source
Finds the base endpoint. A base endpoint is the one thats has the least amont of placeholers. There cannot be multiple base endpoints.
# File lib/lhs/concerns/record/endpoints.rb, line 99 def find_base_endpoint endpoints = self.endpoints.group_by do |endpoint| endpoint.placeholders.length end bases = endpoints[endpoints.keys.min] bases.first end
find_best_endpoint(params)
click to toggle source
Finds the best endpoint. The best endpoint is the one where all placeholders are interpolated.
# File lib/lhs/concerns/record/endpoints.rb, line 78 def find_best_endpoint(params) sorted_endpoints.find do |endpoint| endpoint.placeholders.all? { |match| endpoint.find_value(match, params).present? } end end
find_endpoint_by_url(url)
click to toggle source
Find
endpoint by given URL
# File lib/lhs/concerns/record/endpoints.rb, line 85 def find_endpoint_by_url(url) sorted_endpoints.find do |endpoint| LHC::Endpoint.match?(url, endpoint.url) end end
sorted_endpoints()
click to toggle source
Sort endpoints by number of placeholders, heighest first
# File lib/lhs/concerns/record/endpoints.rb, line 92 def sorted_endpoints endpoints.sort { |a, b| b.placeholders.count <=> a.placeholders.count } end
validates_deprecation_check!(options)
click to toggle source
# File lib/lhs/concerns/record/endpoints.rb, line 68 def validates_deprecation_check!(options) return if options.blank? return if options[:validates].blank? return if options[:validates].is_a?(Hash) return if !options[:validates].is_a?(TrueClass) && options[:validates].match(%r{^\/}) raise 'Validates with either true or a simple string is deprecated! See here: https://github.com/local-ch/lhs#validation' end