Class: Adhearsion::VoIP::DSL::Dialplan::CommandDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/voip/dsl/dialplan/dispatcher.rb

Overview

Serves as a EventCommand proxy for handling EventCommands. Useful if doing any pre-processing.

For example, when sending commands over the event socket to FreeSwitch, every command must start with “api”, but, when doing an originate, it must begin with an ampersand. These two pre-processors would be developed as separate CommandDispatchers.

Direct Known Subclasses

FreeSwitch::OesServer::OesDispatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory, context = nil) ⇒ CommandDispatcher

Returns a new instance of CommandDispatcher.



64
65
66
67
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 64

def initialize(factory, context=nil)
  @context = context
  @factory = factory.new context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 73

def method_missing(name, *args, &block)
  *commands = *@factory.send(name, *args, &block)
  commands.map do |command|
    if command.kind_of? Proc
      instance_eval(&command)
    elsif command.kind_of? EventCommand
      dispatched_command = dispatch! command
      if command.response_block
        while (new_cmd = command.response_block.call(dispatched_command)).kind_of? EventCommand
          dispatched_command = dispatch! new_cmd
        end
      end
      dispatched_command
    else
      command 
    end
  end.last
rescue ReturnValue => r
  return r.obj
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



62
63
64
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 62

def context
  @context
end

#factoryObject (readonly)

Returns the value of attribute factory.



62
63
64
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 62

def factory
  @factory
end

Instance Method Details

#break!(uuid = @context) ⇒ Object

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 98

def break!(uuid=@context)
  raise NotImplementedError, "Must subclass #{self.class} and override this!"
end

#cloneObject



110
111
112
113
114
115
116
117
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 110

def clone
  returning super do |nemesis|
    instance_variables.each do |iv|
      value = instance_variable_get(iv)
      nemesis.instance_variable_set iv, value.clone if value
    end
  end
end

#def_keys!(hash) ⇒ Object

Takes a Hash and meta_def()‘s a method for each key that returns the key’s value in the Hash.



104
105
106
107
108
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 104

def def_keys!(hash)
  hash.each_pair do |k,v|
    meta_def(k) { v } rescue nil
  end
end

#dispatch!(event_command) ⇒ Object

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 69

def dispatch!(event_command)
  raise NotImplementedError, "Must subclass #{self.class} and override this!"
end

#return!(obj) ⇒ Object



94
95
96
# File 'lib/adhearsion/voip/dsl/dialplan/dispatcher.rb', line 94

def return!(obj)
  raise DSL::Dialplan::ReturnValue.new(obj)
end