Class: Wouter::Route

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File 'lib/wouter/route.rb', line 12

def klass
  @klass
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/wouter/route.rb', line 12

def path
  @path
end

#request_methodObject (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`

Returns:

  • (Boolean)


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