Skip to content

Instantly share code, notes, and snippets.

@phobeo
phobeo / main.rb
Created September 30, 2011 22:53
twilio sample
require 'sinatra'
class Main < Sinatra::Base
enable :static
set :public, 'public'
get '/' do
content_type 'application/xml'
twiml = %{<?xml version="1.0" encoding="UTF-8"?>
<Response>
@phobeo
phobeo / campus_stream.rb
Created July 12, 2011 07:34
stream campus tweets into mongodb
require 'rubygems'
require 'tweetstream'
require 'mongo'
CAMPUS_BBOX='39.447025911738,-0.35662933148245,39.470136313596,-0.33659356733534'
puts "Opening Mongo connection..."
conn = Mongo::Connection.new
db = conn.db("testdb")
tweets = db.collection("campus_tweets")
@phobeo
phobeo / widget who uses ruby file.html
Created April 18, 2011 13:57
ruby file (in server).rb
require 'rubygems'
require 'sinatra'
require 'oauth'
require 'json'
require 'haml'
require 'ruby-debug'
enable :sessions
TWITTER_CONSUMER_KEY = "asac8z2Tw9XR2LflFmNVsw"
@phobeo
phobeo / queues_sample
Created April 15, 2011 16:53
queues sample
class ProcessTimeline
def perform(tweet, user)
log "received #{tweet.id} for user #{user}"
# if tweet.id was not stored before, store tweet in all_tweets_for process collection
# store pair [tweet.id, user.id] in tweets_for_user collection
end
end
class ProcessTweet
def perform(tweet)
@phobeo
phobeo / javascript aspect ratio calculation (with GCD)
Created January 24, 2011 15:02
javascript aspect ratio calculation algorithm (take the GCD and divide both elements of resolution)
/* euclidean GCD (feel free to use any other) */
function gcd(a,b) {if(b>a) {temp = a; a = b; b = temp} while(b!=0) {m=a%b; a=b; b=m;} return a;}
/* ratio is to get the gcd and divide each component by the gcd, then return a string with the typical colon-separated value */
function ratio(x,y) {c=gcd(x,y); return ""+(x/c)+":"+(y/c)}
/* eg:
> ratio(320,240)
"4:3"
> ratio(360,240)