Class: Wayfarer::Middleware::Router

Inherits:
Object
  • Object
show all
Extended by:
Base
Defined in:
lib/wayfarer/middleware/router.rb

Defined Under Namespace

Modules: API

Constant Summary

Constants included from Base

Base::API_MODULE

Instance Method Summary collapse

Methods included from Base

api, lazy

Instance Method Details

#call(task) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wayfarer/middleware/router.rb', line 33

def call(task)
  # Avoid rerouting when dispatching a [Controller, :action] pair
  if (action = task[:action])
    log(:already_routed, task, action: action)

    return (yield if block_given?)
  end

  case result = task[:controller].class.route.invoke(task[:uri])
  when Routing::Result::Mismatch then return log(:mismatch, task)
  when Routing::Result::Match
    action = result.action

    log(:match, task, action: action.inspect)

    task[:action] = action
    task[:params] ||= ActiveSupport::HashWithIndifferentAccess.new
    task[:params].merge!(result.params)
  end

  yield if block_given?
end