Class: Fastr::Application

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

Defined Under Namespace

Modules: Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

included

Constructor Details

#initialize(path) ⇒ Application

Returns a new instance of Application.



9
10
11
12
13
14
# File 'lib/fastr/application.rb', line 9

def initialize(path)
  self.app_path = path
  
  @booting = true
  boot
end

Instance Attribute Details

#app_pathObject

Returns the value of attribute app_path.



7
8
9
# File 'lib/fastr/application.rb', line 7

def app_path
  @app_path
end

#routerObject

Returns the value of attribute router.



7
8
9
# File 'lib/fastr/application.rb', line 7

def router
  @router
end

Instance Method Details

#dispatch(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fastr/application.rb', line 16

def dispatch(env)
  return [500, {}, "Server Not Ready"] if @booting
  
  begin 
    do_dispatch(env)
  rescue Exception => e
    bt = e.backtrace.join("\n")
    [500, {}, "Exception: #{e}\n\n#{bt}"]
  end
end

#do_dispatch(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fastr/application.rb', line 27

def do_dispatch(env)
  path = env['PATH_INFO']
  
  log.debug "Checking for routes that match: #{path}"
  route = router.match(env)

  if route.has_key? :ok
    vars = route[:ok]        
    controller = vars[:controller]
    action = vars[:action]
    
    raise Fastr::Error.new("Controller and action not present in route") if controller.nil? or action.nil?
    
    
    klass = "#{controller.capitalize}Controller"
    
    log.info "Routing to controller: #{klass}, action: #{action}"
    
    obj = Module.const_get(klass).new
    obj.env = env
    
    ret = obj.send(action)
    
    #[200, {}, "ok"]
    ret
  else
    [404, {"Content-Type" => "text/plain"}, "404 Not Found: #{path}"]
  end
end