Skip to content

Instantly share code, notes, and snippets.

@mcfiredrill
Created November 16, 2012 00:23
Show Gist options
  • Save mcfiredrill/4082744 to your computer and use it in GitHub Desktop.
Save mcfiredrill/4082744 to your computer and use it in GitHub Desktop.
ruby twitter stream client
require 'rubygems'
require 'oauth'
require 'json'
CONSUMER_KEY = 'xxxxxxxx'
CONSUMER_SECRET = 'xxxxxxxx'
OAUTH_TOKEN = 'xxxxxxxx'
OAUTH_SECRET = 'xxxxxxxx'
@consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET)
@access = OAuth::Token.new(OAUTH_TOKEN, OAUTH_SECRET)
uri = URI("https://stream.twitter.com/1.1/statuses/filter.json?track=romney")
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
request = Net::HTTP::Post.new uri.request_uri
request.oauth!(http, @consumer, @access)
puts request['Authorization']
http.request request do |response|
response.read_body do |chunk|
j = JSON.parse(chunk)
puts "#{j["user"]["screen_name"]}: #{j["text"]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment