lib/twilio/verb.rb in twilio-2.4.0 vs lib/twilio/verb.rb in twilio-2.4.1
- old
+ new
@@ -3,17 +3,17 @@
# There are 5 primary verbs (say, play, gather, record, dial) and 3 secondary (hangup, pause, redirect).
# Verbs can be chained and, in some cases, nested.
#
# If your response consists of a single verb, you can call a Verb class method:
#
- # Twilio::Verb.say('The time is 9:35 PM.')
+ # Twilio::Verb.say 'The time is 9:35 PM.'
#
# But if you need to chain several verbs together, just wrap them in an instance block and call the 'response' attribute:
#
- # verb = Twilio::Verb.new { |v|
- # v.dial('415-123-4567')
- # v.redirect('http://www.foo.com/nextInstructions')
+ # verb = Twilio::Verb.new {
+ # dial '415-123-4567'
+ # redirect 'http://www.foo.com/nextInstructions'
# }
# verb.response
class Verb
attr_reader :response
@@ -29,36 +29,36 @@
@xml = Builder::XmlMarkup.new
@xml.instruct!
if block_given?
@chain = true
- @response = @xml.Response { block.call(self) }
+ @response = @xml.Response { instance_eval(&block) }
end
end
-
+
# The Say verb converts text to speech that is read back to the caller.
# Say is useful for dynamic text that is difficult to prerecord.
#
# Examples:
- # Twilio::Verb.say('The time is 9:35 PM.')
- # Twilio::Verb.say('The time is 9:35 PM.', :loop => 3)
+ # Twilio::Verb.say 'The time is 9:35 PM.'
+ # Twilio::Verb.say 'The time is 9:35 PM.', :loop => 3
#
# With numbers, 12345 will be spoken as "twelve thousand three hundred forty five" while
# 1 2 3 4 5 will be spoken as "one two three four five."
#
- # Twilio::Verb.say('Your PIN is 1234', :loop => 4)
- # Twilio::Verb.say('Your PIN is 1 2 3 4', :loop => 4)
+ # Twilio::Verb.say 'Your PIN is 1234', :loop => 4
+ # Twilio::Verb.say 'Your PIN is 1 2 3 4', :loop => 4
#
# If you need a longer pause between each loop, instead of explicitly calling the Pause
# verb within a block, you can set the convenient pause option:
#
- # Twilio::Verb.say('Your PIN is 1 2 3 4', :loop => 4, :pause => true)
+ # Twilio::Verb.say 'Your PIN is 1 2 3 4', :loop => 4, :pause => true
#
# Options (see http://www.twilio.com/docs/api_reference/TwiML/say) are passed in as a hash:
#
- # Twilio::Verb.say('The time is 9:35 PM.', :voice => 'woman')
- # Twilio::Verb.say('The time is 9:35 PM.', {:voice => 'woman', :language => 'es'})
+ # Twilio::Verb.say 'The time is 9:35 PM.', :voice => 'woman'
+ # Twilio::Verb.say 'The time is 9:35 PM.', :voice => 'woman', :language => 'es'
def say(*args)
options = {:voice => 'man', :language => 'en', :loop => 1}
args.each do |arg|
case arg
when String
@@ -81,17 +81,17 @@
}
end
# The Play verb plays an audio URL back to the caller.
# Examples:
- # Twilio::Verb.play('http://foo.com/cowbell.mp3')
- # Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 3)
+ # Twilio::Verb.play 'http://foo.com/cowbell.mp3'
+ # Twilio::Verb.play 'http://foo.com/cowbell.mp3', :loop => 3
#
# If you need a longer pause between each loop, instead of explicitly calling the Pause
# verb within a block, you can set the convenient pause option:
#
- # Twilio::Verb.play('http://foo.com/cowbell.mp3', :loop => 3, :pause => true)
+ # Twilio::Verb.play 'http://foo.com/cowbell.mp3', :loop => 3, :pause => true
#
# Options (see http://www.twilio.com/docs/api_reference/TwiML/play) are passed in as a hash,
# but only 'loop' is currently supported.
def play(*args)
options = {:loop => 1}
@@ -123,21 +123,21 @@
#
# Options (see http://www.twilio.com/docs/api_reference/TwiML/gather) are passed in as a hash
#
# Examples:
# Twilio::Verb.gather
- # Twilio::Verb.gather(:action => 'http://foobar.com')
- # Twilio::Verb.gather(:finishOnKey => '*')
- # Twilio::Verb.gather(:action => 'http://foobar.com', :finishOnKey => '*')
+ # Twilio::Verb.gather :action => 'http://foobar.com'
+ # Twilio::Verb.gather :finishOnKey => '*'
+ # Twilio::Verb.gather :action => 'http://foobar.com', :finishOnKey => '*'
#
# Gather also lets you nest the Play, Say, and Pause verbs:
#
- # verb = Twilio::Verb.new { |v|
- # v.gather(:action => '/process_gather', :method => 'GET) {
- # v.say('Please enter your account number followed by the pound sign')
+ # verb = Twilio::Verb.new {
+ # gather(:action => '/process_gather', :method => 'GET) {
+ # say 'Please enter your account number followed by the pound sign'
# }
- # v.say("We didn't receive any input. Goodbye!")
+ # say "We didn't receive any input. Goodbye!"
# }
# verb.response # represents the final xml output
def gather(*args, &block)
options = args.shift || {}
output {
@@ -156,13 +156,13 @@
#
# Options (see http://www.twilio.com/docs/api_reference/TwiML/record) are passed in as a hash
#
# Examples:
# Twilio::Verb.record
- # Twilio::Verb.record(:action => 'http://foobar.com')
- # Twilio::Verb.record(:finishOnKey => '*')
- # Twilio::Verb.record(:transcribe => true, :transcribeCallback => '/handle_transcribe')
+ # Twilio::Verb.record :action => 'http://foobar.com'
+ # Twilio::Verb.record :finishOnKey => '*'
+ # Twilio::Verb.record :transcribe => true, :transcribeCallback => '/handle_transcribe'
def record(*args)
options = args.shift
output { @xml.Record(options) }
end
@@ -177,21 +177,21 @@
# current document URL if no action is provided.
#
# Options (see http://www.twilio.com/docs/api_reference/TwiML/dial) are passed in as a hash
#
# Examples:
- # Twilio::Verb.dial('415-123-4567')
- # Twilio::Verb.dial('415-123-4567', :action => 'http://foobar.com')
- # Twilio::Verb.dial('415-123-4567', {:timeout => 10, :callerId => '858-987-6543'})
+ # Twilio::Verb.dial '415-123-4567'
+ # Twilio::Verb.dial '415-123-4567', :action => 'http://foobar.com'
+ # Twilio::Verb.dial '415-123-4567', :timeout => 10, :callerId => '858-987-6543'
#
# Twilio also supports an alternate form in which a Number object is nested inside Dial:
#
- # verb = Twilio::Verb.new { |v|
- # v.dial {
- # v.number('415-123-4567')
- # v.number('415-123-4568')
- # v.number('415-123-4569')
+ # verb = Twilio::Verb.new {
+ # dial {
+ # number '415-123-4567'
+ # number '415-123-4568'
+ # number '415-123-4569'
# }
# }
# verb.response # represents the final xml output
def dial(*args, &block)
number_to_dial = ''
@@ -220,14 +220,14 @@
# It is normally chained with other verbs.
#
# Options (see http://www.twilio.com/docs/api_reference/TwiML/pause) are passed in as a hash
#
# Examples:
- # verb = Twilio::Verb.new { |v|
- # v.say('greetings')
- # v.pause(:length => 2)
- # v.say('have a nice day')
+ # verb = Twilio::Verb.new {
+ # say 'greetings'
+ # pause :length => 2
+ # say 'have a nice day'
# }
# verb.response
def pause(*args)
options = args.shift
output { @xml.Pause(options) }
@@ -237,13 +237,13 @@
# It is normally chained with other verbs.
#
# Options (see http://www.twilio.com/docs/api_reference/TwiML/redirect) are passed in as a hash
#
# Examples:
- # verb = Twilio::Verb.new { |v|
- # v.dial('415-123-4567')
- # v.redirect('http://www.foo.com/nextInstructions')
+ # verb = Twilio::Verb.new {
+ # dial '415-123-4567'
+ # redirect 'http://www.foo.com/nextInstructions'
# }
# verb.response
def redirect(*args)
redirect_to_url = ''
options = {}
@@ -268,13 +268,13 @@
#
# Twilio::Verb.hangup
#
# If your response is chained:
#
- # verb = Twilio::Verb.new { |v|
- # v.say("The time is #{Time.now}")
- # v.hangup
+ # verb = Twilio::Verb.new {
+ # say "The time is #{Time.now}"
+ # hangup
# }
# verb.response
def hangup
output { @xml.Hangup }
end
@@ -301,10 +301,10 @@
private
def output
@chain ? yield : @xml.Response { yield }
end
-
+
def loop_with_pause(loop_count, xml, &verb_action)
last_iteration = loop_count-1
loop_count.times do |i|
yield verb_action
xml.Pause unless i == last_iteration
\ No newline at end of file