class Yeager::Router
- Yeager::Router
- Reference
- Object
Overview
Simple router implementation for Crystal, named after "Router Man" - William Yeager. It supports basic router requirements with speed but not battle-tested.
Usage:
require "yeager"
# Create router instance
router = Yeager::Router.new
# Define your routes
router.add "/foo"
router.add "/foo/:hello"
# Run a route on router which will return nil or an
# Hash(Symbol | String => String) if there is a match
router.run "/foo" # -> {:path => "/foo"}
router.run "/foo/world" # -> {"hello" => "world", :path => "/foo/:hello"}
router.run "/bar" # -> nil
Direct Known Subclasses
Defined in:
yeager/router.crInstance Method Summary
-
#add(path : String) : Nil
Adds provided path into the
routes
-
#handle(url : String) : Nil | Yeager::Result
Alias for #run
-
#run(url : String) : Nil | Yeager::Result
Splits the provided url, finds same sized routes and walks over them until find a match and will return the parameters (if defined in the route) in the first match with the
:path
in aResult
instance, if not found a match will returnnil
instead.
Instance Method Detail
Adds provided path into the routes
For given example;
require "yeager"
router = Yeager::Router.new
router.add "/foo/:hello"
router.add "/bar"
Routes will be;
{
"/foo/:hello" => ["foo", ":hello"],
"/bar" => ["bar"],
}
Splits the provided url, finds same sized routes
and walks over them until find a match and will return
the parameters (if defined in the route) in the first match
with the :path
in a Result
instance, if not found a match
will return nil
instead.
Result
will include the matched :path
and the parameters.
For given example;
require "yeager"
router = Yeager::Router.new
router.add "/foo/:hello"
router.run "/foo/world"
will return a Result
instance with following content;
{
"hello" => "world",
:path => "/foo/:hello",
}