1. stevegraham Posted by stevegraham

    Twilio.rb: The Ruby way to interact with the Twilio API

    If you haven’t heard of Twilio, they’re a cool startup in San Francisco that are revolutionising the VOIP game. In simple terms, they offer scalable VOIP platform, with pay as you go pricing, all exposed via a HTTP REST API and competitively priced. 

    Shrewple itself uses Twilio for all of its telephony needs. I had the idea for Shrewple about four or five years ago when I first started playing with Asterisk the open source PBX. When Twilio appeared and abstracted away all the underlying complexity of VOIP behind an API implemented using the lingua franca of the internet, I decided the time was right to jump in.

    There are already a handful of Twilio Ruby libraries so why another? Well, none of them support the entire API, have clean Rails integration, excellent test coverage (an absolute must for any code I use), and provide satisfactory abstraction. Personally I need all these things, so I wrote one myself.

    Consider the following, this is how one makes a call using the Twilio library that has been adopted as de facto:

    This isn’t cool. It isn’t DRY, and it completely ignores every principle of OOP.

    This is how you do the same with Twilio.rb. First configure the library once with your account credentials, e.g. in an application initializer:

    Then to make a call it’s just this:

    Another advantage of this is that it returns a Ruby object that is effectively just a different representation of that resource, so it has its own methods. So to hangup the above call:

    Or to redirect it to another TwiML document immediately:

    Twilio.rb also has great Rails 3 integration. If Rails 3 is being used where Twilio.rb is required, it extends it by adding :voice as another mime type your application can respond to. You can have a controller that looks like this:

    Your app will also now respond to the URL http://example.com/foo.voice

    It also registers an ActionView template handler for TwiML views. In this case the view file `foo.voice`. The views are as simple as this:

    So simple! The whole API is supported, the README is quite comprehensive. See the Github repo for more details.

    Twilio.rb is a beta release, to install use the `—pre` flag with Rubygems and report any bugs to me!

    $ gem install twilio-rb --pre

    Shrewple is software for professionals who can deliver their services over the telephone. Have your own toll free telephone number that your clients call and their credit card will be automatically billed for the time they speak with you.

    To be notified when Shrewple launches, please visit http://www.shrewple.com/ and leave us your email.

    Hope you find Twilio.rb useful, Thanks!