Skip to content

Instantly share code, notes, and snippets.

@bryanaknight
Created January 7, 2014 23:54
Show Gist options
  • Save bryanaknight/8309141 to your computer and use it in GitHub Desktop.
Save bryanaknight/8309141 to your computer and use it in GitHub Desktop.
require 'oauth'
require 'json'
require 'pp'
CONSUMER_KEY = OAuth::Consumer.new(
'qLqLjhVG6TQDesSYnT3bw',
'9Zw1OIB17651BMZUEsRlA5CKk3qG1MJclzr0f0Qq0')
ACCESS_TOKEN = OAuth::Token.new(
'321130543-9SPLOfIzXbHDuWnaZSyBBm3ctix0xrh6REooGZSd',
'rXxUbsc3CWUiDIxmj3Ys1zpzzNRorlvlZYmZFtiLRnG4i'
)
class TwitterRequest
attr_reader :baseurl
def initialize
@baseurl = "https://api.twitter.com"
end
def user_timeline(screen_name, count = 10)
path = "/1.1/statuses/user_timeline.json"
query = URI.encode_www_form(
"screen_name" => screen_name,
"count" => count,
)
response = make_request(URI("#{baseurl}#{path}?#{query}"))
if response.code == '200' then
tweets = JSON.parse(response.body)
print_timeline(tweets)
end
end
def hashtag_tweets(*hash_tag)
path = "/1.1/search/tweets.json"
query = "q=%23" + hash_tag.join("+%23")
response = make_request(URI("#{baseurl}#{path}?#{query}"))
if response.code == '200' then
data = JSON.parse(response.body)
print_timeline(data["statuses"])
end
end
def make_request(address)
http = Net::HTTP.new address.host, address.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Get.new address.request_uri
request.oauth! http, CONSUMER_KEY, ACCESS_TOKEN
http.start
http.request request
end
def print_timeline(tweets)
tweets = tweets.sort_by {|tweet| tweet["created_at"] }.reverse
tweets.each do |tweet|
puts "#{tweet['text']}"
end
puts tweets.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment