class Wouter::Route
`Wouter::Route` represents a route definition. Example:
get '/users/:id/comments/:comment_id', RouteKlass
It contains utility methods for matching a Rack request to a route.
Constants
- REGEX
Attributes
klass[R]
path[R]
request_method[R]
Public Class Methods
new(request_method:, path:, klass:)
click to toggle source
# File lib/wouter/route.rb, line 14 def initialize(request_method:, path:, klass:) @request_method = request_method @path = path @klass = klass end
Public Instance Methods
match?(request)
click to toggle source
Expects `request` to be a `Rack::Request` Check `request.request_method` and `request.path`
# File lib/wouter/route.rb, line 22 def match?(request) return false if request.request_method.to_sym != @request_method return true if request.path == path path_regex =~ request.path end
params(request)
click to toggle source
Expects `request` to be a `Rack::Request` Parse the path parameters out of `request.path`
# File lib/wouter/route.rb, line 31 def params(request) request.path.match(named_path_regex)&.named_captures end
Private Instance Methods
named_path_regex()
click to toggle source
/user/:id/comments/:comment_id //user/(?<id>w+)/comments/(?<comment_id>w+)/
# File lib/wouter/route.rb, line 39 def named_path_regex @named_path_regex ||= begin string_regex = path path.scan(/(:\w+)/).flatten.each do |param| string_regex = string_regex.gsub(param, '(?<' + param[1..-1] +'>' + REGEX + ')') end Regexp.new('^' + string_regex + '$') end end
path_regex()
click to toggle source
/user/:id/comments/:comment_id //user/(w+)/comments/(w+)/
# File lib/wouter/route.rb, line 51 def path_regex @path_regex ||= begin string_regex = path path.scan(/(:\w+)/).flatten.each do |param| string_regex = string_regex.gsub(param, REGEX) end Regexp.new('^' + string_regex + '$') end end