class LHC::Endpoint

The url can also be an url-template.
An endpoint is an url that leads to a backend resource.
frozen_string_literal: true

def self.match?(url, template)

Example: :datastore/contracts/:id == http://local.ch/contracts/1
Returns true if concrete url is covered by the template
Compares a concrete url with a template
def self.match?(url, template)
  regexp = template.gsub PLACEHOLDER, ANYTHING_BUT_SINGLE_SLASH_AND_DOT
  regexp += URL_PARAMETERS
  url.match "#{regexp}$"
end

def self.placeholders(template)

They are alphabetically sorted.
Returns all placeholders found in the url-template.
def self.placeholders(template)
  template.scan(PLACEHOLDER).sort
end

def self.values_as_params(template, url)

creates params according to template
Extracts the values from url and
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
  regexp += URL_PARAMETERS
  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

def add_basic_auth(url)

def add_basic_auth(url)
  return url if !uri || !uri.is_a?(URI) || (uri.user.blank? && uri.password.blank?)
  new_uri = parse_url_gracefully(url)
  new_uri.user = uri.user
  new_uri.password = uri.password
  new_uri.to_s
end

def compile(params)

def compile(params)
  add_basic_auth(
    without_basic_auth(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

def find_value(match, params)

or in the provided params.
Find a value for a placeholder either in the configuration
def find_value(match, params)
  params ||= {}
  match = match.gsub(/^\:/, '').to_sym
  params[match] || LHC.config.placeholders[match]
end

def initialize(url, options = nil)

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

def options

Endpoint options are immutable
def options
  @options.deep_dup
end

def parse_url_gracefully(url)

def parse_url_gracefully(url)
  URI.parse(url)
rescue URI::InvalidURIError
  url
end

def placeholders

They are alphabetically sorted.
Returns all placeholders found in the url-template.
def placeholders
  LHC::Endpoint.placeholders(url)
end

def remove_interpolated_params!(params)

when they are used for interpolation.
Removes keys from provided params hash
def remove_interpolated_params!(params)
  params ||= {}
  removed = {}
  url.scan(PLACEHOLDER) do |match|
    match = match.gsub(/^\:/, '')
    value = find_value(match, params)
    if value
      removed[match.to_sym] = value
      params.delete(match.to_sym)
    end
  end
  removed
end

def uri

def uri
  @uri ||= parse_url_gracefully(url)
end

def without_basic_auth(url)

Strips basic auth from the url
def without_basic_auth(url)
  return url if !uri || !uri.is_a?(URI) || (uri.user.blank? && uri.password.blank?)
  url.gsub("#{uri.user}:#{uri.password}@", '')
end