Class: Racket::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/racket/router.rb

Overview

Handles routing in Racket applications.

Defined Under Namespace

Classes: Route

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Router

Returns a new instance of Router.



59
60
61
62
63
# File 'lib/racket/router.rb', line 59

def initialize(options)
  @options = OpenStruct.new(options)
  @router = HttpRouter.new
  @routes = {}
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



38
39
40
# File 'lib/racket/router.rb', line 38

def routes
  @routes
end

Class Method Details

.service(_options = {}) ⇒ Proc

Returns a service proc that can be used by the registry.

Parameters:

  • _options (Hash) (defaults to: {})

    (unused)

Returns:

  • (Proc)


44
45
46
47
48
49
50
51
52
# File 'lib/racket/router.rb', line 44

def self.service(_options = {})
  lambda do |reg|
    new(
      action_cache: reg.action_cache,
      dev_mode: reg.application_settings.mode == :dev,
      logger: reg.application_logger
    )
  end
end

Instance Method Details

#action_cacheRacket::Utils::Routing::ActionCache



55
56
57
# File 'lib/racket/router.rb', line 55

def action_cache
  @options.action_cache
end

#get_route(controller_class, action, params) ⇒ String

Returns a route to the specified controller/action/parameter combination.

Parameters:

  • controller_class (Class)
  • action (Symbol)
  • params (Array)

Returns:

  • (String)


71
72
73
74
75
# File 'lib/racket/router.rb', line 71

def get_route(controller_class, action, params)
  raise "Cannot find controller #{controller_class}" unless @routes.key?(controller_class)
  params.flatten!
  Route.new(@routes[controller_class], action, params).to_s
end

#map(path, controller_class) ⇒ nil

Maps a controller to the specified path.

Parameters:

  • path (String)
  • controller_class (Class)

Returns:

  • (nil)


82
83
84
85
86
# File 'lib/racket/router.rb', line 82

def map(path, controller_class)
  map_controller(path.empty? ? '/' : path, controller_class)
  @router.add("#{path}(/*params)").to(controller_class)
  action_cache.add(controller_class)
end

#render_error(status, error = nil) ⇒ Object

@todo: Allow the user to set custom handlers for different errors



89
90
91
92
93
94
95
96
97
# File 'lib/racket/router.rb', line 89

def render_error(status, error = nil)
  if (error)
    # If running in dev mode, let Rack::ShowExceptions handle the error.
    raise error if @options.dev_mode
    # Not running in dev mode, let us handle the error ourselves.
    $stderr.write("#{error.class}: #{error.message}\n#{error.backtrace.map { |l| "\t#{l}" }.join("\n")}\n\n")
  end
  Response.generate_error_response(status)
end

#route(env) ⇒ Array

Routes a request and renders it.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Array)

    A Rack response triplet



103
104
105
106
107
108
109
110
111
# File 'lib/racket/router.rb', line 103

def route(env)
  catch :response do # Catches early exits from Controller.respond.
    # Ensure that that a controller will respond to the request. If not, send a 404.
    return render_error(404) unless (target_info = target_info(env))
    Racket::Utils::Routing::Dispatcher.new(env, target_info).dispatch
  end
rescue => err
  render_error(500, err)
end