Creating Gravatar URLs with Ruby

A gravatar, or globally recognized avatar, is quite simply an 80×80 pixel avatar image that follows you from weblog to weblog appearing beside your name when you comment on gravatar enabled sites. Avatars help identify your posts on web forums, so why not on weblogs?

Here’s some Ruby code to generate a gravatar URL:

require "digest/md5" 
require "query_string" 

# Options: size, default, border, rating
def url_for_gravatar(email, options = {})
  options[:gravatar_id] = Digest::MD5.hexdigest(email)
  "http://www.gravatar.com/avatar.php?" + options.to_query_string
end

You find to_query_string on Hash#to_query_string.

Usage

url_for_gravatar("[email protected]", :size => 40)