Class: Redwing::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/redwing/router.rb

Constant Summary collapse

SUPPORTED_METHODS =
%w[GET POST PUT PATCH DELETE].freeze
PARAM_PATTERN =
/:([A-Za-z_][A-Za-z0-9_]*)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



10
11
12
# File 'lib/redwing/router.rb', line 10

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



8
9
10
# File 'lib/redwing/router.rb', line 8

def routes
  @routes
end

Instance Method Details

#match(method, path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/redwing/router.rb', line 32

def match(method, path)
  routes.each do |route|
    next unless route[:method] == method

    match_data = route[:matcher].match(path)
    next unless match_data

    return {route: route, params: match_data.named_captures}
  end
  nil
end

#root(to:) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
# File 'lib/redwing/router.rb', line 26

def root(to:)
  raise ArgumentError, 'root route already defined' if @routes.any? { |r| r[:path] == '/' }

  @routes << {method: 'GET', path: '/', to: to, matcher: compile('/')}
end