Sha256: a109a4db1c7b0230dd57ff5d7befae5f7fb53151b33779bef0faee66a800819c
Contents?: true
Size: 1.35 KB
Versions: 1
Compression:
Stored size: 1.35 KB
Contents
require 'uri_template' module BooticClient class WhinyURI attr_reader :variables def initialize(href) @href = href @uri = URITemplate.new(href) @variables = @uri.variables end def expand(attrs = {}) missing = missing_path_variables(attrs) if missing.any? raise InvalidURLError, missing_err(missing) end undeclared = undeclared_params(attrs) if undeclared.any? raise InvalidURLError, undeclared_err(undeclared) end uri.expand attrs end private attr_reader :uri, :href def path_variables @path_variables ||= ( variables.find_all{ |v| Regexp.new("(\/\{#{v}\})|(\{\/#{v}\})") =~ href } ) end def missing_path_variables(attrs) path_variables - attrs.keys.map(&:to_s) end def undeclared_params(attrs) attrs.keys.map(&:to_s) - variables end 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 def missing_err(missing) "missing required path variables: #{format_vars(missing)}" end def format_vars(vars) vars.map{|v| "`#{v}`"}.join(', ') end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
bootic_client-0.0.19 | lib/bootic_client/whiny_uri.rb |