class Webmachine::Dispatcher

Handles dispatching incoming requests to the proper registered resources and initializing the decision logic.

Constants

WM_DISPATCH

Attributes

resource_creator[RW]

The creator for resources used to process requests. Must respond to call(route, request, response) and return a newly created resource instance.

routes[R]

@return [Array<Route>] the list of routes that will be

dispatched to

@see add_route

Public Class Methods

new(resource_creator = method(:create_resource)) click to toggle source

Initialize a Dispatcher instance @param resource_creator Invoked to create resource instances.

# File lib/webmachine/dispatcher.rb, line 23
def initialize(resource_creator = method(:create_resource))
  @routes = []
  @resource_creator = resource_creator
end

Public Instance Methods

add(*args, &block)
Alias for: add_route
add_route(*args, &block) click to toggle source

Adds a route to the dispatch list. Routes will be matched in the order they are added. @see Route#new

# File lib/webmachine/dispatcher.rb, line 31
def add_route(*args, &block)
  route = Route.new(*args, &block)
  @routes << route
  route
end
Also aliased as: add
dispatch(request, response) click to toggle source

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. @param [Request] request the request object @param [Response] response the response object

# File lib/webmachine/dispatcher.rb, line 43
def dispatch(request, response)
  if 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
  else
    Webmachine.render_error(404, request, response)
  end
end
find_resource(request, response) click to toggle source

Find the first resource that matches an incoming request @param [Request] request the request to match @param [Response] response the response for the resource

# File lib/webmachine/dispatcher.rb, line 66
def find_resource(request, response)
  if route = find_route(request)
    prepare_resource(route, request, response)
  end
end
find_route(request) click to toggle source

Find the first route that matches an incoming request @param [Request] request the request to match

# File lib/webmachine/dispatcher.rb, line 74
def find_route(request)
  @routes.find { |r| r.match?(request) }
end
reset() click to toggle source

Resets, removing all routes. Useful for testing or reloading the application.

# File lib/webmachine/dispatcher.rb, line 59
def reset
  @routes.clear
end

Private Instance Methods

create_resource(route, request, response) click to toggle source
# File lib/webmachine/dispatcher.rb, line 85
def create_resource(route, request, response)
  route.resource.new(request, response)
end
prepare_resource(route, request, response) click to toggle source
# File lib/webmachine/dispatcher.rb, line 80
def prepare_resource(route, request, response)
  route.apply(request)
  @resource_creator.call(route, request, response)
end