Douglas F Shearer

Gravatar For Ruby And Ruby On Rails


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 require is not needed for Rails.
require 'digest/md5'

# Returns a Gravatar URL associated with the email parameter.
#
# Gravatar Options:
# - rating: Can be one of G, PG, R or X. Default is X.
# - size: Size of the image. Default is 80px.
# - default: URL for fallback image if none is found or image exceeds rating.
def gravatar_url(email,gravatar_options={})
  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]
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 img 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" />"

Gravatar

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" />"

Gravatar

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.

 

Comments


Gravatar

Douglas F Shearer

October 27 2007 14:10

Look! My comment has a Gravatar!

Gravatar

Aimee

November 24 2007 20:04

Hi, thanks very much for this code. It worked a treat, very easy to implement! I'll be very interested in the automatic caching if you work out a way to do it.

I've given you a mention here:
http://journal.my...

Thanks again.
Aimee.

Gravatar

Douglas F Shearer

November 27 2007 20:08

Hi!

Got round to trying out one caching option this morning; calling a controller and redirecting to the cached image if it exists, or fetching then redirecting if it doesn't exist. It was pretty inconsistent in fetching images properly, and was pretty slow.

Will try another version tomorrow, but using a cron task to fetch the Gravatars outside client run time.

Gravatar

Adam Fortuna

April 20 2008 00:46

Awesome, worked on the first shot. See why this was first on Google for rails w/gravatar.

Gravatar

Fabian Ramirez

May 10 2008 17:32

Nice work dear! .. but i putted both methods into my user_helper.rb

:)

Gravatar

Ian Stewart

February 18 2009 08:06

I have this working which is great.
Any way to know when Gravatar is returning
a default image so I can provide my user
for more details on how to signup for their custom
Gravatar?

Again this worked great thanks.

Gravatar

al

April 22 2009 00:29

Gravatar test.. Sry >.<

Add Your Comments


Commenting is closed for this entry.

 

You Are Here


Douglas F Shearer

This is the homepage of Douglas F Shearer, a software developer and mountainbike racer. More…

Hire Me!


I'm available for hire. Ruby, Java and PHP work, both remotely (Worldwide) and locally (Scotland). Find out more or email me.

Flickr Latest


Stay Informed


What is RSS?

Categories


  1. Bike (93)
  2. Coding (85)
  3. Other (46)

Top Tags