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

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.

Tags

, , .

Related Posts

October 27th 2007 13:52 | comments (5)
 

Comments


Gravatar

Douglas F Shearer

October 27th 2007 14:10

Look! My comment has a Gravatar!

Gravatar

Aimee

November 24th 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 27th 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 20th 2008 00:46

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

Gravatar

Fabian Ramirez

May 10th 2008 17:32

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

:)

Add Your Comments


(Required)

Your email address to get your Gravatar. Address itself is not shown.

(Include the http://)

(Required)

 

You Are Here


Douglas F Shearer

This is the homepage of Douglas F Shearer, a software developer and mountainbike racer. Find out more at the About page.

Gallery Latest


Side on Chips on crown Front on chip2 chip1 img67

Stay Informed


What is RSS?

Top Tags