Skip to content

Instantly share code, notes, and snippets.

@eddiezane
Last active August 29, 2015 14:05
Show Gist options
  • Save eddiezane/0bf1eb052efc0d19c0d8 to your computer and use it in GitHub Desktop.
Save eddiezane/0bf1eb052efc0d19c0d8 to your computer and use it in GitHub Desktop.
FourMom
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']
end
# 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}."
end
# ... and send it!
@@sg.send(mail)
end
end
end
source 'https://rubygems.org'
ruby '2.1.2'
gem 'iron_cache'
gem 'sendgrid-ruby'
gem 'sinatra'
web: bundle exec rackup config.ru -p $PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment