class Cradix(Payload)
- Cradix(Payload)
- Reference
- Object
Overview
A radix like tree specialized in uri path for routing purpose. Usage:
radix = Cradix(String).new
radix.add "/users", "List users"
radix.add "/users/:id", "Get a user by id"
radix.add "/users/self", "Get the current login in user"
puts radix.search("/users/toto").first # => {"Get a user by id", {"id" => "toto"}}
puts radix.search("/users/self").first # => {"Get the current login in user", {}}
Defined in:
cradix.crConstant Summary
-
VERSION =
{{ (`shards version /srv/crystaldoc.info/github-Globoplox-cradix-v1.0.0/src`).chomp.stringify }}
Instance Method Summary
-
#add(path : String, payload : Payload) : Nil
Add an entry to the tree.
-
#search(path : String) : Array(Tuple(Payload, Hash(String, String)))
Search the tree for matching elements.
Instance Method Detail
def add(path : String, payload : Payload) : Nil
#
Add an entry to the tree. path is expected to be an url path, with optional placeholders prefixed with ':'. If their was already a payload for this path, it is redfined.
def search(path : String) : Array(Tuple(Payload, Hash(String, String)))
#
Search the tree for matching elements. Returns an array of payloads and extracted url parameters. there might be multiple results when wildcards are involved. The non wildcard match always appear first in the results if there is one. Example:
node.add "/user/toto/name", "A"
node.add "/user/:id/name", "B"
node.add "/:blib/:blob/name", "C"
node.add "/:foo/:bar/age", "D"
node.search "/user/toto/name" # => [{"A", {}},
{"B", {"id" => "toto"}},
{"C", {"blib" => "user", "blob" => "toto"}}]