Class: Shaf::Router

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

Defined Under Namespace

Classes: MethodNotAllowedResponder, NullController

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = NullController.new) ⇒ Router

Returns a new instance of Router.



104
105
106
# File 'lib/shaf/router.rb', line 104

def initialize(app = NullController.new)
  @app = app
end

Class Method Details

.default_controllerObject

This controller will be used when no other can handle the request (E.g. returning 404 Not Found)



79
80
81
# File 'lib/shaf/router.rb', line 79

def default_controller
  @default_controller ||= nil
end

.mount(controller, default: false) ⇒ Object



68
69
70
71
# File 'lib/shaf/router.rb', line 68

def mount(controller, default: false)
  @default_controller = controller if default
  controllers << controller
end

.routesObject



73
74
75
# File 'lib/shaf/router.rb', line 73

def routes
  @routes ||= init_routes
end

Instance Method Details

#call(env) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/shaf/router.rb', line 108

def call(env)
  # When the api is mounted in Rails then the mount point will be not be
  # present in PATH_INFO but it will instead be available in SCRIPT_NAME
  # Shaf need to know about the full path in order to make all path helpers
  # work, so we need to add the mountpoint back to PATH_INFO.
  unless String(env['SCRIPT_NAME']).empty?
    env['PATH_INFO'] = '' if env['PATH_INFO'] == '/'
    env['PATH_INFO'] = "#{env['SCRIPT_NAME']}#{env['PATH_INFO']}"
  end

  http_method, path = http_details(env)

  result = nil

  each_controller_for(http_method, path) do |controller|
    result = controller.call(env)
    break unless cascade? result
  end

  result
end