Skip to content

Commit

Permalink
Allow setting barge per-prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed May 4, 2015
1 parent 33f2150 commit d2dff32
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- jruby
Expand Down
17 changes: 15 additions & 2 deletions lib/adhearsion-ivr/ivr_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,19 @@ def on_failure(&block)

def run
@errors = 0
deliver_prompt true
deliver_prompt interruptible: 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
@barge = nil
end

if grammar
ask_options = { grammar: grammar, mode: :voice }
elsif grammar_url
Expand Down Expand Up @@ -188,6 +193,14 @@ def prompts
self.class.prompts
end

def barge(val = nil)
if val.nil?
@barge.nil? ? self.class.barge : @barge
else
@barge = val
end
end

def max_attempts
self.class.max_attempts
end
Expand Down
63 changes: 63 additions & 0 deletions spec/adhearsion-ivr/ivr_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -677,5 +677,68 @@ 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
prompts << -> { 'fourth' } # Unbargeable

max_attempts 4

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 noinput_result
controller.should_receive(:ask).once.with('fourth', grammar: :some_grammar, mode: :voice, interruptible: false).and_return match_result
controller.should_receive(:say).once.with "Let's go to Paris"
controller.run
end
end

context 'with barge enabled' do
let(:controller_class) do
Class.new(Adhearsion::IVRController) do
prompts << -> { 'first' } # Bargeable
prompts << -> { 'second' } # Bargeable
prompts << -> { self.barge false; 'third' } # Unbargeable
prompts << -> { 'fourth' } # Bargeable

max_attempts 4
barge true

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: true).and_return noinput_result
controller.should_receive(:ask).once.with('third', grammar: :some_grammar, mode: :voice, interruptible: false).and_return noinput_result
controller.should_receive(:ask).once.with('fourth', 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

0 comments on commit d2dff32

Please sign in to comment.