/ node

The SendGrid Ruby Gem and My Mama!

MamaA sample of the things I love are, Ruby, webhooks, and my mama! In this post, I'm putting all of them together!

Let's start with how excited SendGrid is to announce the release of the official SendGrid Ruby Gem, sendgrid-ruby! The library includes the smtpapi-ruby library created by our friend Wataru Sato of SendGrid Japan. As always, any sort of feedback is welcomed and encouraged.

What better way to introduce a new library than with a hack!?

My mother, like most moms I imagine, was very sad and hesitant to see her baby boy move out and start life on his own. Because I'm always traveling across the country, my mom is constantly checking up on me via text to see where I am and if my flights made it ok. Since she still has a "dumb" phone and is not the least bit technical, I decided to make this process easier for both of us.

Enter FourMom: it's Foursquare, for my mom! It turns out that Foursquare has some pretty awesome webhooks to take advantage of when a user checks in. If you're not familiar with what a webhook is, you can read the What's a Webhook? post by our very own Nick Quinlan. Inspired by a similar hack created by good friend Amit Jotwani, I decided that I want to send my mother a text message whenever I check-in to a new city or state.

We'll be using Sinatra, a beautiful micro web framework for Ruby, Heroku to host the app, and Iron Cache to store the last check in. Heroku is an awesome platform that allows you to host your app for free! We need Iron Cache because Heroku will kill our app after a period of no requests (starting it again on the next request) and we'll lose what we have in memory. We'll be sending the text message as an email using SendGrid. Most carriers have an email address for each phone number. I found this site to be very useful.

Let's get to it!

Start in a new folder and create the necessary files for our app! You can find them below:

Be sure to customize the email block with your recipient, from email, subject, and message.

Next, initialize the project as a git repo:
$ git init && git add . && git commit -m "Initial commit"

Create your new Heroku app:
$ heroku create

Take note of the url, and add Iron Cache with:
$ heroku addons:add iron_cache:developer

Set your SendGrid credentials as config variables:

Alternatively, you can add SendGrid as a Heroku addon:
$ heroku addons:add sendgrid:starter

Now head here and create a new app for Foursquare. Be sure to enable the Push API and paste in your Heroku app's URL to all four of the boxes. Next, we need to authenticate our account with our app using OAuth. This is easily done through this script. Save it, run it, and paste in the variables it asks for from your Foursquare app's page.

All that's left is to deploy the code to Heroku!

$ git push heroku master

The next time you check in on Foursquare, your app will receive a post request from their servers, check if you are somewhere new, and update your recipient with your new and exciting location! You can find the completed project here.

Feel free to reach out to me on Twitter if you run into any issues!

This post was originally written for and posted to the SendGrid Blog