A 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:

source 'https://rubygems.org'
ruby '2.1.2'

gem 'iron_cache'
gem 'sendgrid-ruby'
gem 'sinatra'
web: bundle exec rackup config.ru -p $PORT
require 'json'

require 'iron_cache'
require 'sendgrid-ruby'
require 'sinatra/base'

# Create a modular style Sinatra app
class FourMom < Sinatra::Base
  # Create a new SendGrid client
  @@sg = SendGrid::Client.new do |c|
    c.api_user = ENV['SENDGRID_USERNAME']
    c.api_key  = ENV['SENDGRID_PASSWORD']

  # Grab our Iron Cache
  @@cache = IronCache::Client.new.cache('fourmom')

  # Save the values locally in case our dyno went to sleep
  @@city  = @@cache.get('city')
  @@city  = @@city.value if @@city
  @@state = @@cache.get('state')
  @@state = @@state.value if @@state

  # Our post request handler
  post '/' do
    # Parse out the data from Foursquare
    checkin = JSON.parse(params[:checkin])
    city    = checkin['venue']['location']['city']
    state   = checkin['venue']['location']['state']

    # Check if we are somewhere new
    if city != @@city || state != @@state
      # Update our values
      @@city = city
      @@state = state
      @@cache.put('city', @@city)
      @@cache.put('state', @@state)

      # Create a new email...
      mail = SendGrid::Mail.new do |m|
        # My mom has a Verizon phone
        m.to      = '1238675309@vtext.com'
        m.from    = 'taco@cat.limo'
        m.subject = 'FourMom'
        m.text    = "Hi mama! I'm currently in: #{city}, #{state}."

      # ... and send it!

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!

SendGrid Team
Expert advice and insight about all things email including best practices tips, examples, and advice for marketers, developers, and everyone in between.