Twitter4R v0.3.0

Written on 2:18:00 AM by S. Potter

After added a few extra APIs for favoriting statuses in October, I finally got around to adding them to Twitter4R tonight. Since there is new functionality I have released it as version 0.3.0. To install all you need to do is:

$ sudo gem install twitter4r
The Rubyforge mirrors might still be syncing if you are doing this soon after I post this message. The changes to the library include:
  • Added Twitter::Client#authenticate? method that is a lightweight way to verify a Twitter user's credentials
  • Favorites: allows Twitterers to add statuses to their favorites, list all their current favorites and remove any statuses from the list.
Example code for verifying a user would look like:
gem('twitter4r', '>=0.3.0')

client =
unless client.authenticate?("macdeveloperthatlovesleopard", "iamasciolists")
  puts "Password does not describe user well!"

if client.authenticate?("developerthatdoesnotpretendtobegraphicdesigner", "seriousdevelopernotfakestevejobswannabe")
  puts "Password does describe user well!"
Example code for using the favorites API:
gem('twitter4r', '>=0.3.0')

client = Twitter::Client.from_config('some/path.yml')
statuses = client.favorites
ids = statuses.collect {|s| }
# clean favorites list by deleting
statuses.each {|s| client.favorite(:remove, s) }

# now add back first status id from original favorites list
client.favorite(:add, ids[0])

