Parent

Innate::Route

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.

Constants

ROUTES

Public Class Methods

[](key) click to toggle source
    # File lib/innate/route.rb, line 48
48:     def self.[](key)
49:       found = self::ROUTES.assoc(key)
50:       found[1] if found
51:     end
[]=(key, value) click to toggle source
    # File lib/innate/route.rb, line 53
53:     def self.[]=(key, value)
54:       self::ROUTES.delete_if{|route_key, route_value| route_key == key }
55:       self::ROUTES << [key, value]
56:     end
clear() click to toggle source
    # File lib/innate/route.rb, line 58
58:     def self.clear
59:       self::ROUTES.clear
60:     end
new(app = Innate::DynaMap) click to toggle source
    # File lib/innate/route.rb, line 62
62:     def initialize(app = Innate::DynaMap)
63:       @app = app
64:     end

Public Instance Methods

call(env) click to toggle source
    # File lib/innate/route.rb, line 66
66:     def call(env)
67:       path = env['PATH_INFO']
68:       path << '/' if path.empty?
69: 
70:       if modified = resolve(path)
71:         Log.debug("%s routes %p to %p" % [self.class.name, path, modified])
72:         env['PATH_INFO'] = modified
73:       end
74: 
75:       @app.call(env)
76:     end
resolve(path) click to toggle source
    # File lib/innate/route.rb, line 78
78:     def resolve(path)
79:       self.class::ROUTES.each do |key, value|
80:         if key.is_a?(Regexp)
81:           md = path.match(key)
82:           return value % md.to_a[1..1] if md
83: 
84:         elsif value.respond_to?(:call)
85:           new_path = value.call(path, Current.request)
86:           return new_path if new_path
87: 
88:         elsif value.respond_to?(:to_str)
89:           return value.to_str if path == key
90: 
91:         else
92:           Log.error("Invalid route %p => %p" % [key, value])
93:         end
94:       end
95: 
96:       nil
97:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.