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.crConstructors
Instance Method Summary
-
#add(path : String) : Nil
Adds provided path into the
routes -
#handle(url : String) : Yeager::Result?
Alias for #run
-
#handle_multiple(url : String) : Yeager::Result?
Alias for #run_multiple
-
#run(url : String) : Yeager::Result?
By using the run_multiple splits the provided url, and walks over routes until find a match and will return the parameters (if defined in the route) in the first match with the
:pathin aResultinstance, if not found a match will returnnilinstead. -
#run_multiple(url : String, once : Bool = false) : Array(Yeager::Result)?
Splits the provided url, finds same sized routes and walks over all of them.
Constructor Detail
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"],
}By using the run_multiple splits the provided url, and walks over
routes 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",
}Splits the provided url, finds same sized routes and walks over all
of them. Will keep matched ones in an Array, which will include the
parameters (if defined in the route) in the first match with the
:path in a Result instance, if not found any match will return
nil instead.
For given example;
require "yeager"
router = Yeager::Router.new
router.add "/foo/:hello"
router.add "/foo/:bar"
router.run_multiple "/foo/world"
will return an Array of Result instance with following content;
[
{
"hello" => "world",
:path => "/foo/:hello",
},
{
"bar" => "world",
:path => "/foo/:bar",
},
]