Don't Reinvent The Wheel

April 29th, 2007

Next time you install attachment_fu and begin to create an avatar model, stop and consider Gravatar.com. Gravatar allows people to store avatars linked to their email addresses. You probably have heard of Gravatar before but did you know there is a really easy ruby lib for integrating it into your Rails applications?

If you did, pat yourself on the back and move on to the next article in your feed reader but if you didn’t, check out the code below. It assumes that you have installed the gravatar gem (sudo gem install gravatar).

require 'rubygems'
require 'gravatar'

puts Gravatar.new('nunemaker@gmail.com').to_s
# => http://gravatar.com/avatar.php?gravatar_id=e13c31390e0369fcd5972292ce0e7b92&size=80

Yep, that’s it. If the gravatar_id exists, you get the url to a person’s avatar and if it doesn’t, you get a simple default avatar from gravatar.com.

Personally, I’m tired of uploading my avatar each time I create an account. Let’s build on what others have created instead of constantly reinventing the wheel. Mix this with some openid and you have really simple account registration, authentication and personal avatars.

5 Responses to “Don't Reinvent The Wheel”

  1. Avi Says:

    Hey, that’s awesome, thanks for the tip! Just one question, can this create avatars for the user? As in, I don’t want to ask the users to go to gravatar to create an avatar, so if they upload a pic on my site, can I create the gravatar for them using their email address?

  2. John Nunemaker Says:

    @Avi – Nope, there doesn’t appear to be an api for creating images. I would recommend requesting it. I think it would be a good addition as well.

  3. Beno Says:

    Be wary of gravatars. I used this in a an application and although I loved the idea, my users did not. Not only that, it ended up being very slow and unreliable. This was about 6 months ago however, and if their load issues have been worked out, maybe it won’t rub your users the wrong way. I found that my users wanted avatars per site, depending on the content/context.

  4. Beate Says:

    That’s nice, indeed, but gravatar had some hiccups the last month, their server was down, and then you hat lots of broken images on your site. All has pros and cons ;)

  5. John Nunemaker Says:

    All very true comments. They seem to be growing through some growing pains but I think they’ll come out of it before too long, hopefully. :)

Leave a Reply


(textile enabled)