Class: HttpRouter::Path
- Inherits:
-
Object
- Object
- HttpRouter::Path
- Defined in:
- lib/http_router/path.rb
Instance Attribute Summary collapse
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#splitting_indexes ⇒ Object
readonly
Returns the value of attribute splitting_indexes.
Instance Method Summary collapse
- #===(other_path) ⇒ Object
- #compare_parts(p1, p2) ⇒ Object
- #generate_querystring(uri, params) ⇒ Object
-
#initialize(route, path, parts, splitting_indexes) ⇒ Path
constructor
A new instance of Path.
- #url(args, options) ⇒ Object
- #variable_names ⇒ Object
- #variables ⇒ Object
Constructor Details
#initialize(route, path, parts, splitting_indexes) ⇒ Path
Returns a new instance of Path.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/http_router/path.rb', line 4 def initialize(route, path, parts, splitting_indexes) @route, @path, @parts, @splitting_indexes = route, path, parts, splitting_indexes if duplicate_variable_names = variable_names.dup.uniq! raise AmbiguousVariableException.new("You have duplicate variable name present: #{duplicate_variable_names.join(', ')}") end @path_validation_regex = path.split(/([:\*][a-zA-Z0-9_]+)/).map{ |part| case part[0] when ?:, ?* route.matches_with[part[1, part.size].to_sym] || '.*?' else Regexp.quote(part) end }.join @path_validation_regex = Regexp.new("^#{@path_validation_regex}$") eval_path = path.gsub(/[:\*]([a-zA-Z0-9_]+)/) {"\#{args.shift || (options && options.delete(:#{$1})) || raise(MissingParameterException.new(\"missing parameter #{$1}\"))}" } instance_eval " def raw_url(args,options) \"#{eval_path}\" end " end |
Instance Attribute Details
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
3 4 5 |
# File 'lib/http_router/path.rb', line 3 def parts @parts end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
3 4 5 |
# File 'lib/http_router/path.rb', line 3 def route @route end |
#splitting_indexes ⇒ Object (readonly)
Returns the value of attribute splitting_indexes.
3 4 5 |
# File 'lib/http_router/path.rb', line 3 def splitting_indexes @splitting_indexes end |
Instance Method Details
#===(other_path) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/http_router/path.rb', line 28 def ===(other_path) return false if @parts.size != other_path.parts.size @parts.each_with_index {|p,i| return unless compare_parts(p, other_path.parts[i]) } true end |
#compare_parts(p1, p2) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/http_router/path.rb', line 36 def compare_parts(p1, p2) case p1 when Glob then p2.is_a?(Glob) when Variable then p2.is_a?(Variable) else p1 == p2 end end |
#generate_querystring(uri, params) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/http_router/path.rb', line 54 def generate_querystring(uri, params) if params && !params.empty? uri_size = uri.size params.each do |k,v| case v when Array v.each { |v_part| uri << '&' << Rack::Utils.escape(k.to_s) << '%5B%5D=' << Rack::Utils.escape(v_part.to_s) } else uri << '&' << Rack::Utils.escape(k.to_s) << '=' << Rack::Utils.escape(v.to_s) end end uri[uri_size] = ?? end end |
#url(args, options) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/http_router/path.rb', line 45 def url(args, ) path = raw_url(args, ) raise InvalidRouteException.new if path !~ @path_validation_regex raise TooManyParametersException.new unless args.empty? Rack::Utils.uri_escape!(path) generate_querystring(path, ) path end |
#variable_names ⇒ Object
76 77 78 |
# File 'lib/http_router/path.rb', line 76 def variable_names @variable_names ||= variables.map{|v| v.name} end |
#variables ⇒ Object
69 70 71 72 73 74 |
# File 'lib/http_router/path.rb', line 69 def variables unless @variables @variables = @parts.select{|p| p.is_a?(Variable)} end @variables end |