From defb7703c69d1e4f756254723d50997fb8dc63da Mon Sep 17 00:00:00 2001 From: Gibheer Date: Mon, 9 Sep 2013 15:45:59 +0200 Subject: sort routes for more specific results This change sorts the routes to get the most specific routes first and avoid hitting a route which is not the most specific match. With this the regex to extract variables is made more strict to avoid matching half of the URI. --- lib/zero/router.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/zero/router.rb b/lib/zero/router.rb index 659d9b6..482f25a 100644 --- a/lib/zero/router.rb +++ b/lib/zero/router.rb @@ -20,7 +20,7 @@ module Zero # match for variables in routes VARIABLE_MATCH = %r{:(\w+)[^/]?} # the replacement string to make it an regex - VARIABLE_REGEX = '(?<\1>.+?)' + VARIABLE_REGEX = '(?<\1>[\w]+?)' # regex part of the beginning of the line REGEX_BEGINNING = '\A' # regex part of the end of the line @@ -41,7 +41,7 @@ module Zero # @param routes [Hash] a map of URLs to rack compatible applications def initialize(routes) @routes = {} - routes.each do |route, target| + routes.to_a.sort_by(&:first).reverse.each do |route, target| @routes[ Regexp.new(REGEX_BEGINNING + route.gsub(VARIABLE_MATCH, VARIABLE_REGEX) + -- cgit v1.2.3-70-g09d2