Gravatar is a web based avatar service. You sign up, upload your avatar, and associate it with your email address(es). Now when you visit a site leveraging Gravatar and enter your email address (like comments on a blog), your avatar will automatically appear!
So today I decided to implement Gravatar on this blog, and here’s what I came up with.
If you are using this with Ruby on Rails, then add the code to you Application Helper (application_helper.rb). You may ommit the require line at the top, as this is already included in your Rails environment.
Generate A Gravatar URL
# This line is not needed for Rails.
require 'digest/md5'
# Returns a Gravatar URL associated with the email parameter.
def gravatar_url(email,gravatar_options={})
# Default highest rating.
# Rating can be one of G, PG, R X.
# If set to nil, the Gravatar default of X will be used.
gravatar_options[:rating] ||= nil
# Default size of the image.
# If set to nil, the Gravatar default size of 80px will be used.
gravatar_options[:size] ||= nil
# Default image url to be used when no gravatar is found
# or when an image exceeds the rating parameter.
gravatar_options[:default] ||= nil
# Build the Gravatar url.
grav_url = 'http://www.gravatar.com/avatar.php?'
grav_url << "gravatar_id=#{Digest::MD5.new.update(email)}"
grav_url << "&rating=#{gravatar_options[:rating]}" if gravatar_options[:rating]
grav_url << "&size=#{gravatar_options[:size]}" if gravatar_options[:size]
grav_url << "&default=#{gravatar_options[:default]}" if gravatar_options[:default]
return grav_url
end
All we have to do to get a Gravatar URL is:
gravatar_url('test@gravatar.com')
=> "http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0"
Use the @gravatar_options hash when you want to adjust the size, rating, or add a default URL for when there is no Gravatar.
gravatar_url('test@gravatar.com',{ :size => 20 })
=> "http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0&size=20"
For more detail on the particulars of the options, see the online documentation.
Generate Gravatar HTML
If you want to generate an mg tag from this, something like the following is what you want.
# Returns a Gravatar image tag associated with the email parameter.
def gravatar(email,gravatar_options={})
# Set the img alt text.
alt_text = 'Gravatar'
# Sets the image sixe based on the gravatar_options.
img_size = gravatar_options.include?(:size) ? gravatar_options[:size] : '80'
"<img src=\"#{gravatar_url(email, gravatar_options)}\" alt=\"#{alt_text}\" height=\"#{img_size}\" width=\"#{img_size}\" />"
end
Now if we run our examples from above…
gravatar('test@gravatar.com')
=> "<img src="http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0&rating=PG" alt="Gravatar" height="80" width="80" />"
And with options…
gravatar('test@gravatar.com',{ :size => 20 })
=> "<img src="http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0&rating=PG&size=20" alt="Gravatar" height="20" width="20" />"
Enjoy!
Taking it further.
A good enhancement to this would be to cache the Gravatars locally, to speed up their load times. Look out for a future blog post on this.
Tags
Ruby on rails, Ruby, Gravatar.
Related Posts
- Acts_As_Indexed now on GitHub
- JQuery on Rails
- Mod_Rails aka Passenger Migration and Review
- Scotland On Rails 2008 - Day Two
- Scotland On Rails 2008 - Day One





