Class: Adhearsion::Router

Inherits:
Object show all
Includes:
Filters
Defined in:
lib/adhearsion/router.rb,
lib/adhearsion/router/route.rb,
lib/adhearsion/router/evented_route.rb,
lib/adhearsion/router/openended_route.rb,
lib/adhearsion/router/unaccepting_route.rb

Defined Under Namespace

Modules: EventedRoute, Filters, OpenendedRoute, UnacceptingRoute Classes: FilteredRouter, Route

Constant Summary collapse

NoMatchError =
Class.new Adhearsion::Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filters

#evented, #filtered_routes, #openended, #unaccepting

Constructor Details

#initialize(&block) ⇒ Router

Returns a new instance of Router.



16
17
18
19
# File 'lib/adhearsion/router.rb', line 16

def initialize(&block)
  @routes = []
  instance_exec(&block)
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



14
15
16
# File 'lib/adhearsion/router.rb', line 14

def routes
  @routes
end

Instance Method Details

#handle(call) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/adhearsion/router.rb', line 31

def handle(call)
  raise NoMatchError unless route = match(call)
  logger.info "Call #{call.id} selected route \"#{route.name}\" (#{route.target})"
  route.dispatch call
rescue NoMatchError
  logger.warn "Call #{call.id} could not find a matching route. Rejecting."
  call.reject :error
end

#match(call) ⇒ Object



27
28
29
# File 'lib/adhearsion/router.rb', line 27

def match(call)
  @routes.find { |route| route.match? call }
end

#route(*args, &block) ⇒ Object



21
22
23
24
25
# File 'lib/adhearsion/router.rb', line 21

def route(*args, &block)
  Route.new(*args, &block).tap do |route|
    @routes << route
  end
end