Skip to content

Instantly share code, notes, and snippets.

View jonmarkgo's full-sized avatar
🎓
preparing the MLH Fellowship

Jonathan Gottfried jonmarkgo

🎓
preparing the MLH Fellowship
View GitHub Profile
require "google/api_client"
require "sinatra"
require "twilio-ruby"
require "json"
post "/receivesms" do
client = Google::APIClient.new(:key => "ABC123")
client.authorization = nil
search = client.discovered_api('customsearch')
post "/subcallback" do
reply_result = @client.execute(
:api_method => @glass.timeline.get,
:parameters => {"id" => @data["itemId"]},
:authorization => @client.authorization)
reply = reply_result.data.text
msg_result = @client.execute(
:api_method => @glass.timeline.get,
:parameters => {"id" => reply_result.data.inReplyTo},
:authorization => @client.authorization)
post "/receivesms" do
timeline_item = @glass.timeline.insert.request_schema.new({
:sourceItemId => params[:SmsSid],
:menuItems => [{:action => "REPLY"}, {:action => "READ_ALOUD"}],
:html => "<article>\n <section>\n <div class=\"text-auto-size\" style=\"\">\n <p class=\"yellow\">SMS from #{params[:From]}</p>\n <p>#{params[:Body]}</p>\n </div>\n </section>\n <footer>\n <div><img src=\"http://i.imgur.com/VM8Jji2.png\"></div>\n </footer>\n</article>",
:speakableText => params[:Body],
:title => "SMS from #{params[:From]}",
:notification => {:level => "AUDIO_ONLY"}
})
result = @client.execute(
get "/" do
api_result = @client.execute(
:api_method => @oauth2.userinfo.get)
"You have authenticated as G+ user #{api_result.data.name}, your new Twilio-powered Glass phone number is #{@phone_number}<br><a href='http://www.twilio.com/' style='text: decoration: none; display: inline-block; width: 166px; height: 0; overflow: hidden; padding-top: 31px; background: url(http://www.twilio.com/packages/company/img/logos_icon_poweredbysmall.png) no-repeat;'>powered by twilio</a><br><br>Built by <a href='http://twitter.com/jomarkgo'>@jonmarkgo</a>"
end
get "/oauth2authorize" do
redirect @client.authorization.authorization_uri.to_s, 303
end
get "/oauth2callback" do
@client.authorization.fetch_access_token!
token_pair = if session[:token_id]
TokenPair.get(session[:token_id])
else
TokenPair.new
if request.path_info == '/subcallback' #if we get a push from google, do a different lookup based on the userToken
@data = JSON.parse(request.body.read)
token_pair = TokenPair.get(@data['userToken'])
@phone_number = token_pair.phone_number
@client.authorization.update_token!(token_pair.to_hash)
else
if session[:token_id] #if the user is logged in
token_pair = TokenPair.get(session[:token_id])
@client.authorization.update_token!(token_pair.to_hash)
@phone_number = token_pair.phone_number
before do
@base_url = "https://myserver.com"
@twilio = Twilio::REST::Client.new "ACxxxxxxxxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyyyyyyy"
@client = Google::APIClient.new
@client.authorization.client_id = "1234.apps.googleusercontent.com"
@client.authorization.client_secret = "ITSASECRET"
@client.authorization.scope = [
"https://www.googleapis.com/auth/glass.timeline",
"https://www.googleapis.com/auth/userinfo.profile"
]
require './app'
run Sinatra::Application
source 'https://rubygems.org'
gem 'google-api-client'
gem 'twilio-ruby'
gem 'sinatra'
gem 'data_mapper'
gem 'rack-ssl-enforcer'
group :production do
gem "pg"
gem "dm-postgres-adapter"
require "sinatra"
require "google/api_client"
require "data_mapper"
require "twilio-ruby"
require 'rack-ssl-enforcer'
DataMapper::setup(:default, ENV["DATABASE_URL"] || "sqlite3://#{Dir.pwd}/dev.db")
class TokenPair
include DataMapper::Resource