Class: Wouter::Route
- Inherits:
-
Object
- Object
- Wouter::Route
- Defined in:
- lib/wouter/route.rb
Overview
‘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.
Constant Summary collapse
- REGEX =
'([A-Za-z0-9\-\_]+)'
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#request_method ⇒ Object
readonly
Returns the value of attribute request_method.
Instance Method Summary collapse
-
#initialize(request_method:, path:, klass:) ⇒ Route
constructor
A new instance of Route.
-
#match?(request) ⇒ Boolean
Expects ‘request` to be a `Rack::Request` Check `request.request_method` and `request.path`.
-
#params(request) ⇒ Object
Expects ‘request` to be a `Rack::Request` Parse the path parameters out of `request.path`.
Constructor Details
#initialize(request_method:, path:, klass:) ⇒ Route
Returns a new instance of Route.
14 15 16 17 18 |
# File 'lib/wouter/route.rb', line 14 def initialize(request_method:, path:, klass:) @request_method = request_method @path = path @klass = klass end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
12 13 14 |
# File 'lib/wouter/route.rb', line 12 def klass @klass end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/wouter/route.rb', line 12 def path @path end |
#request_method ⇒ Object (readonly)
Returns the value of attribute request_method.
12 13 14 |
# File 'lib/wouter/route.rb', line 12 def request_method @request_method end |
Instance Method Details
#match?(request) ⇒ Boolean
Expects ‘request` to be a `Rack::Request` Check `request.request_method` and `request.path`
22 23 24 25 26 27 |
# 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) ⇒ Object
Expects ‘request` to be a `Rack::Request` Parse the path parameters out of `request.path`
31 32 33 |
# File 'lib/wouter/route.rb', line 31 def params(request) request.path.match(named_path_regex)&.named_captures end |