class

Cradix(Payload)

Inherits Reference < Object

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", {}}

Constants

VERSION = {{ (`shards version /tmp/tmp.HMJMdk/src/src`).chomp.stringify }}

Instance methods

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.

Source
search(path : String) : Array(Tuple(Payload, Hash(String, String), String | Nil))

Search the tree for matching elements. Returns an array of payloads and extracted url parameters. there might be multiple results when path parameters 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"}}]
Source