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.

Advertisements

One thought on “A Simple Recipe For Accessing Twitter From Ruby

  1. John Hattan

    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.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s