Skip to content

Instantly share code, notes, and snippets.

@zh
Created March 18, 2011 03:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zh/875599 to your computer and use it in GitHub Desktop.
Save zh/875599 to your computer and use it in GitHub Desktop.
Working with twitter stream (eventmachine)
require 'rubygems'
require 'em-http'
require 'json'
EM.run {
username = 'tw_user'
password = 'tw_password'
buffer = ""
http = EventMachine::HttpRequest.new('http://stream.twitter.com/1/statuses/filter.json').post({
:head => { 'Authorization' => [ username, password ] },
:query => { "track" => 'iphone' }
#:query => { "follow" => '12345678,7654321' }
})
http.callback {
unless http.response_header.status == 200
puts "Call failed with response code #{http.response_header.status}"
end
}
http.stream do |chunk|
buffer += chunk
while line = buffer.slice!(/.+\r\n/)
tweet = JSON.parse(line)
puts tweet['text']
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment