The super fantastic way of getting Gravatars. By Chris.
In less than 5 minutes you can add Gravatars to your Ruby project. It works in Rails, Merb and plain ol' Ruby.
The best way to learn more about Gravtastic is to look at the 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
Add this to your environment.rb
:
config.gem 'gravtastic', :version => '>= 2.1.0'
Next, say that you want a Gravatar for your model:
class User < ActiveRecord::Base
is_gravtastic!
end
And you are done! In your views you can now use the #gravatar_url
method:
<%= image_tag @user.gravatar_url %>
If you want to change the image, you can do this:
<%= image_tag @user.gravatar_url(:rating => 'R', :secure => true) %>
That will show R rated Gravatars over a secure connection. If you find yourself repeating that all around your app, you can set the Gravatar defaults. In your model, just change the is_gravtastic!
line to something like this:
is_gravtastic :author_email, :secure => true,
:filetype => :gif,
:size => 120
Now all your Gravatars will come from a secure connection, be a GIF and be 120x120px. The email will also come from the author_email
field, not the default email
field. Don't worry, you arn't locked into these defaults (you can override them by passing options to #gravatar_url
like before).
If you want to check to see if your user has a real gravatar, not the default image you can do this:
<% if @user.gravatar_exists? %>
<%= image_tag @user.gravatar_url %>
<% else %>
<%= image_tag @user.gravatar_url %>
<%= link_to 'upload a profile picture at gravatar' 'http://gravatar.com/signup' %>
<% end %>
Note: You can use either is_gravtastic!
or is_gravtastic
, they both do the same thing.
So you just have a regular ol' Ruby app? No Rails and ActiveRecord?
require 'gravtastic'
class BoringUser
include Gravtastic
is_gravtastic!
end
And wallah! That works exactly the same as in Rails! Now all instances of the BoringUser class will have #gravatar_url
methods.
Note: the #gravatar_url
methods don't get included until you specify the class is_gravtastic!
Option | Description | Default | Values | |
---|---|---|---|---|
secure | Gravatar transmitted with SSL | false | true/false | |
size | The size of the image | 80 | 1..512 | |
default | The default avatar image | none | Any URL, or "identicon", "monsterid", "wavatar" | |
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 |
Fork the project, submit a pull request and I'll get to it straight away. Or you can just view the source like:
git clone git://github.com/chrislloyd/gravtastic.git
- Xavier Shay and others for Enki (the reason this was originally written)
- Matthew Moore
- Vincent Charles
- Paul Farnell
- Jeff Kreeftmeijer
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.
And there was some stuff added in 2010 by Tyler Montgomery. Its not really (c), its just code, and its open source, so use it, eat it, play with it, make money with it, get gangster with it.