Twilio
Quick and Dirty Apps with Sinatra, DataMapper, RestClient & Heroku

Summarized using AI

Quick and Dirty Apps with Sinatra, DataMapper, RestClient & Heroku

John Britton • April 07, 2011 • Earth

In the presentation titled "Quick and Dirty Apps with Sinatra, DataMapper, RestClient & Heroku" by John Britton, attendees at Ruby on Ales 2011 learn about developing quick applications using a combination of four main tools: Sinatra, DataMapper, RestClient, and Heroku. John Britton shares his experience working at Twilio and emphasizes that developing simple applications can be achieved in a matter of hours rather than days.

Key Points Discussed:
- Introduction to the Tools:

- Sinatra: A lightweight framework ideal for specifying URLs and executing Ruby code.

- DataMapper: Simplifies the process of setting up persistent objects, making it easy to manage small app databases.

- RestClient: A REST client that streamlines API requests using HTTP methods.
- Heroku: A cloud platform that allows developers to deploy apps without the hassle of server management.

  • Example Applications:

    • It's in Your Gmail: A simple app that helps users retrieve previously emailed information by creating a redirect based on a URL pattern, allowing them to find important details easily. The app is quick to set up with minimal code through Sinatra.
    • Call Congress: A more complex app that provides a phone number for users to reach their local congress representatives. It utilizes the Sunlight Labs API to fetch the correct representatives based on the caller's area code, guiding users through a dial menu.
    • Phone Lottery: An interactive app that allows users to enter a lottery by calling a specific number, capturing their phone number, and selecting a random winner. It highlights how DataMapper can effectively manage entrants' data without requiring complex database setups.
  • Tools for Development:

    • John discusses the use of HTTP Console, which aids in simplifying API interaction, offering a user-friendly way to send requests and view responses in real time. It integrates smoothly with Heroku for easier data management.

Conclusions:

- Building quick apps does not require advanced skills but rather an understanding and willingness to use available tools effectively. The takeaway message is that experimenting with simple frameworks allows for rapid development and deployment, fostering creativity and innovation in application development.

The presentation encourages attendees to leverage these tools to create functional applications in a time-efficient manner, ultimately enhancing their coding experience and productivity.

Quick and Dirty Apps with Sinatra, DataMapper, RestClient & Heroku
John Britton • April 07, 2011 • Earth

by: John Britton

Help us caption & translate this video!

http://amara.org/v/GZC8/

Ruby on Ales 2011

