you can only have one scheme per route
This commit is contained in:
parent
669a5a6dc9
commit
941f0f565b
|
@ -148,13 +148,13 @@ class HttpRouter
|
|||
# Example:
|
||||
# router = HttpRouter.new
|
||||
# router.add('/:foo.:format', :name => :test).to{|env| [200, {}, []]}
|
||||
# router.url(:test, 123, 'html')
|
||||
# router.path(:test, 123, 'html')
|
||||
# # ==> "/123.html"
|
||||
# router.url(:test, 123, :format => 'html')
|
||||
# router.path(:test, 123, :format => 'html')
|
||||
# # ==> "/123.html"
|
||||
# router.url(:test, :foo => 123, :format => 'html')
|
||||
# router.path(:test, :foo => 123, :format => 'html')
|
||||
# # ==> "/123.html"
|
||||
# router.url(:test, :foo => 123, :format => 'html', :fun => 'inthesun')
|
||||
# router.path(:test, :foo => 123, :format => 'html', :fun => 'inthesun')
|
||||
# # ==> "/123.html?fun=inthesun"
|
||||
def url(route, *args)
|
||||
compile
|
||||
|
|
|
@ -7,13 +7,15 @@ class HttpRouter
|
|||
def initialize(route, path, validation_regex = nil)
|
||||
@route = route
|
||||
@path = path.dup
|
||||
@param_names = if path.respond_to?(:names)
|
||||
path.names.map(&:to_sym)
|
||||
puts "path is a string? #{@path.is_a?(String)}"
|
||||
@param_names = if @path.respond_to?(:names)
|
||||
@path.names.map(&:to_sym)
|
||||
elsif path.is_a?(String)
|
||||
path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
|
||||
@path.scan(/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/).map{|p| p.last.to_sym}
|
||||
else
|
||||
[]
|
||||
end
|
||||
puts "path #{@path.inspect} #{@param_names.inspect}"
|
||||
|
||||
if path.is_a?(String)
|
||||
path[0, 0] = '/' unless path[0] == ?/
|
||||
|
@ -41,7 +43,13 @@ class HttpRouter
|
|||
if validation_regex
|
||||
instance_eval <<-EOT, __FILE__, __LINE__ + 1
|
||||
def generate(args, options)
|
||||
puts 'generating!'
|
||||
puts "args: \#{args.inspect}"
|
||||
puts "options: \#{options.inspect}"
|
||||
generated_path = \"#{code}\"
|
||||
puts "after .. args: \#{args.inspect}"
|
||||
puts "generated path \#{generated_path.inspect}"
|
||||
p #{validation_regex.inspect}.match(generated_path)
|
||||
#{validation_regex.inspect}.match(generated_path) ? generated_path : nil
|
||||
end
|
||||
EOT
|
||||
|
@ -101,6 +109,7 @@ class HttpRouter
|
|||
path_args_processing(a) do |args, options|
|
||||
path = args.empty? ? matching_path(options) : matching_path(args, options)
|
||||
path &&= path.generate(args, options)
|
||||
puts "path is #{path.inspect}"
|
||||
raise TooManyParametersException unless args.empty?
|
||||
raise InvalidRouteException.new("Error generating #{@route.path_for_generation}") unless path
|
||||
path ? [path, options] : nil
|
||||
|
@ -125,7 +134,7 @@ class HttpRouter
|
|||
params_size = params ? params.size : 0
|
||||
path.param_names.size == (significant_keys ? (params_size) + significant_keys.size : params_size) }
|
||||
when Hash
|
||||
@path_generators.find { |path| (params && !params.empty? && (path.param_names & params.keys).size == path.param_names.size) || path.param_names.empty? }
|
||||
@path_generators.find { |path| p path.param_names; (params && !params.empty? && (path.param_names & params.keys).size == path.param_names.size) || path.param_names.empty? }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -127,7 +127,7 @@ class HttpRouter
|
|||
node = node.add_host([route.host, route.other_hosts].flatten.compact) if route.host or route.other_hosts
|
||||
node = node.add_user_agent(route.user_agent) if route.user_agent
|
||||
node = node.add_request_method(route.request_methods) if route.request_methods
|
||||
node = node.add_scheme(route.schemes) if route.schemes
|
||||
node = node.add_scheme(route.scheme) if route.scheme
|
||||
path_obj = node.add_destination(route, path, param_names)
|
||||
path_obj
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
class HttpRouter
|
||||
class Node
|
||||
class Scheme < AbstractRequestNode
|
||||
def initialize(router, parent, scheme)
|
||||
super(router, parent, scheme, :scheme)
|
||||
def initialize(router, parent, schemes)
|
||||
super(router, parent, schemes, :scheme)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ class HttpRouter
|
|||
class Route
|
||||
VALID_HTTP_VERBS = %w{GET POST PUT DELETE HEAD OPTION}
|
||||
|
||||
attr_reader :default_values, :router, :match_partially, :other_hosts, :paths, :request_methods, :schemes
|
||||
attr_reader :default_values, :router, :match_partially, :other_hosts, :paths, :request_methods
|
||||
attr_accessor :match_partially, :router, :host, :user_agent, :name, :ignore_trailing_slash,
|
||||
:path_for_generation, :path_validation_regex, :generator, :scheme
|
||||
|
||||
|
@ -34,10 +34,6 @@ class HttpRouter
|
|||
(@paths ||= []) << path
|
||||
end
|
||||
|
||||
def add_scheme(scheme)
|
||||
(@schemes ||= []) << scheme
|
||||
end
|
||||
|
||||
def add_request_method(methods)
|
||||
@request_methods ||= Set.new
|
||||
methods = [methods] unless methods.is_a?(Array)
|
||||
|
|
|
@ -39,6 +39,7 @@ class HttpRouter
|
|||
opts.delete(:conditions)
|
||||
end
|
||||
opts.each do |k, v|
|
||||
puts "k #{k.inspect} with adding? #{@route.respond_to?(:"add_#{k}")}"
|
||||
if @route.respond_to?(:"#{k}=")
|
||||
@route.send(:"#{k}=", v)
|
||||
elsif @route.respond_to?(:"add_#{k}")
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{"a": "/:var"}
|
||||
["/test", "a", {"var":"test"}]
|
||||
# ["/test", "a", ["test"]]
|
||||
["/test", "a", ["test"]]
|
||||
|
||||
{"a": "/"}
|
||||
{"b": "/test"}
|
||||
|
|
Loading…
Reference in New Issue