ReNet
ReNet

Reputation: 3

my sinatra website to talk to twilio

Sinatra wraps all the views in the layout.erb wherever you placed <%= yield %> in that file. This is great if you are only serving html pages to browsers. BUT We are writting an app that also requires to talk to Twilio via XML. Challenge: all our outputs were being sent out wrapped in a <!DOCTYPE/html>.

We managed to bypass the layout.erb by specifying layout: false in our action.erb file.

post '/incoming' do
  content_type 'text/xml'
  @message = "this is working"
 erb :'/incoming.xml', layout: false
end

I am sure there is a 'better' way of having Sinatra serve the xml content and would appreciate very much your help on this!

Upvotes: 0

Views: 37

Answers (2)

bcoyle73
bcoyle73

Reputation: 33

You can just use the twilio-ruby gem https://github.com/twilio/twilio-ruby to render the Twiml from your action like this:

post '/incoming' do
  response = Twilio::TwiML::Response.new do |r|
    r.Say "This is working"
  end

  response.text
end

That will render the proper Twiml doc that twilio is expecting.

Upvotes: 1

Brent Schooley
Brent Schooley

Reputation: 792

If I understand your situation/question correctly you are looking for a cleaner way of serving XML (Twilio's TwiML in this case) from Sinatra. If that's the case, I recommend checking out the Sinatra::Builder module. This module will allow you to dynamically build the TwiML right within the route. It also supports templates if you want to use a templated approach.

Full documentation for Builder can be found on Builder's RubyForge page.

Upvotes: 0

Related Questions