Method: Halcyon::Application#dispatch

Defined in:
lib/halcyon/application.rb

#dispatch(env) ⇒ Object

Dispatches the controller and action according the routed request.

+env+ the request environment details, including "halcyon.route"

If no :controller is specified, the default Application controller is dispatched to.

Once the controller is selected and instantiated, the action is called, defaulting to :default if no action is provided.

If the action called is not defined, a 404 Not Found exception will be raised. This will be sent to the client as such, or handled by the Rack application container, such as the Rack Cascade middleware to failover to another application (such as Merb or Rails).

Refer to Halcyon::Application::Router for more details on defining routes and for where to get further documentation.

Returns (String|Array|Hash):body

Raises:

  • (NotFound)


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/halcyon/application.rb', line 160

def dispatch(env)
  route = env['halcyon.route']
  # make sure that the right controller/action is called based on the route
  controller = case route[:controller]
  when NilClass
    # default to the Application controller
    ::Application.new(env)
  when String
    # pulled from URL, so camelize (from extlib) and symbolize first
    begin
      Object.full_const_get(route[:controller].to_const_string).new(env)
    rescue NameError => e
      raise NotFound.new
    end
  end
  
  # Establish the selected action, defaulting to +default+.
  action = (route[:action] || 'default').to_sym
  
  # Respond correctly that a non-existent action was specified if the
  # method does not exist.
  raise NotFound.new unless controller.methods.include?(action.to_s)
  
  # if no errors have occured up to this point, the route should be fully
  # valid and all exceptions raised should be treated as
  # <tt>500 Internal Server Error</tt>s, which is handled by <tt>call</tt>.
  controller._dispatch(action)
end