class Yeager::RouterHandler(T)
- Yeager::RouterHandler(T)
- Yeager::Router
- Reference
- Object
Overview
Yeager::Router with handler support which will call the provided block if there is a match with the route processed. Takes Type argument for the block arguments, it must be a Proc with required argument types. Return value is not used so passing Nil would be enough.
Usage:
require "yeager"
# Create router handler instance with (-> Nil) block type
router = Yeager::RouterHandler(-> Nil).new
# Define your routes with paths and blocks to call (a.k.a callback)
router.add "/foo", ->{ print "Hello from /foo!" }
# Run a route on router handler which will return nil or an
# Hash(Symbol | String => String) if there is a match and will call
# the provided block for the path
router.handle "/foo" # -> {:path => "/foo"} and prints "Hello from /foo!"
router.handle "/bar" # -> nil
Defined in:
yeager/routerhandler.crInstance Method Summary
-
#add(path : String, callback : T) : Nil
Adds provided path into the
routes
and thecallback
to handlers -
#handle(url : String, params : Bool) : Nil | Yeager::Result
If path found executes the handler with parameters if
params
value isfalse
,empty_result
will be passed as params -
#handle(url : String, params : Bool, *args) : Nil | Yeager::Result
If path found executes the handler with parameters and provided arguments if
params
value isfalse
,empty_result
will be passed as params -
#handle(url : String, *args) : Nil | Yeager::Result
If path found executes the handler with provided arguments
Instance methods inherited from class Yeager::Router
add(path : String) : Nil
add,
handle(url : String) : Nil | Yeager::Result
handle,
run(url : String) : Nil | Yeager::Result
run
Instance Method Detail
Adds provided path into the routes
and the callback
to handlers
For given example;
require "yeager"
router = Yeager::RouterHandler(String -> Nil).new
router.add "/foo/:hello", ->(name : String) {
print "Hello #{name}!"
}
Routes will be;
{"/foo/:hello" => ["foo", ":hello"]}
and Handlers;
{"/foo/:hello" => Proc(String -> Nil)}
If path found executes the handler with parameters
if params
value is false
, empty_result
will be passed as params
require "yeager"
r = Yeager::RouterHandler(Yeager::Result -> Nil).new
r.add "/:name", ->(params : Yeager::Result) {
p "Hello #{params["name"]}!"
}
r.handle("/user", params = true)
will print "Hello user!"
If path found executes the handler with parameters and provided arguments
if params
value is false
, empty_result
will be passed as params
require "yeager"
r = Yeager::RouterHandler(Yeager::Result, String -> Nil).new
r.add "/:name", ->(params : Yeager::Result, user_type : String) {
p "Hello #{user_type} #{params["name"]}!"
}
r.handle("/user", params = true, "test")
will print "Hello test user!"
If path found executes the handler with provided arguments
require "yeager"
r = Yeager::RouterHandler(String -> Nil).new
called = false
r.add "/", ->(name : String) { p "Hello #{name}!" }
r.handle "/", "user"
will print "Hello user!"