Class: Webmachine::Dispatcher
- Inherits:
-
Object
- Object
- Webmachine::Dispatcher
- Defined in:
- lib/webmachine/dispatcher.rb,
lib/webmachine/dispatcher/route.rb
Overview
Handles dispatching incoming requests to the proper registered resources and initializing the decision logic.
Defined Under Namespace
Classes: NotFoundResource, Route
Instance Attribute Summary collapse
-
#resource_creator ⇒ Object
The creator for resources used to process requests.
-
#routes ⇒ Array<Route>
readonly
The list of routes that will be dispatched to.
Instance Method Summary collapse
-
#add_route(*args, &block) ⇒ Object
(also: #add)
Adds a route to the dispatch list.
-
#dispatch(request, response) ⇒ Object
Dispatches a request to the appropriate Resource in the dispatch list.
-
#find_resource(request, response) ⇒ Object
Find the first resource that matches an incoming request.
-
#find_route(request) ⇒ Object
Find the first route that matches an incoming request.
-
#initialize(resource_creator = method(:create_resource)) ⇒ Dispatcher
constructor
Initialize a Dispatcher instance.
-
#reset ⇒ Object
Resets, removing all routes.
Constructor Details
#initialize(resource_creator = method(:create_resource)) ⇒ Dispatcher
Initialize a Dispatcher instance
22 23 24 25 |
# File 'lib/webmachine/dispatcher.rb', line 22 def initialize(resource_creator = method(:create_resource)) @routes = [] @resource_creator = resource_creator end |
Instance Attribute Details
#resource_creator ⇒ Object
The creator for resources used to process requests. Must respond to call(route, request, response) and return a newly created resource instance.
18 19 20 |
# File 'lib/webmachine/dispatcher.rb', line 18 def resource_creator @resource_creator end |
#routes ⇒ Array<Route> (readonly)
Returns the list of routes that will be dispatched to.
13 14 15 |
# File 'lib/webmachine/dispatcher.rb', line 13 def routes @routes end |
Instance Method Details
#add_route(*args, &block) ⇒ Object Also known as: add
Adds a route to the dispatch list. Routes will be matched in the order they are added.
30 31 32 33 34 |
# File 'lib/webmachine/dispatcher.rb', line 30 def add_route(*args, &block) route = Route.new(*args, &block) @routes << route route end |
#dispatch(request, response) ⇒ Object
Dispatches a request to the appropriate Resource in the dispatch list. If a matching resource is not found, a “404 Not Found” will be rendered.
42 43 44 45 46 47 48 49 50 |
# File 'lib/webmachine/dispatcher.rb', line 42 def dispatch(request, response) resource = find_resource(request, response) Webmachine::Events.instrument('wm.dispatch') do |payload| Webmachine::Decision::FSM.new(resource, request, response).run payload[:resource] = resource.class.name payload[:request] = request.dup payload[:code] = response.code end end |
#find_resource(request, response) ⇒ Object
Find the first resource that matches an incoming request
61 62 63 64 65 66 67 |
# File 'lib/webmachine/dispatcher.rb', line 61 def find_resource(request, response) if route = find_route(request) prepare_resource(route, request, response) else NotFoundResource.new(request, response) end end |
#find_route(request) ⇒ Object
Find the first route that matches an incoming request
71 72 73 |
# File 'lib/webmachine/dispatcher.rb', line 71 def find_route(request) @routes.find {|r| r.match?(request) } end |
#reset ⇒ Object
Resets, removing all routes. Useful for testing or reloading the application.
54 55 56 |
# File 'lib/webmachine/dispatcher.rb', line 54 def reset @routes.clear end |