Sha256: 18f08c4df6ee62e544fef85c407fd8ba8943ea1389afa4329cb83ee353705919

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 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)
            returning new 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

3 entries across 3 versions & 1 rubygems

Version Path
adhearsion-0.8.6 lib/adhearsion/voip/call_routing.rb
adhearsion-0.8.5 lib/adhearsion/voip/call_routing.rb
adhearsion-0.8.4 lib/adhearsion/voip/call_routing.rb