Skip to content

Instantly share code, notes, and snippets.

@shaundon
Created September 20, 2012 08:19
Show Gist options
  • Save shaundon/3754641 to your computer and use it in GitHub Desktop.
Save shaundon/3754641 to your computer and use it in GitHub Desktop.
A simple Ruby script to connect to Twitter's streaming API and return tweets matching a search term in real time.
# Tweet Stream. Uses the 'tweetstream' gem.
# Asks for a keyword, and monitors tweet for that keyword.
require 'tweetstream'
# Ask for keyword.
print "Keyword: "
keyword = gets.chomp()
TweetStream.configure do |config|
config.consumer_key = 'YOUR KEY HERE'
config.consumer_secret = 'YOUR SECRET HERE'
config.oauth_token = 'OAUTH TOKEN HERE'
config.oauth_token_secret = 'OAUTH TOKEN SECRET HERE'
config.auth_method = :oauth
end
puts "Connecting to Twitter..\n"
client = TweetStream::Client.new
client.on_error do |message|
puts message
end
client.on_inited do
puts "Connected to Twitter!\n"
end
client.track(keyword) do |status|
puts "@#{status.user.screen_name}: #{status.text}"
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment