Class: RocketIO::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/rocketio/application.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Application

Returns a new instance of Application.



4
5
6
7
8
9
10
# File 'lib/rocketio/application.rb', line 4

def initialize *args, &block
  controllers = args.empty? ? RocketIO.controllers : args.flatten
  if block
    controllers.each {|c| c.class_exec {private define_method(:__run__, block)}}
  end
  @router = Router.new(*controllers)
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rocketio/application.rb', line 12

def call env
  catch :__response__ do # catch 404 errors and potential `halt` calls from middleware
    controller, method, path_params = @router.resolve_path(env[PATH_INFO])

    unless controller
      controller = Controller.new
      controller.instance_variable_set(:@__env__, env)
      return controller.error(404)
    end

    controller = controller.new(method, path_params)
    chain = controller.middleware.reverse.inject(controller) {|app,ware| ware.call(app)}
    if controller.sessions
      chain = controller.sessions[0].new(chain, controller.sessions[1])
    end
    chain.call(env)
  end
end