Skip to content

Instantly share code, notes, and snippets.

@kenn
Created March 19, 2010 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenn/338237 to your computer and use it in GitHub Desktop.
Save kenn/338237 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'em-http'
require 'json'
require 'lib/ring_buffer'
TWEETS = RingBuffer.new(10)
def handle_tweet(tweet)
return unless tweet['text']
TWEETS.push(tweet)
end
EM.next_tick do
http = EM::HttpRequest.new('http://stream.twitter.com/1/statuses/sample.json').get :head => { 'Authorization' => [ 'user', 'password' ] }
buffer = ""
http.stream do |chunk|
buffer += chunk
while line = buffer.slice!(/.+\r?\n/)
handle_tweet JSON.parse(line)
end
end
end
get '/tweets' do
TWEETS.map {|tweet| "<p><b>#{tweet['user']['screen_name']}</b>: #{tweet['text']}</p>" }.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment