lib/galago/router/path.rb in galago-router-0.0.1 vs lib/galago/router/path.rb in galago-router-0.0.2
- old
+ new
@@ -1,44 +1,49 @@
module Galago
- class Router::Path
+ class Router
+ class Path
- def initialize(path)
- @path = path.to_s
- end
+ def initialize(path)
+ @path = path.to_s
+ end
- def recognizes?(request_path)
- request_path =~ regex
- end
+ def recognizes?(request_path)
+ request_path =~ regex
+ end
- def named_parameters
- @path_parameters ||= @path.scan(/\:(\w+)/).flatten
- end
+ def named_parameters
+ @path_parameters ||= @path.scan(/\:(\w+)/).flatten
+ end
- def add_path_params_to_env(env)
- request = Rack::Request.new(env)
- identify_params_in_path(request.path).each do |key, value|
- request.update_param(key, value)
+ def add_path_params_to_env(env)
+ request = Rack::Request.new(env)
+
+ if path_params = identify_params_in_path(request.path)
+ path_params.each { |key, value| request.update_param(key, value) }
+ end
end
- end
- def identify_params_in_path(request_path)
- values = regex.match(request_path).captures
- Hash[named_parameters.zip(values)]
- end
+ def to_s
+ @path
+ end
- def to_s
- @path
- end
+ private
- private
+ def regex
+ @regex_path ||= convert_path_to_regex(@path)
+ end
- def regex
- @regex_path ||= convert_path_to_regex(@path)
- end
+ def convert_path_to_regex(path)
+ regexp = path.to_s.gsub(/\:\w+/, '([\w-]+)')
+ Regexp.new("^#{regexp}$")
+ end
- def convert_path_to_regex(path)
- regexp = path.to_s.gsub(/\:\w+/, '([\w-]+)')
- Regexp.new("^#{regexp}$")
- end
+ def identify_params_in_path(path)
+ if match = regex.match(path)
+ named_parameters.zip(match.captures)
+ end
+ end
+ end
end
end
+