class Rory::Dispatcher
The dispatcher takes care of sending an incoming request to the appropriate controller, after examining the routes.
Attributes
request[R]
Public Class Methods
new(rack_request, app = nil)
click to toggle source
# File lib/rory/dispatcher.rb, line 6 def initialize(rack_request, app = nil) @request = rack_request @routing = {} @app = app end
rack_app(app)
click to toggle source
# File lib/rory/dispatcher.rb, line 12 def self.rack_app(app) Proc.new { |env| new(Rory::Request.new(env), app).dispatch } end
Public Instance Methods
dispatch()
click to toggle source
# File lib/rory/dispatcher.rb, line 50 def dispatch if controller controller.present else render_not_found end end
extension()
click to toggle source
# File lib/rory/dispatcher.rb, line 26 def extension File.extname(full_path)[1..-1] end
full_path()
click to toggle source
# File lib/rory/dispatcher.rb, line 34 def full_path @request.path_info[1..-1] || '' end
json_requested?()
click to toggle source
# File lib/rory/dispatcher.rb, line 30 def json_requested? extension == 'json' end
method()
click to toggle source
# File lib/rory/dispatcher.rb, line 42 def method override_method || request.request_method.downcase end
override_method()
click to toggle source
# File lib/rory/dispatcher.rb, line 18 def override_method requested_override = request.params['_method'] return nil unless requested_override if ['put', 'patch', 'delete'].include?(requested_override.downcase) requested_override.downcase end end
path_without_extension()
click to toggle source
# File lib/rory/dispatcher.rb, line 38 def path_without_extension full_path.gsub(/(.*)\.#{extension}$/, '\1') end
redirect(path = '/')
click to toggle source
# File lib/rory/dispatcher.rb, line 58 def redirect(path = '/') unless path =~ /\:\/\// path = "#{@request.scheme}://#{@request.host_with_port}#{path}" end return [ 302, {'Content-type' => 'text/html', 'Location'=> path }, ['Redirecting...'] ] end
render_not_found()
click to toggle source
# File lib/rory/dispatcher.rb, line 65 def render_not_found return [ 404, {'Content-type' => 'text/html' }, ['Four, oh, four.'] ] end
route()
click to toggle source
# File lib/rory/dispatcher.rb, line 46 def route @routing[:route] ||= get_route end
Private Instance Methods
all_routes()
click to toggle source
# File lib/rory/dispatcher.rb, line 99 def all_routes @app ? @app.routes : [] end
controller()
click to toggle source
# File lib/rory/dispatcher.rb, line 71 def controller if klass = controller_class @routing.merge!(:dispatcher => self) klass.new(request, @routing, @app) end end
controller_class()
click to toggle source
# File lib/rory/dispatcher.rb, line 78 def controller_class if route controller_name = Rory::Support.camelize("#{route.controller}_controller") if route.module controller_name.prepend "#{Rory::Support.camelize("#{route.module}")}/" end Rory::Support.constantize(controller_name) end end
get_route()
click to toggle source
# File lib/rory/dispatcher.rb, line 88 def get_route mapped_route = all_routes.detect do |route| route.matches_request?(path_without_extension, method) end if mapped_route @request.params.delete('_method') @request.params.merge! mapped_route.path_params(path_without_extension) end mapped_route end