00:00:22 I hope I'm not offending anybody, but I don't have any slides.
00:00:24 This is me on Twitter and GitHub. You can find me there, and you'll see all the code that I'm going to show you over the next little while.
00:00:39 My name is John Britton. I work at a company called Twilio in San Francisco, but I live in New York. Originally, I got into programming with a kid's programming language— I know that's kind of a sad origin story, but that's where I came from. Now I do Ruby stuff.
00:00:58 I want to talk to you about quick and dirty apps that I build. Basically, whenever I have an idea and I want to push something out really quickly, these are the tools that I use to make that happen in hours instead of days.
00:01:08 I usually sit down with an idea and want to get it up and running all within a few minutes. So today, we're going to talk about Sinatra, DataMapper, RestClient, and Heroku.
00:01:28 If you're not familiar with Sinatra already, it's a lightweight framework that lets you specify your URL, execute some Ruby code, and then return the result to the browser.
00:01:40 As for DataMapper, when I start writing these small apps, I don't like to store data because it's a pain.
00:01:49 If I can avoid storing things, I try to do so. However, I found that DataMapper is really simple to set up for persistent objects with these small apps.
00:02:00 I use a lot of APIs for different services, and RestClient makes it super simple to do. You can see if you want to do something again; just use the HTTP method required—like GET or POST.
00:02:24 For DataMapper, you can define a class, include DataMapper, and now you can set up relationships and auto-increment fields. It's really easy to do.
00:02:36 Once you build these apps and want to get them out the door without managing servers, you can use Heroku. I'm going to give you three different examples of apps and walk through what I built and how they work.
00:02:54 I sent a few emails to some organizers asking for information about this event, and the common responses were, 'Let me Google that for you,' or 'How can I find out what's going on?' This made me frustrated.
00:03:11 I had an idea: maybe the information's not on the web, but it's something that's been emailed to me before. I often get fed up with people saying, 'Hey, can you send me that again?' So I made a little app called 'It's in Your Gmail.'
00:03:30 This app consists of a handler that responds to GET requests at any URL. It captures a regular expression that matches every character that comes after the slash and then creates a redirect.
00:03:43 So now when my friends ask me where an event is going to be, I can say, 'I've already emailed it to you; it's in your Gmail! Go look there!'. It's a really simple app.
00:04:03 All I had to do was create an app.rb file, which took only six lines. You set up your Gemfile to use Sinatra, and it just runs.
00:04:22 The next thing I want to show you is something a bit more complicated called Call Congress.
00:04:29 Many of my friends complain that they wish certain bills were passed or that the government would make laws their way, but they're not sure how to contact their congresspeople.
00:04:48 I came up with the idea of having a phone number that connects you directly to the congresspeople relevant to your area. I'm from Massachusetts and still a voter there.
00:05:03 The idea for this app is simple: you call a specific number and get a list of your senators and representatives. You can select who you'd like to connect to.
00:05:35 When you call this number, it will look up your area code, find out where you're from, and give you a list of senators and representatives in your area, along with a dial menu.
00:06:01 The way this works is we require certain gems, grab our Sunlight Labs API key, and then make requests to my Call Congress app. All of this is done within a web app, which I'll demonstrate shortly.
00:06:30 To illustrate, if I make a POST request to my Call Congress app URL, it will respond with an introduction message, thanking you for using the app and providing options to connect you.
00:06:57 When you call, the app captures your zip code, queries the Sunlight API, gets the legislators, and provides a dial menu so you can press '1' for one representative, '2' for another, and so on.
00:07:19 If you don't provide a zip code, the app will inform you that it doesn't know your zip code and will redirect you to the national congress dialing number.
00:07:43 When you select a congressperson by pressing a button, the app connects you directly to them. It's interesting to note that this app doesn't store any data.
00:08:14 The next app I want to show you is called Phone Lottery, which is a bit more interactive. If everyone could take out your cell phones, I hope you all have one.
00:08:50 When you call the number shown, you'll get a response saying, 'Thanks for calling,' and the call will hang up.
00:09:06 I've set it up using DataMapper, which is convenient for small apps like this. Each caller is an entrant, and they all have an ID and phone number.
00:09:38 Using DataMapper's auto-upgrade feature, it sets up my database from the database URL and automatically manages the columns and tables. This is useful for rapid development.
00:10:00 All calls to the entry URL create a new entry with their phone number, and they receive an XML response saying they've successfully entered the lottery.
00:10:40 Now, let's do a call out to randomly select a winner from all the entrants. We have four beer tickets to give away, along with the messenger bag.
00:11:10 Finally, I wanted to share something for all of you who didn't win, called 'Losers.' This shows all the phone numbers of those that lost but are still eligible for the next lottery.
00:11:47 I am collecting all those numbers from the database using DataMapper, and while it could look nicer, I didn't focus on the design.
00:12:50 Using RestClient, I could then create requests to the Twilio API, allowing me to call back anyone who entered and give them a nice message.
00:13:29 I created a simple structure to handle the API calls and to send out calls with a specified message to each phone number.
00:13:50 The final part of my presentation showcased useful tools I have found while building these kinds of apps. One important tool is HTTP Console, which I discovered not too long ago.
00:14:36 HTTP Console allows you to interact with your API easily and is much simpler than using curl. It enables you to send requests effortlessly and see the responses in real time.
00:15:20 It also integrates with Heroku to sync your database, which is incredibly useful for local development. You can pull your production data to your local machine with just a simple command.
00:15:53 That wraps up most of what I wanted to share regarding my presentation on quick applications using Sinatra, DataMapper, RestClient, and Heroku. If anyone has questions, I'm happy to answer them.
00:16:46 In summary, you don't need to be a wizard to build applications like these. It just takes a little effort, some tools, and a willingness to experiment.
00:17:22 Thanks everyone for listening! I hope you found this presentation helpful and informative.
Explore all talks recorded at Ruby on Ales 2011
+8