lib/adhearsion/router.rb in adhearsion-2.0.1 vs lib/adhearsion/router.rb in adhearsion-2.1.0

- old
+ new

@@ -2,12 +2,17 @@ module Adhearsion class Router extend ActiveSupport::Autoload + autoload :EventedRoute + autoload :OpenendedRoute autoload :Route + autoload :UnacceptingRoute + NoMatchError = Class.new Adhearsion::Error + attr_reader :routes def initialize(&block) @routes = [] instance_exec(&block) @@ -22,11 +27,47 @@ def match(call) @routes.find { |route| route.match? call } end def handle(call) - return unless route = match(call) + raise NoMatchError unless route = match(call) logger.info "Call #{call.id} selected route \"#{route.name}\" (#{route.target})" - route.dispatcher + route.dispatch call + rescue NoMatchError + logger.warn "Call #{call.id} could not find a matching route. Rejecting." + call.reject :error + end + + module Filters + def evented(&block) + filtered_routes EventedRoute, &block + end + + def unaccepting(&block) + filtered_routes UnacceptingRoute, &block + end + + def openended(&block) + filtered_routes OpenendedRoute, &block + end + + def filtered_routes(mixin, &block) + FilteredRouter.new(self, mixin).instance_exec(&block) + end + end + + include Filters + + class FilteredRouter < SimpleDelegator + include Filters + + def initialize(delegate, mixin) + super delegate + @mixin = mixin + end + + def route(*args, &block) + super.tap { |r| r.extend @mixin } + end end end end