Class: Fastr::Router

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/fastr/router.rb

Overview

The router manages the routes for an application.

Routes are configured in the app/config/routes.rb file.

Example

router.draw do |route|
  route.for '/:controller/:action'
  route.for '/home/:action', :action => '[A-Za-z]+'
  route.for '/test', :to => 'home#index'
end

Author:

  • Chris Moos

Defined Under Namespace

Modules: Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

create_logger, included, level=

Constructor Details

#initialize(app) ⇒ Router

Returns a new instance of Router.



26
27
28
29
30
31
# File 'lib/fastr/router.rb', line 26

def initialize(app)
  @app = app
  self.routes = []
  self.route_file = "#{@app.app_path}/app/config/routes.rb"
  setup_watcher
end

Instance Attribute Details

#route_fileString

The full path to the routes file.

Returns:



24
25
26
# File 'lib/fastr/router.rb', line 24

def route_file
  @route_file
end

#routesArray

The routes for this router.

Returns:

  • (Array)


20
21
22
# File 'lib/fastr/router.rb', line 20

def routes
  @routes
end

Instance Method Details

#draw(&block) ⇒ Object

Evaluates the block in the context of the router.



107
108
109
# File 'lib/fastr/router.rb', line 107

def draw(&block)
  block.call(self)
end

#for(path, *args) ⇒ Object

Adds a route for a path and arguments.

Parameters:



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fastr/router.rb', line 92

def for(path, *args)
  arg = args[0]
  log.debug "Adding route, path: #{path}, args: #{args.inspect}"

  match = get_regex_for_route(path, arg)
  hash = get_to_hash(arg)
  route_info = {:regex => match[:regex], :args => arg, :vars => match[:vars], :hash => hash}
  
  # Add the HTTP methods for this route if they exist in options
  route_info[:methods] = arg[:methods] if not arg.nil? and arg.has_key? :methods
  
  self.routes.push(route_info)
end

#loadObject

Loads the routes from #route_file and evaluates it within the context of Fastr::Router.



80
81
82
83
84
85
86
# File 'lib/fastr/router.rb', line 80

def load
  log.debug "Loading routes from: #{self.route_file}"
  self.routes = []
  
  file = File.open(self.route_file)
  @app.instance_eval(file.read)
end

#match(env) ⇒ Hash

Searches the routes for a match given a Rack env.

#match looks in the #routes to find a match.

This method looks at PATH_INFO in env to get the current request’s path.

Return

No Match:

{:error => :not_found}

Match:

{:ok => {:controller => 'controller', :action => 'action', :var => 'value'}}

Parameters:

  • env (Hash)

Returns:

  • (Hash)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fastr/router.rb', line 51

def match(env)
  self.routes.each do |info|
    
    # If the route didn't specify method(s) to limit by, then all HTTP methods are valid.
    # If the route specified method(s), we check the request's HTTP method and validate
    # that it exists in that list.
    next unless info[:methods].nil? or info[:methods].include?(env["REQUEST_METHOD"].downcase.to_sym)
    
    match = env['PATH_INFO'].match(info[:regex])

    # See if a route matches
    if not match.nil?

      # Map any parameters in our matched string
      vars = {}
      
      info[:vars].each_index do |i|
        var = info[:vars][i]
        vars[var] = match[i+1]
      end

      return {:ok => vars.merge!(info[:hash]) }
    end
  end
  
  {:error => :not_found}
end