lib/rasti/web/route.rb in rasti-web-0.2.3 vs lib/rasti/web/route.rb in rasti-web-1.0.0
- old
+ new
@@ -13,21 +13,24 @@
def match?(path)
!@regexp.match(normalize(path)).nil?
end
def extract_params(path)
- result = @regexp.match path
- result ? result.names.each_with_object({}) { |v,h| h[v] = result[v] } : {}
+ result = @regexp.match normalize(path)
+ result ? result.names.each_with_object(Hash::Indifferent.new) { |v,h| h[v] = result[v] } : {}
end
def call(env)
@endpoint.call env
end
private
def compile
- %r{^#{pattern.gsub(')', '){0,1}').gsub(/:[a-z0-9_-]+/) { |var| "(?<#{var[1..-1]}>[^\/?#]+)" }}$}
+ compiled = pattern.gsub(')', '){0,1}')
+ .gsub('/*', '(\/(?<wildcard>.*))?')
+ .gsub(/:[a-z0-9_-]+/) { |var| "(?<#{var[1..-1]}>[^\/?#]+)" }
+ %r{^#{compiled}$}
end
def normalize(path)
return '/' if path.strip.empty? || path == '/'
return path[0..-2] if path[-1, 1] == '/'
\ No newline at end of file