Twittering Around In Ruby
Join the DZone community and get the full member experience.Join For Free
This code uses the Twitter4R v0.2.0 which is a complete Ruby library that provides access to all documented Twitter REST APIs (and some experimental features). Read more at: http://twitter4r.rubyforge.org http://snakesgemscoffee.blogspot.com/2007/07/twitter4r-020-release.html You will first need to install the Twitter4R Ruby Gem: sudo gem install twitter4r.
require('rubygems') gem('twitter4r', '>=0.2.0') require('twitter') login = 'mylogin' # change this password = 'mypass' # change this friend = 'myfriend' # change this client = Twitter::Client.new(:login => login, :password => password) public_timeline = client.timeline_for(:public) do |status| # do something here with each individual status in timeline that is also returned puts status.text end # can also pass a block like above to process each individual status object returned in timeline friends_timeline = client.timeline_for(:friends) # same as above with block if you want friend_timeline = client.timeline_for(:friend, friend) # Retrieve the user object (with all public profile information) for my friend user = Twitter::User.find(friend) # If I don't want to be friends any more I "defriend" the user... user.defriend # Now I realize that was a terrible mistake and "befriend" them... user.befriend # At this point I want to send them a private message to let them know I made a mistake # You can do this in two ways. message = Twitter::Message.create(:client => client, :user => user, :text => "I didn't really mean to defriend you. Sorry!") # OR... message = client.message(:post, "I didn't really mean to defriend you. Sorry!", user) # Now I want to post a new status to my own timeline. # This can be done one of two ways... status = Twitter::Status.create(:client => client, :text => 'Sleeping off the beer from last night') # OR... status = client.status(:post, 'Sleeping off the beer from last night') # Now I realize my mother is on twitter and wouldn't like to see me talking about beer, # so assuming she doesn't have IM or SMS alerts we can delete the evidence.... client.status(:delete, status)