The super fantastic way of getting Gravatars. By The Poacher.
_____ _ _ _ / ____| | | | | (_) | | __ _ __ __ ___ _| |_ __ _ ___| |_ _ ___ | | |_ | '__/ _` \ \ / / __/ _` / __| __| |/ __| | |__| | | | (_| |\ V /| || (_| \__ \ |_| | (__ \_____|_| \__,_| \_/ \__\__,_|___/\__|_|\___|
In less than a minute you can add Gravatars to your Ruby project. It works in Rails, Merb & Sinatra.
The best way to learn more about Gravtastic is to look through the annotated source. It's one file, about 80 LOC and really pretty simple. If that isn't for you, then follow the instructions below!
sudo gem install gravtastic
For this example I'm going to assume you are using Rails. Don't worry if you arn't, the concepts are still the same.
First off, add this to your
Next, in your model:
class User < ActiveRecord::Base include Gravtastic gravtastic end
Note: You can use either
has_gravatar, they all do the same thing.
And you are done! In your views you can now use the
#gravatar_url method on instances of
<%= image_tag @user.gravatar_url %>
Gravatar gives you some options. You can use them like this:
<%= image_tag @user.gravatar_url(:rating => 'R', :secure => true) %>
That will show R rated Gravatars over a secure connection. If you find yourself using the same options over and over again, you can set the Gravatar defaults. In your model, just change the
is_gravtastic line to something like this:
gravtastic :secure => true, :filetype => :gif, :size => 120
Now all your Gravatars will come from a secure connection, be a GIF and be 120x120px.
Gravatar needs an email address to find the person's avatar. By default, Gravtastic calls the
A common question is "how do I detect wether the user has an avatar or not?" People usually write code to perform a HTTP request to Gravatar to see wether the gravatar exists. This is certainly a solution, but not a very good one. If you have page where you show 50 users, the client will have to wait for 50 HTTP requests before they even get the page. Slooww.
The best way to do this is to set the
:default option when using
#gravatr_url. If the user doesn't have an avatar, Gravatar essentially redirects to the "default" url you provide.
Complete List of Options
|secure||Gravatar transmitted with SSL||false||true/false|
|size||The size of the image||80||1..512|
|default||The default avatar image||none||"identicon", "monsterid", "wavatar" or an absolute URL.|
|rating||The lowest level of ratings you want to allow||G||G, PG, R or X|
|filetype||The filetype of the image||png||gif, jpg or png|
Gravatar is really just simple Ruby. There is no special magic which ties it to one ORM (like ActiveRecord or MongoMapper). You can use the following pattern to include it anywhere:
require 'gravtastic' class MyClass include Gravtastic is_gravtastic end
For instance, with the excellent MongoMapper you can use do this:
class Person include MongoMapper::Document include Gravtastic is_gravtastic key :email end
And wallah! It's exactly the same as with ActiveRecord! Now all instances of the
Person class will have
#gravatar_url methods don't get included until you specify the class
Making Changes Yourself
Gravtastic is a mature project. There isn't any active work which needs to be done on it, but I do continue to maintain it. Just don't expect same day fixes. If you find something that needs fixing, the best way to contribute is to fork the repo and submit a pull request.
git clone git://github.com/chrislloyd/gravtastic.git
- Xavier Shay and others for Enki (the reason this was originally written)
- Matthew Moore
- Galen O'Hanlon
- Jason Cheow
- Paul Farnell
- Jeff Kreeftmeijer
- Ryan Lewis
- Arthur Chiu
Copyright (c) 2008 Chris Lloyd.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.