Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Better way to write responses #6

Closed
wants to merge 2 commits into from

2 participants

André Luis Leal Cardoso Junior Andrew Benton
André Luis Leal Cardoso Junior

I'm building response blocks in the project I'm working on like these, It's fairly simple and way more readable for me.

I think this would be a nice addition to twilio, what do you guys think?

Andrew Benton

we just released a new version of the twilio ruby library called twilio-ruby. it includes a utility for building twiml responses that uses builder for xml. the class is here:

https://github.com/twilio/twilio-ruby/blob/master/lib/twilio-ruby/twiml/response.rb

and here's how you'd use it:

response = Twilio::TwiML::Response.new do |r|
  r.Say 'hello there'
end

puts response.text #=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Say>hello there</Say></Response>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 1, 2010
  1. André Luis Leal Cardoso Junior

    Fix failing spec

    andrehjr authored
  2. André Luis Leal Cardoso Junior

    Add a better dsl

    andrehjr authored
This page is out of date. Refresh to see the latest.
Showing with 43 additions and 7 deletions.
  1. +21 −2 lib/twiliolib.rb
  2. +22 −5 tests/response_spec.rb
23 lib/twiliolib.rb
View
@@ -176,7 +176,7 @@ def allowed?(verb)
#@return [Object] Twilio Verb object
#
#@raises [ArgumentError] Invalid Argument
- def initialize(body = nil, params = {})
+ def initialize(body = nil, params = {}, &block)
@children = []
if body.class == String
@body = body
@@ -191,6 +191,10 @@ def initialize(body = nil, params = {})
raise ArgumentError, "Attribute Not Supported"
end
end
+
+ if block_given?
+ instance_eval(&block)
+ end
end
#set an attribute key / value
@@ -247,46 +251,57 @@ def append(verb)
def addSay(string_to_say = nil, opts = {})
append Twilio::Say.new(string_to_say, opts)
end
+ alias_method :say, :addSay
def addPlay(file_to_play = nil, opts = {})
append Twilio::Play.new(file_to_play, opts)
end
+ alias_method :play, :addPlay
def addGather(opts = {})
append Twilio::Gather.new(opts)
end
+ alias_method :gather, :addGather
def addRecord(opts = {})
append Twilio::Record.new(opts)
end
+ alias_method :record, :addRecord
def addDial(number = nil, opts = {})
append Twilio::Dial.new(number, opts)
end
+ alias_method :dial, :addDial
def addRedirect(url = nil, opts = {})
append Twilio::Redirect.new(url, opts)
end
+ alias_method :redirect, :addRedirect
def addPause(opts = {})
append Twilio::Pause.new(opts)
end
+ alias_method :pause, :addPause
def addHangup
append Twilio::Hangup.new
end
+ alias_method :hangup, :addHangup
def addNumber(number, opts = {})
append Twilio::Number.new(number, opts)
end
+ alias_method :number, :addNumber
def addConference(room, opts = {})
append Twilio::Conference.new(room, opts)
end
+ alias_method :conference, :addConference
def addSms(msg, opts = {})
append Twilio::Sms.new(msg, opts)
end
+ alias_method :sms, :addSms
end
@@ -361,8 +376,12 @@ class Response
extend Twilio::Verb::ClassMethods
include Twilio::Verb
allowed_verbs :say, :play, :gather, :record, :dial, :redirect, :pause, :hangup, :sms
+
+ def self.build(&block)
+ Response.new(&block)
+ end
end
-
+
# Twilio Utility function and Request Validation class
class Utils
27 tests/response_spec.rb
View
@@ -31,7 +31,7 @@ def bad_attr(verb)
@r = Twilio::Response.new
@r.respond.should == '<Response></Response>'
end
-
+
it "add attribute" do
@r = Twilio::Response.new
@r.set :crazy => 'delicious'
@@ -41,7 +41,24 @@ def bad_attr(verb)
it "bad attribute" do
bad_attr(Twilio::Response)
end
+
+ it "should say something with more parameters" do
+ Twilio::Response.build do
+ say "whatever", :voice => 'woman'
+ end.respond.should == '<Response><Say voice="woman">whatever</Say></Response>'
+ end
+
+ it "should say something" do
+ Twilio::Response.build do
+ say "whatever"
+ end.respond.should == '<Response><Say>whatever</Say></Response>'
+ end
+ it "should redirect to a url via POST" do
+ Twilio::Response.build do
+ redirect "example.com", :method => "POST"
+ end.respond.should == '<Response><Redirect method="POST">example.com</Redirect></Response>'
+ end
end
describe Twilio::Say do
@@ -52,7 +69,8 @@ def bad_attr(verb)
@r.append(Twilio::Say.new("Hello Monkey"))
@r.respond.should == '<Response><Say>Hello Monkey</Say></Response>'
end
-
+
+
it "should say hello monkey and loop 3 times" do
@r = Twilio::Response.new
@r.append(Twilio::Say.new("Hello Monkey", :loop => 3))
@@ -64,7 +82,7 @@ def bad_attr(verb)
@r.append(Twilio::Say.new("Hello Monkey", :voice => 'woman'))
@r.respond.should == '<Response><Say voice="woman">Hello Monkey</Say></Response>'
end
-
+
it "should say have a woman say hello monkey and loop 3 times and be in french" do
@r = Twilio::Response.new
@r.append(Twilio::Say.new("Hello Monkey", :language => 'fr'))
@@ -88,7 +106,6 @@ def bad_attr(verb)
@r.set :crazy => 'delicious'
@r.respond.should == '<Say crazy="delicious"></Say>'
end
-
end
describe Twilio::Play do
@@ -264,7 +281,7 @@ def bad_attr(verb)
@r = Twilio::Response.new
@r.append(Twilio::Sms.new("Hello, World", :to => 1231231234,
:from => 3453453456, :statusCallback => "example.com?id=34&action=hey"))
- @r.respond.should == '<Response><Sms from="3453453456" statusCallback="example.com?id=34&amp;action=hey" to="1231231234">Hello, World</Sms></Response>'
+ @r.respond.should == '<Response><Sms to="1231231234" from="3453453456" statusCallback="example.com?id=34&amp;action=hey">Hello, World</Sms></Response>'
end
Something went wrong with that request. Please try again.