Class: Ramaze::Dispatcher
- Extended by:
- Trinity
- Defined in:
- lib/ramaze/dispatcher.rb,
lib/ramaze/dispatcher/file.rb,
lib/ramaze/dispatcher/error.rb,
lib/ramaze/contrib/profiling.rb,
lib/ramaze/dispatcher/action.rb,
lib/ramaze/dispatcher/directory.rb
Overview
The Dispatcher receives requests from adapters and sets up the proper environment to process them and respond.
Defined Under Namespace
Classes: Action, ActionProfiler, Directory, Error, File
Constant Summary collapse
- FILTER =
requests are passed to every
OrderedSet[ Dispatcher::File, Dispatcher::Action ]
Class Method Summary collapse
-
.call(env = nil) ⇒ Object
(also: handle)
Entry point for Adapter#respond, takes a Rack::Request and Rack::Response, sets up the environment and the goes on to dispatch for the given path from rack_request.
-
.dispatch(path) ⇒ Object
filters the path until a response or redirect is thrown or a filter is successful, builds a response from the returned hash in case of throws.
-
.dispatch_to(path) ⇒ Object
protects against recursive dispatch and reassigns the path_info in the request, the rest of the request is kept intact.
- .error(obj, meta = {}) ⇒ Object
-
.filter(path) ⇒ Object
Calls .call(path) on every object in Dispatcher::FILTER until one returns something else than false/nil.
-
.general_dispatch(path) ⇒ Object
splits up the dispatch based on Global.shield.
-
.shielded_dispatch(path) ⇒ Object
Protects somewhat against hammering of erroring paths, since error-pages take a while to build in the default mode it is possible to decrease overall speed quite a bit by hitting Ramaze with such paths.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
Methods included from StateAccessor
each, #state_accessor, #state_reader, #state_writer
Constructor Details
#initialize(*args) ⇒ Dispatcher
Returns a new instance of Dispatcher.
18 19 20 |
# File 'lib/ramaze/dispatcher.rb', line 18 def initialize(*args) Dispatcher.call(*args) end |
Class Method Details
.call(env = nil) ⇒ Object Also known as: handle
Entry point for Adapter#respond, takes a Rack::Request and Rack::Response, sets up the environment and the goes on to dispatch for the given path from rack_request.
env
will be ignored, it’s just for compatibility with rack middleware
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ramaze/dispatcher.rb', line 36 def call(env = nil) path = request.path_info.squeeze('/') path.sub!(/^#{Regexp.escape(Global.prefix)}/, '/') path.squeeze!('/') if new_path = Rewrite.resolve(path) path = new_path Log.dev("Rewriting `#{path}' to `#{path}'") end case path when *Global.ignore unless ::File.exist?(Dispatcher::File.resolve_path(path)) return response.build(Global.ignore_body, Global.ignore_status) end end general_dispatch path rescue Object => exception error(exception) end |
.dispatch(path) ⇒ Object
filters the path until a response or redirect is thrown or a filter is successful, builds a response from the returned hash in case of throws.
106 107 108 109 110 111 112 113 114 |
# File 'lib/ramaze/dispatcher.rb', line 106 def dispatch(path) catch(:respond){ redirected = catch(:redirect){ filter(path) throw(:respond) } response.build(*redirected) } end |
.dispatch_to(path) ⇒ Object
protects against recursive dispatch and reassigns the path_info in the request, the rest of the request is kept intact.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ramaze/dispatcher.rb', line 61 def dispatch_to(path) if request.path_info == path if error = STATE[:exception] raise error else raise "Recursive redirect from #{path} to #{path}" end end request.path_info = path general_dispatch path end |
.error(obj, meta = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ramaze/dispatcher.rb', line 129 def error(obj, = {}) controller = STATE[:controller] [:controller] ||= controller if controller if Global.error_page Dispatcher::Error.call(obj, ) else = "No action for '#{[:path]}'" << " on '#{Rack::Utils.escape_html(controller)}'" if controller raise(obj) if obj.respond_to?(:message) raise(Ramaze::Error, ) end end |
.filter(path) ⇒ Object
Calls .call(path) on every object in Dispatcher::FILTER until one returns something else than false/nil.
119 120 121 122 123 124 125 126 127 |
# File 'lib/ramaze/dispatcher.rb', line 119 def filter(path) result = nil FILTER.each do |dispatcher| result = dispatcher.call(path) return result if result and not result.respond_to?(:exception) end error(result, :path => path) end |
.general_dispatch(path) ⇒ Object
splits up the dispatch based on Global.shield
74 75 76 77 78 79 80 |
# File 'lib/ramaze/dispatcher.rb', line 74 def general_dispatch(path) if Global.shield shielded_dispatch path else dispatch path end end |
.shielded_dispatch(path) ⇒ Object
Protects somewhat against hammering of erroring paths, since error-pages take a while to build in the default mode it is possible to decrease overall speed quite a bit by hitting Ramaze with such paths. shielded_dispatch checks the path and caches erronous responses so they won’t be recreated but simply pushed out again without stopping at the Controller. Please note that this is just minor protection and the best option is to create a performant error-page instead.
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ramaze/dispatcher.rb', line 90 def shielded_dispatch(path) shield_cache = Cache.shield handled = shield_cache[path] return handled if handled dispatched = dispatch(path) unless trait[:shielded].include?(dispatched.status) dispatched else shield_cache[path] = dispatched end end |