Permalink
Browse files

Merge pull request #4 from globaldev/finish-on-key

Add support for finishOnKey in Gather blocks
  • Loading branch information...
2 parents 6c8314d + 4dfc114 commit bd0f4792cf6906204d9cf9dbb9ac6b0d5ac414ae @JMongol committed Mar 13, 2013
@@ -84,14 +84,19 @@ def gather_method
return @xml["method"]
end
+ def gather_finish_on_key
+ raise "Not a gather" unless gather?
+ return @xml["finishOnKey"] || '#' # '#' is the default finish key if not specified
+ end
+
def press(digits)
raise "Not a gather" unless gather?
# Fetch the path and then post
path = gather_action
# Update the root call
- root_call.request_for_twiml!(path, :digits => digits, :method => gather_method)
+ root_call.request_for_twiml!(path, :digits => digits, :method => gather_method, :finish_on_key => gather_finish_on_key)
end
# Some basic accessors
@@ -116,6 +121,16 @@ def get_gather_node
@xml.at_xpath("Gather")
end
+ def formatted_digits(digits, options = {})
+ if digits.nil?
+ ''
+ elsif options[:finish_on_key]
+ digits.split(options[:finish_on_key])[0]
+ else
+ digits
+ end
+ end
+
protected
# New object creation
def self.from_xml(parent, xml)
@@ -159,7 +174,7 @@ def request_for_twiml!(path, options = {})
:format => :xml,
:CallSid => @root_call.sid,
:From => @root_call.from_number,
- :Digits => options[:digits] || "",
+ :Digits => formatted_digits(options[:digits], :finish_on_key => options[:finish_on_key]),
:To => @root_call.to_number,
:AnsweredBy => (options[:is_machine] ? "machine" : "human")
)
@@ -1,2 +1,2 @@
-<Say voice="woman">You entered <%= @digits %></Say>
+<Say voice="woman">You entered <%= @digits %>.</Say>
<Redirect><%= twilio_index_path %></Redirect>
@@ -0,0 +1,3 @@
+<Gather finishOnKey="*" action="<%= test_action_twilio_index_path %>" method="post">
+ <Say>Please enter some digits followed by hash.</Say>
+</Gather>
@@ -4,6 +4,7 @@
post "test_start", :on => :collection
post "test_action", :on => :collection
+ post "test_gather_finish_on_asterisk", :on => :collection
post "test_hangup", :on => :collection
post "test_dial", :on => :collection
post "test_redirect", :on => :collection
@@ -235,7 +235,7 @@
@call.current_path.should == test_action_twilio_index_path
# This view says the digits we pressed - make sure
- @call.should have_say "You entered 98765"
+ @call.should have_say "You entered 98765."
end
it "should gather without a press" do
@@ -246,8 +246,38 @@
# We should still be on the same page
@call.current_path.should == test_start_twilio_index_path
end
+
+ it "should respond to the default finish key of hash" do
+ @call.within_gather do |gather|
+ gather.press "98765#"
+ end
+ @call.should have_say "You entered 98765."
+ end
end
+ describe "with finishOnKey specified" do
+ before(:each) do
+ @call = ttt_call(test_gather_finish_on_asterisk_twilio_index_path, @our_number, @their_number)
+ end
+
+ it "should strip the finish key from the digits" do
+ @call.within_gather do |gather|
+ gather.press "98765*"
+ end
+
+ @call.should have_say "You entered 98765."
+ end
+
+ it "should still accept the digits without a finish key (due to timeout)" do
+ @call.within_gather do |gather|
+ gather.press "98765"
+ end
+
+ @call.should have_say "You entered 98765."
+ end
+
+ end
+
describe "failure" do
before(:each) do
@call = ttt_call(test_say_twilio_index_path, @our_number, @their_number)

0 comments on commit bd0f479

Please sign in to comment.