From c7ec557844a9b87e16466dfa32f1cf5510e3a362 Mon Sep 17 00:00:00 2001 From: Ben Langfeld Date: Fri, 1 May 2015 14:59:06 -0300 Subject: [PATCH] Allow setting barge per-prompt --- lib/adhearsion-ivr/ivr_controller.rb | 20 +++++++++++++++- spec/adhearsion-ivr/ivr_controller_spec.rb | 28 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/adhearsion-ivr/ivr_controller.rb b/lib/adhearsion-ivr/ivr_controller.rb index 0da547c..e047551 100644 --- a/lib/adhearsion-ivr/ivr_controller.rb +++ b/lib/adhearsion-ivr/ivr_controller.rb @@ -130,11 +130,15 @@ def run deliver_prompt true end - def deliver_prompt(interruptible = self.class.barge) + def deliver_prompt(interruptible = nil) prompt = prompts[@errors] || prompts.last prompt = instance_exec(&prompt) if prompt.respond_to? :call logger.debug "Prompt: #{prompt.inspect}" + if interruptible.nil? + interruptible = self.barge + end + if grammar ask_options = { grammar: grammar, mode: :voice } elsif grammar_url @@ -188,6 +192,20 @@ def prompts self.class.prompts end + def barge(val = nil) + if val.nil? + if @barge.nil? + self.class.barge + else + old = @barge + @barge = nil + old + end + else + @barge = val + end + end + def max_attempts self.class.max_attempts end diff --git a/spec/adhearsion-ivr/ivr_controller_spec.rb b/spec/adhearsion-ivr/ivr_controller_spec.rb index 042b0e4..e03caf3 100644 --- a/spec/adhearsion-ivr/ivr_controller_spec.rb +++ b/spec/adhearsion-ivr/ivr_controller_spec.rb @@ -677,5 +677,33 @@ def grammar controller.run end end + + context 'when overriding barge per-prompt' do + context 'with default barge behaviour' do # Only first prompt bargeable + let(:controller_class) do + Class.new(Adhearsion::IVRController) do + prompts << -> { 'first' } # Bargeable + prompts << -> { 'second' } # Unbargeable + prompts << -> { self.barge true; 'third' } # Bargeable + + on_complete do |result| + say "Let's go to #{result.utterance}" + end + + def grammar + :some_grammar + end + end + end + + it 'allows interruption of each prompt correctly' do + controller.should_receive(:ask).once.with('first', grammar: :some_grammar, mode: :voice, interruptible: true).and_return noinput_result + controller.should_receive(:ask).once.with('second', grammar: :some_grammar, mode: :voice, interruptible: false).and_return noinput_result + controller.should_receive(:ask).once.with('third', grammar: :some_grammar, mode: :voice, interruptible: true).and_return match_result + controller.should_receive(:say).once.with "Let's go to Paris" + controller.run + end + end + end end end