Monday, November 05, 2007

Twitter4R v0.3.0

After Twitter.com 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')
require('twitter')

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

if client.authenticate?("developerthatdoesnotpretendtobegraphicdesigner", "seriousdevelopernotfakestevejobswannabe")
puts "Password does describe user well!"
end


Example code for using the favorites API:
gem('twitter4r', '>=0.3.0')
require('twitter')
require('twitter/console')

client = Twitter::Client.from_config('some/path.yml')
statuses = client.favorites
ids = statuses.collect {|s| s.id }
# 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])

Enjoy!

11 comments:

Sérgio Santos said...

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 ;)

thilo said...

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?

S. Potter said...

@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).

Thanks!

thilo said...

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.
Twitter::Client.new.timeline_for(:public).to_json
should return the same json as calling
http://twitter.com/statuses/public_timeline.json
And finally ;-) filters would be great!
Thank you!

S. Potter said...

@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.

thilo said...

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

S. Potter said...

@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.

Thanks!

siong1987 said...

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?

siong1987 said...

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.

avocade said...

Yup. To clarify: simply change the line

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

in the file rails.rb inside the twitter4r gem.

S. Potter said...

@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!