31 May 2010

Ruby Twitter Gem : Usage in Rails Application


In my current project, there was a requirement to show the latest tweet in the home page and also an interface to send tweets from the same rails application using a particular twitter account.

A quick Google search showed maximum results on a gem named 'Twitter'

here in this post I am going to use this gem to pull the latest tweets as well as, implementing a simple interface to send tweets directly.

There are wide possibilities using this gem, like, you can follow friends, unfollow friends, delete tweets, send direct messages, create lists etc etc.

So first of all Install the gem:
gem install twitter

If you have created a test rails application, create a new controller and name it as 'TwitterPublish'
ruby script/generate controller twitter_publish
Now add the following code to your newly created 'TwitterPublishController'
Controller TwitterPublish < ApplicationController

  require 'rubygems'
  require 'twitter'

  def tweet
    httpauth = Twitter::HTTPAuth.new('twitter_username',  'password')
    @user = Twitter::Base.new(httpauth)
  end

end
In the above controller code, provide the required twitter login details.

Next create a tweet.html.erb file within app/views/twitter_publish

and paste the following code :
<% @ user.friends_timeline.each do |tweet| %>
  <%= image_tag "#{tweet.user[:profile_image_url]}" %>
  &nbsp; <%= tweet.user[:screen_name] %>&nbsp;<br/>
  <%= tweet.text %><br/>
<% end %>
Now if we run the server, and check out the link http://localhost:3000/twitter_publish/tweets , we will find all the latest tweets from the people I am following will be listed down along with their image.

Next, we need a text box in the same view isung which we will be able to tweet directly.

add the following code at the top of app/views/twitter_publish/tweet.html.erb :
<% form_for :twitter do |t| %>

  What's happening ?

  <%= t.text_field :tweet %><br/>

   <%= t.submit "Tweet" %>

<% end %>
And to create the update we send as tweet through the text field, we have to add the following code to our tweet method in TwitterpublishController:
if request.post?

  @user.update(params[:twitter][:tweet])

  redirect_to :action=>'tweet'

end
That's all, you can now tweet directly from your rails application.

I have also come across another gem for the same purpose named 'Twitter4R'

Have you tried this gem out ?

Do share your views on both the gems.

7 comments:

  1. Good post. Couple of remarks, though:

    require 'rubygems' line should not be there. And twitter gem requirement should be done through the rails in environment.rb file.

    ReplyDelete
  2. What should be there in database.yml?
    Due to that only, it is showing some error. Please help me.
    I need this urgently.

    Thanks in advance.

    ReplyDelete
  3. When I followed your steps, I got this following error :
    uninitialized constant Twitter::HTTPAuth

    Can you please suggest me some solution.
    Thanks in advance.

    ReplyDelete
  4. I admire your blog for such a nice environment for twitter, I love to use twitter since a long time and I ma a fond of different twitter environments and I found "marketontwitr" by google which has quite friendly environment to their users and marketers.

    ReplyDelete
  5. HTTPAuth has been deprecated by Twitter...

    ReplyDelete
  6. Nice to share so good info for the visitors. Nice and i appreciate this.

    ReplyDelete
  7. Though the levels look really simple, they are actually
    quite challenging. If you are unable to sign in to Game Center or are having problems staying connected:
    . Making your game play is the next phase you'll be focusing on.

    Feel free to visit my weblog ... spiele Spielen

    ReplyDelete

Share your thoughts, Lets have a discussion :)