Class: Punchblock::Translator::Asterisk::Component::Output

Inherits:
Component show all
Defined in:
lib/punchblock/translator/asterisk/component/output.rb

Instance Attribute Summary

Attributes inherited from Component

#call, #id, #internal

Instance Method Summary collapse

Methods inherited from Component

#call_id, #execute_command, #initialize, #logger_id, #send_complete_event, #send_event

Constructor Details

This class inherits a constructor from Punchblock::Translator::Asterisk::Component::Component

Instance Method Details

#continue(event = nil) ⇒ Object



67
68
69
# File 'lib/punchblock/translator/asterisk/component/output.rb', line 67

def continue(event = nil)
  signal :continue, event
end

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/punchblock/translator/asterisk/component/output.rb', line 13

def execute
  @call.answer_if_not_answered

  return with_error 'option error', 'An SSML document is required.' unless @component_node.ssml

  return with_error 'option error', 'An interrupt-on value of speech is unsupported.' if @component_node.interrupt_on == :speech

  [:start_offset, :start_paused, :repeat_interval, :repeat_times, :max_time].each do |opt|
    return with_error 'option error', "A #{opt} value is unsupported on Asterisk." if @component_node.send opt
  end

  case @media_engine
  when :asterisk, nil
    return with_error 'option error', "A voice value is unsupported on Asterisk." if @component_node.voice

    @execution_elements = @component_node.ssml.children.map do |node|
      case node
      when RubySpeech::SSML::Audio
        lambda { current_actor.play_audio! node.src }
      else
        return with_error 'unrenderable document error', 'The provided document could not be rendered.'
      end
    end.compact

    @pending_actions = @execution_elements.count

    send_ref

    @interrupt_digits = '0123456789*#' if [:any, :dtmf].include? @component_node.interrupt_on

    @execution_elements.each do |element|
      element.call
      wait :continue
      process_playback_completion
    end
  when :unimrcp
    doc = @component_node.ssml.to_s.squish.gsub(/["\\]/) { |m| "\\#{m}" }
    send_ref
    @call.send_agi_action! 'EXEC MRCPSynth', doc, mrcpsynth_options do |complete_event|
      pb_logger.debug "MRCPSynth completed with #{complete_event}."
      send_complete_event success_reason
    end
  end
end

#play_audio(path) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/punchblock/translator/asterisk/component/output.rb', line 71

def play_audio(path)
  pb_logger.debug "Playing an audio file (#{path}) via STREAM FILE"
  op = current_actor
  @call.send_agi_action! 'STREAM FILE', path, @interrupt_digits do |complete_event|
    pb_logger.debug "STREAM FILE completed with #{complete_event}. Signalling to continue execution."
    op.continue! complete_event
  end
end

#process_playback_completionObject



58
59
60
61
62
63
64
65
# File 'lib/punchblock/translator/asterisk/component/output.rb', line 58

def process_playback_completion
  @pending_actions -= 1
  pb_logger.debug "Received action completion. Now waiting on #{@pending_actions} actions."
  if @pending_actions < 1
    pb_logger.debug "Sending complete event"
    send_complete_event success_reason
  end
end

#setupObject



9
10
11
# File 'lib/punchblock/translator/asterisk/component/output.rb', line 9

def setup
  @media_engine = @call.translator.media_engine
end