« Mac Software Christmas Bundles | Main | Wellington »

A Simple Recipe For Accessing Twitter From Ruby

I wanted to play with accessing Twitter via their REST API and there are several Ruby libraries to do that. I picked one that said it had full support (Twitter4r) and tried it out.

First install the Ruby gem for Twitter4r:

sudo gem install twitter4r

or on Windows:

gem install -–version 1.1.1 json

gem install twitter4r

Then you can access it with a little chunk of code as simple as this:

require('rubygems')
gem('twitter4r', '0.3.0')
require('time')
require('twitter')

client = Twitter::Client.new(:login => 'your Twitter username', :password => 'your Twitter password')
timeline = client.timeline_for(:me) 

timeline.each do |status|
  puts status.user.screen_name, status.text
end

When I ran that, out popped the last 20 tweets I had done.

TrackBack

TrackBack URL for this entry:
http://www.johnmunsch.com/cgi-bin/mt/mt-tb.cgi/134

Comments

Yeah, it's similar with PHP. It's easy, but it creeps me out that in order to do something with Twitter, people have to give you the keys to the kingdom. I auto-post twitter updates from thecodezone.com and I don't like to store peoples' passwords (even though I encrypt 'em), but there's no other way and they're way overdue for their new system supporting third parties.

I'd much rather have a system like Facebook where I'd store unique key for the user, and the user can grant me permissions to post and can revoke that permission from within their twitter account.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)