Class: Webmachine::Dispatcher

Inherits:
Object
  • Object
show all
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: Route

Constant Summary collapse

WM_DISPATCH =
'wm.dispatch'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_creator = method(:create_resource)) ⇒ Dispatcher

Initialize a Dispatcher instance

Parameters:

  • resource_creator (defaults to: method(:create_resource))

    Invoked to create resource instances.


23
24
25
26
# File 'lib/webmachine/dispatcher.rb', line 23

def initialize(resource_creator = method(:create_resource))
  @routes = []
  @resource_creator = resource_creator
end

Instance Attribute Details

#resource_creatorObject

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


19
20
21
# File 'lib/webmachine/dispatcher.rb', line 19

def resource_creator
  @resource_creator
end

#routesArray<Route> (readonly)

Returns the list of routes that will be dispatched to.

Returns:

  • (Array<Route>)

    the list of routes that will be dispatched to

See Also:


14
15
16
# File 'lib/webmachine/dispatcher.rb', line 14

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.

See Also:

  • Route#new

31
32
33
34
35
# File 'lib/webmachine/dispatcher.rb', line 31

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.

Parameters:

  • request (Request)

    the request object

  • response (Response)

    the response object


43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object

Find the first resource that matches an incoming request

Parameters:

  • request (Request)

    the request to match

  • response (Response)

    the response for the resource


66
67
68
69
70
# 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) ⇒ Object

Find the first route that matches an incoming request

Parameters:

  • request (Request)

    the request to match


74
75
76
# File 'lib/webmachine/dispatcher.rb', line 74

def find_route(request)
  @routes.find {|r| r.match?(request) }
end

#resetObject

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


59
60
61
# File 'lib/webmachine/dispatcher.rb', line 59

def reset
  @routes.clear
end