Sha256: 6cc0759681a16e7aa8cbe5986bea8828420a93cc8c62d98f42aa817b7c5f982f

Contents?: true

Size: 1.28 KB

Versions: 16

Compression:

Stored size: 1.28 KB

Contents

module Adhearsion
  module VoIP
    module CallRouting
      class Rule
        attr_reader :patterns, :providers, :options

        def initialize(*args, &block)
          @options   = args.pop
          @patterns  = Array(args)
          @providers = Array(options[:to])
        end
      end

      class RuleSet < Array
        def [](index)
          case index
          when String
            detect do |rule|
              rule.patterns.any? do |pattern|
                index =~ pattern
              end
            end
          else
            super
          end
        end
      end

      class Router
        class << self
          attr_accessor :rules

          def define(&block)
            new.tap do |router|
              router.define(&block)
              rules.concat router.rules
            end
          end

          def calculate_route_for(end_point)
            if rule = rules[end_point.to_s]
              rule.providers
            end
          end
        end

        self.rules ||= RuleSet.new

        attr_reader :rules
        def initialize
          @rules = []
        end

        def define(&block)
          instance_eval(&block)
        end

        def route(*args, &block)
          rules << Rule.new(*args, &block)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
adhearsion-1.2.6 lib/adhearsion/voip/call_routing.rb
adhearsion-1.2.5 lib/adhearsion/voip/call_routing.rb
adhearsion-1.2.4 lib/adhearsion/voip/call_routing.rb
adhearsion-1.2.3 lib/adhearsion/voip/call_routing.rb
adhearsion-1.2.1 lib/adhearsion/voip/call_routing.rb
adhearsion-1.2.0 lib/adhearsion/voip/call_routing.rb
adhearsion-1.1.1 lib/adhearsion/voip/call_routing.rb
adhearsion-1.1.0 lib/adhearsion/voip/call_routing.rb
adhearsion-1.0.3 lib/adhearsion/voip/call_routing.rb
adhearsion-1.0.2 lib/adhearsion/voip/call_routing.rb
adhearsion-cw-1.0.2.3 lib/adhearsion/voip/call_routing.rb
adhearsion-cw-1.0.2.2 lib/adhearsion/voip/call_routing.rb
adhearsion-cw-1.0.2.1 lib/adhearsion/voip/call_routing.rb
adhearsion-1.0.1 lib/adhearsion/voip/call_routing.rb
adhearsion-1.0.0 lib/adhearsion/voip/call_routing.rb
rene-adhearsion-0.8.6 lib/adhearsion/voip/call_routing.rb