Sha256: 57eae8e8a12d64a6a73854d9b88eec8dda03b037d84d788a693416dfe90b8e8e

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

# encoding: utf-8

require 'has_guarded_handlers'

module Adhearsion
  class Router
    class Route
      include HasGuardedHandlers

      attr_reader :name, :target, :guards

      def initialize(name, target = nil, *guards, &block)
        @name = name
        if block
          @target, @guards = block, ([target] + guards)
        else
          @target, @guards = target, guards
        end
        @guards.compact!
      end

      def match?(call)
        !guarded? guards, call
      end

      def dispatch(call, callback = nil)
        Adhearsion::Events.trigger_immediately :call_routed, call: call, route: self

        controller = if target.respond_to?(:call)
          CallController.new call, controller_metadata, &target
        else
          target.new call, controller_metadata
        end

        call.accept if accepting?

        call.execute_controller controller, lambda { |call_actor|
          begin
            if call_actor[:ahn_prevent_hangup]
              logger.info "Call routing completed, keeping the call alive at controller/router request."
            else
              call_actor.hangup
            end
          rescue Call::Hangup
          end
          callback.call if callback
        }
      end

      def controller_metadata=(metadata)
        @controller_metadata = metadata
      end

      def controller_metadata
        return {} unless instance_variable_defined?(:@controller_metadata)
        @controller_metadata
      end

      def evented?
        false
      end

      def accepting?
        true
      end

      def openended?
        false
      end

      def inspect
        "#<#{self.class}:#{object_id} name=#{name} target=#{target} guards=#{guards}>"
      end
      alias :to_s :inspect
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adhearsion-2.2.0 lib/adhearsion/router/route.rb