Skip to content
billymeltdown edited this page Sep 13, 2010 · 10 revisions

This is a forked repository. Have a look at
jnunemaker’s twitter wiki
for more information.

This gem was forked before version 0.5. I plan to keep it at version 0.4.2’s codebase for the time being because:

  • I’m not ready to switch over to OAuth yet in my own apps, since Twitter has no date set, yet.
  • My apps (http://keeptempo.com, http://gopingme.com) require the exception handling I’ve added here.

If I decide to move to OAuth at some point in the future, this will remain available as-is, and I’ll make my changes on a fork of jnunemaker’s master.

403 Request Handling

I needed some more informational exception handling for my apps. Basically, I really need to know why a create_friendship request failed. The twitter ruby gem typically returns a really generic error for 403 requests, but the actual error documents returned by Twitter tell you a lot more. For example, if I can rescue AlreadyFollowing after a follow request, I know I’m good to go, as opposed to just thinking that the request failed. Saves me from having to check friendship_exists? first, which then runs up your rate limit on the API. Annoying when you are coding a system that has to do automatic follow-backs.

To do this, I needed exceptions with the info body from Twitter’s response, mostly for the friendship/following methods. I define a new exception, Twitter::RequestRefused < ArgumentError, and all the other exceptions derive from that one, so you can at least catch RequestRefused to collect ’em all.


module Twitter
  class RequestRefused < ArgumentError; end
  class CantFindUsers < RequestRefused; end
  class AlreadyFollowing < RequestRefused; end
  class YouAreNotFriends < RequestRefused; end
end

Changing the way the request method handles 403 responses from the API is not an insignificant change – most of these circumstances would have fallen through to Twitter::CantConnect. The up-shot is that now you get more informational messages, but the downside is that if you are using rescue Twitter::CantConnect in your code as a catch-all, you’re no longer catching exceptions thrown for the scenarios described below. This may or may not have adverse affects on the rest of the API and CLI, although everything looks okay when I run the current rspec tests.

The basics are:


>> twitter.d('billymeltdown', 'herro!')
Twitter::RequestRefused: Response code 403: Forbidden You cannot send messages to users who are not following you.
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8.1/lib/twitter/base.rb:235:in `request'
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8.1/lib/twitter/base.rb:94:in `d'

>> twitter.friendship_exists? 'billymeltdown', 'monkeyseeeeemonkydoooo'
Twitter::CantFindUsers: Could not find both specified users.
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8/lib/twitter/base.rb:232:in `request'
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8/lib/twitter/base.rb:109:in `friendship_exists?'

>> twitter.create_friendship ''
Twitter::RequestRefused: Response code 403: Forbidden 
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8/lib/twitter/base.rb:234:in `request'
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8/lib/twitter/base.rb:99:in `create_friendship'

>> twitter.create_friendship 'gpm'
Twitter::AlreadyFollowing: Could not follow user: gpm is already on your list.
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8/lib/twitter/base.rb:233:in `request'
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8/lib/twitter/base.rb:99:in `create_friendship'

>> twitter.destroy_friendship 'billymeltdown'
Twitter::YouAreNotFriends: You are not friends with the specified user.
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8.1/lib/twitter/base.rb:234:in `request'
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8.1/lib/twitter/base.rb:104:in `destroy_friendship'

>> twitter.create_friendship 'doriangray'
Twitter::RequestRefused: Response code 403: Forbidden Could not follow user: You can't follow yourself!
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8.1/lib/twitter/base.rb:235:in `request'
	from /Library/Ruby/Gems/1.8/gems/twitter-0.3.8.1/lib/twitter/base.rb:99:in `create_friendship'


Clone this wiki locally