Class: Adhearsion::VoIP::FreeSwitch::FreeSwitchDialplanCommandFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ FreeSwitchDialplanCommandFactory

Returns a new instance of FreeSwitchDialplanCommandFactory.



8
9
10
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 8

def initialize(context=nil)
  @context = context
end

Instance Method Details

#hangup!Object



56
57
58
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 56

def hangup!
  cmd "exit"
end

#input(number = nil, hash = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 103

def input(number=nil, hash={})
  timeout, file = hash[:timeout], hash[:play] || hash[:file]
  break_on = hash[:break_on] || '#'

  # TODO: compile play() and set its DTMF callback to this one
  digits = []
  dtmf_hook = lambda do |digit|
    puts "RECEIVED #{digit} WITH #{digits}"
    return! digits.to_s if digit.to_s == break_on.to_s
    digits << digit
    return! digits.to_s if number && digits.size >= number
  end
  DSL::Dialplan::NoOpEventCommand.new.tap do |command|
    command.on_keypress &dtmf_hook
  end
end

#join(id) ⇒ Object



52
53
54
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 52

def join(id)
  DSL::Dialplan::ExitingEventCommand.new "conference", id.to_s
end

#play(*files) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 31

def play(*files)
  hash = files.last.kind_of?(Hash) ? files.pop : {}
  conference, to = hash[:conference], hash[:to]
  puts "conference: #{conference.inspect}, to: #{to.inspect}, hash: #{hash.inspect}, files: #{files.inspect}"
  if conference
    # Normal (inbound) event socket playing to a conference
    files.map do |file|
      cmd "conference", "#{conference} play #{file} #{to}"
    end
  elsif to
    # Normal event socket syntax
    files.map do |file|
      # TODO: Support playing to an individual leg of the call.
      cmd "broadcast", "#{to} #{file} both"
    end
  else
    # Outbound event sockets
    files.map { |file| cmd('playback', file) }
  end
end

#record(hash = {}, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 72

def record(hash={}, &block)
  # TODO: Could want to record a conference or a UUID
  p hash
  if hash[:stop]
    cmd 'stop_record_session', hash[:stop]
  else
    file = hash[:file] || File.join(Dir::tmpdir, String.random(32), '.wav')

    raise "Cannot supply both a timeout and a block!" if hash[:timeout] && block_given?

    dtmf_breaker = lambda do |digit|
      return! file if digit == hash[:break_on]
    end

    rec_cmd = cmd "record", file, :on_keypress => dtmf_breaker
    [].tap do |cmds|
      cmds << play('beep') if hash[:beep]
      cmds << rec_cmd
      if hash[:timeout]
        cmds << DSL::Dialplan::NoOpEventCommand.new(hash[:timeout])
      elsif block_given?
        cmds << block
        cmds << record(:stop => file)
      end
      cmds << file
      p cmds
      cmds
    end
  end
end

#return!(obj) ⇒ Object



60
61
62
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 60

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

#set(key, value) ⇒ Object



27
28
29
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 27

def set(key, value)
  cmd 'set', "#{key}=#{value}"
end

#speak(text, hash = {}) ⇒ Object

These should all return those objects…



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 13

def speak(text, hash={})
  voice, engine = hash[:voice] || "Dianne", hash[:engine] || "cepstral"

  dtmf = hash[:on_keypress]
  speak_cmd = cmd 'speak', "#{engine}|#{voice}|%p" % text, :on_keypress => dtmf

  if hash[:timeout] == 0
    [speak_cmd, DSL::Dialplan::NoOpEventCommand.new(hash[:timeout], :on_keypress => dtmf)]
  else
    puts "Returning the normal speak command"
    speak_cmd
  end
end

#wait(seconds = nil, &block) ⇒ Object



68
69
70
# File 'lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb', line 68

def wait(seconds=nil, &block)
  DSL::Dialplan::NoOpEventCommand.new(seconds)
end