Class: Regal::Route

Inherits:
Object
  • Object
show all
Extended by:
RoutesDsl
Defined in:
lib/regal/app.rb

Overview

A route is an application, or a part of an application

Instance Method Summary collapse

Methods included from RoutesDsl

after, any, before, delete, get, head, mount, options, patch, post, put, rescue_from, route, scope

Constructor Details

#initialize(attributes) ⇒ Route

Create a new application with this route as its root

Parameters:



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/regal/app.rb', line 317

def initialize(attributes)
  @attributes = attributes.dup.freeze
  @name = self.class.name
  @befores = self.class.befores
  @afters = self.class.afters
  @rescuers = self.class.rescuers
  @handlers = self.class.handlers
  @routes = self.class.create_routes(attributes)
  @route = self
  freeze
end

Instance Method Details

#call(env) ⇒ Array<(Integer, Hash, Enumerable)>

Route and handle a request

Parameters:

  • env (Hash)

Returns:

  • (Array<(Integer, Hash, Enumerable)>)


333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/regal/app.rb', line 333

def call(env)
  path_components = path_components = env[PATH_INFO_KEY].split(SLASH).drop(1)
  parent_routes, path_captures = match_route(path_components)
  matching_route = parent_routes.last
  request_method = env[REQUEST_METHOD_KEY]
  if matching_route && matching_route.can_handle?(request_method)
    request = Request.new(env, path_captures, @attributes)
    response = Response.new
    finishing_route = run_befores(parent_routes, request, response)
    if finishing_route.nil? && !response.finished?
      begin
        result = matching_route.handle(request_method, request, response)
        unless response.finished?
          response.body = result
        end
      rescue => e
        finishing_route = handle_error(parent_routes, finishing_route, e, request, response)
      end
    end
    run_afters(parent_routes, finishing_route, request, response)
    if no_body_response?(request_method, response)
      response.no_body
    end
    response
  elsif matching_route
    METHOD_NOT_ALLOWED_RESPONSE
  else
    NOT_FOUND_RESPONSE
  end
end