class Racket::Utils::Routing::Dispatcher

Class responsible for dispatching requests to controllers.

Public Class Methods

extract_target(response) click to toggle source

Extracts the target class, target params and target action from a list of valid routes.

@param [HttpRouter::Response] response @return [Array]

# File lib/racket/utils/routing.rb, line 87
def self.extract_target(response)
  target_klass = response.route.dest
  params = response.param_values.first.reject(&:empty?)
  action =
    if params.empty?
      target_klass.settings.fetch(:default_action)
    else
      params.shift.to_sym
    end
  [target_klass, params, action]
end
new(env, target_info) click to toggle source

Constructor

@param [Hash] env @param [Array] target_info

# File lib/racket/utils/routing.rb, line 103
def initialize(env, target_info)
  @env = env
  @controller_class, @params, @action = target_info
end

Public Instance Methods

dispatch() click to toggle source

Dispatches request to a controller. This is the default action whenever a matching route for a request is found.

@return [Array] A racket response triplet

# File lib/racket/utils/routing.rb, line 112
def dispatch
  # Rewrite PATH_INFO to reflect that we split out the parameters
  update_path_info(@params.length)

  # Call controller
  call_controller(Current.init(RouterParams.new(@action, @params, @env)))
end

Private Instance Methods

call_controller(mod) click to toggle source
# File lib/racket/utils/routing.rb, line 122
def call_controller(mod)
  @controller_class.new.extend(mod).__run
end
update_path_info(num_params) click to toggle source

Updates the PATH_INFO environment variable.

@param [Fixnum] num_params @return [nil]

# File lib/racket/utils/routing.rb, line 130
def update_path_info(num_params)
  @env['PATH_INFO'] = @env['PATH_INFO']
                      .split('/')[0...-num_params]
                      .join('/') unless num_params.zero?
  nil
end