Created
March 19, 2010 21:53
-
-
Save kenn/338237 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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