Class | Innate::Route |
In: |
lib/innate/route.rb
|
Parent: | Object |
Innate support simple routing using string, regex and lambda based routers. Route are stored in a dictionary, which supports hash-like access but preserves order, so routes are evaluated in the order they are added.
This middleware should wrap Innate::DynaMap.
Please note that Rack::File is put before Route and Rewrite, that means that you cannot apply routes to static files unless you add your own route middleware before.
String routers are the simplest way to route in Innate. One path is translated into another:
Innate::Route[ '/foo' ] = '/bar' '/foo' => '/bar'
Regex routers allow matching against paths using regex. Matches within your regex using () are substituted in the new path using printf-like syntax.
Innate::Route[ %r!^/(\d+)\.te?xt$! ] = "/text/%d" '/123.txt' => '/text/123' '/789.text' => '/text/789'
For more complex routing, lambda routers can be used. Lambda routers are passed in the current path and request object, and must return either a new path string, or nil.
Innate::Route[ 'name of route' ] = lambda{ |path, request| '/bar' if path == '/foo' and request[:bar] == '1' } '/foo' => '/foo' '/foo?bar=1' => '/bar'
Lambda routers can also use this alternative syntax:
Innate::Route('name of route') do |path, request| '/bar' if path == '/foo' and request[:bar] == '1' end
NOTE: Use self::ROUTES notation in singleton methods to force correct
lookup.
ROUTES | = | [] |