Monday, December 13, 2010

twitter integration


Note

The Twitter gem supports OAuth and HTTP Auth (until Twitter deprecates it). OAuth is scary at first, but not once you try it out a little bit. Feel free to read my article on OAuth to get past the scary part. Also, I have created an example app that uses OAuth and the Twitter gem for you to peruse.

Install

$ sudo gem install twitter

Examples

First some OAuth action:
# NOT SHOWN: granting access to twitter on website
# and using request token to generate access token
oauth = Twitter::OAuth.new('consumer token', 'consumer secret')
oauth.authorize_from_access('access token', 'access secret')

client = Twitter::Base.new(oauth)
client.friends_timeline.each { |tweet| puts tweet.inspect }
client.user_timeline.each { |tweet| puts tweet.inspect }
client.replies.each { |tweet| puts tweet.inspect }

client.update('Heeeyyyyoooo from Twitter Gem!')
Now some HTTP Auth action:
httpauth = Twitter::HTTPAuth.new('username', 'password')

client = Twitter::Base.new(httpauth)
client.update('Heeeeyyyyooo from the Twitter Gem')
client.friends_timeline.each { |tweet| puts tweet.text }

Search API Examples

#searches all tweets for httparty
Twitter::Search.new('httparty').each do |r|
puts r.inspect
end

# searches all of jnunemaker's tweets for httparty
Twitter::Search.new('httparty').from('jnunemaker').each do |r|
puts r.inspect
end

# searches all tweets from jnunemaker to oaknd1
Twitter::Search.new.from('jnunemaker').to('oaknd1').each do |r|
puts r.inspect
end

# you can also use fetch to actually just get the parsed response
Twitter::Search.new.from('jnunemaker').to('oaknd1').fetch()

Trends API Examples

Twitter::Trends.current
Twitter::Trends.current(:exclude => 'hashtags')

Twitter::Trends.daily # current day
Twitter::Trends.daily(:exclude => 'hashtags')
Twitter::Trends.daily(:date => Date.new(2009, 5, 1))

Twitter::Trends.weekly # current day
Twitter::Trends.weekly(:exclude => 'hashtags')
Twitter::Trends.weekly(:date => Date.new(2009, 5, 1))

No comments:

Post a Comment