If you enjoyed this post Subscribe to our feed


  1. Anonymous |

    Nice update, specially with the authenticate?, which I been wanting since it was released. It's very useful for quick check ups.

    Keep up the good work on Twitter4R ;)

  2. thilo |

    thank you for the great library! It works fine but I can't figure out this one:
    render :json=>@statuses.to_json works fine but
    render :xml=>@statuses.to_xml gives the error message
    'Not all elements respond to to_xml'
    @statuses is the result of a 'timeline_for' call.
    What am I doing wrong?

  3. S. Potter |

    @thilo: thanks for the comment. At first glance it definitely looks like a defect in Twitter4R. I will look into it tonight and either release a patch this weekend (time permitting).


  4. thilo |

    just another minor issue: The twitter api returns the elements 'source' and 'truncated' in a status. They seem to get lost in twitter4r.
    It would be great if a call to the to_xml and to_json methods of status arrays return the same xml and json as from the api directly, e.g.
    should return the same json as calling
    And finally ;-) filters would be great!
    Thank you!

  5. S. Potter |

    @thilo: Thanks. Actually I noticed the missing source and truncated attributes on Status last weekend and I have added those in the v0.3.1 branch in the Git repository waiting for me to decide if my #to_xml patch is sufficient or if I need to patch ActiveRecord, ActiveResource and ActiveSupport for their ridiculously lame support (ok, non-existent support) of namespaced classes to XML.

    The Current #to_xml fix in the 0.3.1 branch in Git is able to use :exclude, :include, :methods, :indent and :skip_instruct presently. However, this adds the builder Gem as a new dependency to Twitter4R (unless I add some decently smart magic).

    Sorry for the delay, my day job workload just got a little heavier lately.

    Thanks for the comments though. Much appreciated.

  6. thilo |

    in the meantime I found out that an optional 'since' parameter in direct message requests seems to get lost. I think the problem is in line 21 in the file messaging.rb which only includes a page parameter:
    def uri_suffix(opts); opts && opts[:page] ? "?page=#{opts[:page]}" : ""; end
    The 'since' parameter works fine in timeline_for requests.
    When can we expect the 0.3.1 version?
    Thank you, Thilo

  7. S. Potter |

    @thilo: Thanks for the patch and bug reporting contributions. I'll be attending to Twitter4R work this week assuming work isn't hectic. Please email me (see README of Twitter4R SVN or Git repository for email address) and let me know what name I should add to the contributors list for you.


  8. siong1987 |

    I encounter this problem:

    "`gem_original_require': no such file to load -- active_record/xml_serialization (MissingSourceFile)"

    when I am trying to use it in a rails 2.0.2 project.

    I did have active_record installed as a gem in my gems directory. But, I am wondering what is xml_serialization for?

  9. siong1987 |

    I think I have found out the problem. It is because that xml_serialization is not longer been used in Rails 2.0.2.

    We should change it to include "active_record/serialization" in rails.rb.

    Anyway, I will try to submit a patch too but I am new to Ruby and Rails. Hopefully, I could do it well and nicely.

  10. Oskar |

    Yup. To clarify: simply change the line

    require 'active_record/xml_serialization'
    require 'active_record/serialization'

    in the file rails.rb inside the twitter4r gem.

  11. S. Potter |

    @avocade: yes, that is for Rails 2.0. When I wrote this earlier last year (2007) when Rails 1.2 was the standard and 'active_record/xml_serialization' was correct. Technically I haven't released a Twitter4R that is compatible with Rails 2.0 yet. I will need to make sure not to break Rails 1.2 applications with the 0.3.1/0.4.0 release expected in mid/late-May. Thanks!

  12. Anonymous |

    I'm getting this error.
    both 0.3.0 and 0.2.0

    i am using it on IRB first...
    ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

    >> client.status(:post, 'Testing twitter4r') NoMethodError: undefined method `parse' for Time:Class

  13. S. Potter |

    Please require('time') before you call the API. That should fix the issue. The GitHub version of Twitter4R has fixed this:

    $ sudo gem install mbbx6spp-twitter4r


  14. Anonymous |

    I'm pretty new to Ruby and Rails but would like to use Twitter4R in a project. I'd appreciate any help you could provide with the following issues:

    First off, I had a number of problems installing this gem on windows due to "nmake" and then another issue with "cl" once I put nmake into my windows\system32 directory.

    I downloaded the 0.3.0 .zip and dropped the folders into my project directory. Is this an ok way to install it?

    Second, do you have, or can you point me to a walk-through on how to get a Rails project that uses this code up and running? I've developed in many other languages but feel like an idiot for not being able to figure out how to get this to work. Perhaps a simple walk-through would help me and others with the same problem?

    Thanks a lot!

  15. S. Potter |

    Hi Greg,

    Installing any native RubyGem on Windows is problematic. Twitter4R does not have native extensions, however, it depends on the JSON RubyGem which by default has a native extension. Windows does not provide a C compile by default, which is why the gem install fails.

    Uncompressing the Twitter4R zip (for Windows only) is a valid workaround for this RubyGems issue specific to default Windows installations (if you have nmake installed or Visual Studio and nmake.exe is in your PATH you should not need to do this workaround and the gem install should work). You will also need to install the json-pure RubyGem which is a Ruby only version of the JSON gem.

    This will be slower than the native extension version, but probably not as slow as Ruby XML parsers that are used by other Ruby Twitter libraries out there. It might only be a performance issue if you are deploying the Rails application on a Windows box that does not have nmake.exe in its PATH.

    There is a very old screencast I did for Twitter4R on Rails 1.2x (it requires some tweaking for Rails 2.x - i.e. you should NOT require 'twitter/rails' any more), which is on YouTube:

    The first half of the screencast talks perhaps too much about the project, but it gets into the juicy parts 24 seconds into the screencast.


  16. Anonymous |

    Hi Susan,

    Thanks for providing a great library. I am building a site and I want the users that login to make the queries to Twitter instead of my user account so that I do not exceed the # queries per hour rule.

    How can I do this? I hope I did not miss this somewhere, sorry if I did.



  17. S. Potter |

    @Justin: You can create a new Twitter::Client instance with each of your users credentials so that they can do "their" Twitter activity.

    For example, if @screen_name_1 signs into your application you create a new instance of Twitter::Client as follows:
    >> => "screen_name_1", :password => "thepassword")

    Then you can store this instance (or at least the credentials in your database somewhere (try using some kind of encryption for the passwords at least) and create the lightweight instance of Twitter::Client whenever the user does something on your site.


  18. Anonymous |

    Hi Susan,

    Thanks. I was trying no to store their usernames and passwords, but I can see this will be the only way.

    I, also, was trying not to store their information in the database and just in the session. Rails sessions seem to last for a very long time.

    I guess I could blowfish the password in the session. Any thoughts yay or nay.

    Thanks for the advise.


  19. Josh Lippiner |


    How would you verify that a user EXISTS? I am using the gem to let a user send a tweet to another user and I need to make sure the recipient is a valid twitter user.

    Any thoughts? There is not valid method for users.

  20. S. Potter |

    @Josh can you tell me where the user exists API is in the REST API docs at:

    I see a friendships/exists and block/exists API, but no user/exists.

    If you want to test whether a user exists you can do the following:
    client.user("potentially_invalid_user", :info)
    rescue Twitter::RESTError => re
    if "404 == re.code
    # do something to signal no user exists

    It is certainly not the most beautiful solution, but it works.

  21. S. Potter |

    oops I forgot the close the quote in the code example. It should have been:

    client.user("potentially_invalid_user", :info)
    rescue Twitter::RESTError => re
    if "404" == re.code
    # do something to signal no user exists

  22. S. Potter |

    Actually the code should re-raise the RESTError *iff* it is not a 404 for the application calling your code to deal with OR you should handle it appropriately for your needs in your code.


Post a Comment