class BooticClient::WhinyURI

Attributes

complain_on_undeclared_params[R]
href[R]
uri[R]
variables[R]

Public Class Methods

new(href, complain_on_undeclared_params = true) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 9
def initialize(href, complain_on_undeclared_params = true)
  @href = href
  @uri = URITemplate.new(href)
  @variables = @uri.variables
  @complain_on_undeclared_params = complain_on_undeclared_params
end

Public Instance Methods

expand(attrs = {}) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 16
def expand(attrs = {})
  attrs = stringify(attrs)

  missing = missing_path_variables(attrs)
  if missing.any?
    raise InvalidURLError, missing_err(missing)
  end

  undeclared = undeclared_params(attrs)
  if complain_on_undeclared_params
    if undeclared.any?
      raise InvalidURLError, undeclared_err(undeclared)
    end
  end

  uri.expand whitelisted(attrs)
end

Private Instance Methods

declared_params() click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 55
def declared_params
  @declared_params ||= variables - path_variables
end
format_vars(vars) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 74
def format_vars(vars)
  vars.map{|v| "`#{v}`"}.join(', ')
end
missing_err(missing) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 70
def missing_err(missing)
  "missing required path variables: #{format_vars(missing)}"
end
missing_path_variables(attrs) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 51
def missing_path_variables(attrs)
  path_variables - attrs.keys
end
path_variables() click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 37
def path_variables
  @path_variables ||= (
    variables.find_all{ |v|
      !!(href["{#{v}}"]) || !!(href["{/#{v}}"])
    }
  )
end
stringify(attrs) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 78
def stringify(attrs)
  attrs.each_with_object({}) do |(k, v), hash|
    hash[k.to_s] = v
  end
end
undeclared_err(undeclared) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 63
def undeclared_err(undeclared)
  msg = ["undeclared URI variables: #{format_vars(undeclared)}"]
  query_vars = variables - path_variables
  msg << "Allowed query variables are #{format_vars(query_vars)}" if query_vars.any?
  msg.join('. ')
end
undeclared_params(attrs) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 59
def undeclared_params(attrs)
  attrs.keys - variables
end
whitelisted(attrs = {}) click to toggle source
# File lib/bootic_client/whiny_uri.rb, line 45
def whitelisted(attrs = {})
  variables.each_with_object({}) do |key, hash|
    hash[key] = attrs[key] if attrs.key?(key)
  end
end