Skip to content

Commit

Permalink
File type support and doc changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeering committed Jan 5, 2010
1 parent 64e96c3 commit a353d5f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
38 changes: 33 additions & 5 deletions README.textile
Expand Up @@ -33,7 +33,7 @@ Once you have installed it as a plugin for your rails app usage is simple.

<pre>gravatar_image_tag('spam@spam.com'.gsub('spam', 'mdeering'), :alt => 'Michael Deering')</pre>

*Boom* here is my gravatar !http://www.gravatar.com/avatar.php?gravatar_id=4da9ad2bd4a2d1ce3c428e32c423588a(Michael Deering)!
*Boom* here is my gravatar !http://www.gravatar.com/avata/4da9ad2bd4a2d1ce3c428e32c423588a(Michael Deering)!

h2. Configuration Points

Expand All @@ -43,7 +43,7 @@ If no default image is given in either an initializer or through the options pas

<pre>gravatar_image_tag('junk', :alt => 'Default Gravatar Image')</pre>

*Splat* the default gravatar image !http://www.gravatar.com/avatar.php?gravatar_id=0c821f675f132d790b3f25e79da739a7(Default Gravatar Image)!
*Splat* the default gravatar image !http://www.gravatar.com/avatar/0c821f675f132d790b3f25e79da739a7(Default Gravatar Image)!

You can specify the default image through an initializer as follows:

Expand All @@ -54,7 +54,13 @@ You can also specify the default image to fallback to or override the one set in

<pre>gravatar_image_tag('junk', :alt => 'Github Default Gravatar', :gravatar => { :default => 'http://github.com/images/gravatars/gravatar-80.png' })</pre>

*Ka-Pow* !http://www.gravatar.com/avatar.php?default=http%3A%2F%2Fgithub.com%2Fimages%2Fgravatars%2Fgravatar-80.png&gravatar_id=0c821f675f132d790b3f25e79da739a7(Github Default Gravatar)!
*Ka-Pow* !http://www.gravatar.com/avatar/0c821f675f132d790b3f25e79da739a7?default=http%3A%2F%2Fgithub.com%2Fimages%2Fgravatars%2Fgravatar-80.png(Github Default Gravatar)!

p. Other options supported besides an image url to fall back on include the following:
* :identicon !http://www.gravatar.com/avatar/0c821f675f132d790b3f25e79da739a7?default=identicon(Identicon Avatar)!
* :monsterid !http://www.gravatar.com/avatar/0c821f675f132d790b3f25e79da739a7?default=monsterid(Monster Id Avatar)!
* :wavatar !http://www.gravatar.com/avatar/0c821f675f132d790b3f25e79da739a7?default=wavatar(Wavatar Avatar)!
* 404: !http://www.gravatar.com/avatar/0c821f675f132d790b3f25e79da739a7?default=404(Not Found)!

h3. Setting the default image size

Expand All @@ -69,21 +75,43 @@ You can also specify the image size or override the one set in the plugin config

<pre>gravatar_image_tag('spam@spam.com'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :class => 'some-class', :gravatar => { :size => 15 })</pre>

*Mini Me!* !(some-class)http://www.gravatar.com/avatar.php?gravatar_id=4da9ad2bd4a2d1ce3c428e32c423588a&size=15(Michael Deering)!
*Mini Me!* !(some-class)http://www.gravatar.com/avatar/4da9ad2bd4a2d1ce3c428e32c423588a?size=15(Michael Deering)!

h3. Grabbing gravatars from the secure gravatar server.

p. You can make a request for a gravatar from the secure server at https://secure.gravatar.com by passing the _:gravatar => { :secure => true }_ option to the gravatar_image_tag call.

<pre>gravatar_image_tag('spam@spam.com'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :gravatar => { :secure => true } )</pre>

Delivered by a secure url! !https://secure.gravatar.com/avatar.php?gravatar_id=4da9ad2bd4a2d1ce3c428e32c423588a(Michael Deering)!
Delivered by a secure url! !https://secure.gravatar.com/avatar/4da9ad2bd4a2d1ce3c428e32c423588a(Michael Deering)!

p. You globally set the gravatar image tag to always use the secure server by setting the following. By default this is set to false.

<pre># config/initializers/gravatar_defaults.rb
ActionView::Base.secure_gravatar = true</pre>

h3. Using Gravatar's built in rating system

p. By default only those gravatars that have a rating of 'g' are returned. You can change this by passing in the rating parameter as follows. Possible options for ratings are _g, pg, r, or x_.

<pre>gravatar_image_tag('spam@spam.com'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :gravatar => { :rating => 'pg' } )</pre>

p. You can globally set the gravatar image tag rating by setting the following. There is no default value, the default of 'g' is being set by the gravatar service itself.

<pre># config/initializers/gravatar_defaults.rb
ActionView::Base.default_gravatar_rating = 'r'</pre>

h3. Specifying a filetype

p. By default no filetype extension is given in the produced gravatar urls but of your application requires it you can specify one to be added to the generated url.

<pre>gravatar_image_tag('spam@spam.com'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :gravatar => { :filetype => :gif } )</pre>

p. You can globally set the filetype extension as follows.

<pre># config/initializers/gravatar_defaults.rb
ActionView::Base.default_gravatar_filetype = :gif</pre>

h2. Credits

The ideas an methods for this plugin are from expanding upon my original blog post "Adding Gravatar To Your Website Or Blog (Gravatar Rails)":http://mdeering.com/posts/005-adding-gravitar-to-your-website-or-blog
Expand Down
16 changes: 13 additions & 3 deletions ROADMAP.textile
@@ -1,8 +1,18 @@
h1. 0.2.0

* Add model helpers for ActiveRecord and DataMapper

<pre>class Comment < ActiveRecord::Base
has_gravatar :author, :size => 120
end</pre>

<pre>Comment.first.gravatar_url</pre>

h1. 0.1.0

* -Support https calls to Gravatar-
* Support the ratings call to Gravatar
* Support for the filetype call to Gravatar
* -Add support for https calls to Gravatar-
* -Add support the different ratings call to Gravatar-
* -Add support for the filetype call to Gravatar-

h1. 0.0.1

Expand Down
2 changes: 1 addition & 1 deletion gravatar_image_tag.gemspec
Expand Up @@ -5,7 +5,7 @@

Gem::Specification.new do |s|
s.name = %q{gravatar_image_tag}
s.version = "0.0.1"
s.version = "0.0.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael Deering"]
Expand Down
25 changes: 17 additions & 8 deletions lib/gravatar_image_tag.rb
@@ -1,35 +1,44 @@
module GravatarImageTag

def self.included(base)
base.cattr_accessor :default_gravatar_image, :default_gravatar_rating,
:default_gravatar_size, :secure_gravatar
base.cattr_accessor :default_gravatar_filetype, :default_gravatar_image,
:default_gravatar_rating, :default_gravatar_size, :secure_gravatar
base.send :include, InstanceMethods
end

module InstanceMethods

def gravatar_image_tag(email, options = {})
options[:src] = gravatar_url( email, options.delete( :gravatar ) )
options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize
options[:alt] ||= 'Gravatar'
tag 'img', options, false, false # Patch submitted to rails to allow image_tag here https://rails.lighthouseapp.com/projects/8994/tickets/2878-image_tag-doesnt-allow-escape-false-option-anymore
end

def gravatar_url(email, overrides)
overrides ||= {}
url_params = {
gravatar_params = {
:default => ActionView::Base.default_gravatar_image,
:gravatar_id => Digest::MD5.hexdigest(email),
:filetype => ActionView::Base.default_gravatar_filetype,
:rating => ActionView::Base.default_gravatar_rating,
:secure => ActionView::Base.secure_gravatar,
:size => ActionView::Base.default_gravatar_size
}.merge(overrides).delete_if { |key, value| value.nil? }
"#{gravitar_url_base(url_params.delete(:secure))}?#{url_params.map { |key, value| "#{key}=#{URI.escape(value.is_a?(String) ? value : value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"}.join('&')}"
"#{gravatar_url_base(gravatar_params.delete(:secure))}/#{gravitar_id(email, gravatar_params.delete(:filetype))}#{url_params(gravatar_params)}"
end

private

def gravitar_url_base(secure = false)
'http' + (!!secure ? 's://secure.' : '://') + 'gravatar.com/avatar.php'
def gravatar_url_base(secure = false)
'http' + (!!secure ? 's://secure.' : '://') + 'gravatar.com/avatar'
end

def gravitar_id(email, filetype = nil)
"#{ Digest::MD5.hexdigest(email) }#{ ".#{filetype}" unless filetype.nil? }"
end

def url_params(gravatar_params)
return nil if gravatar_params.keys.count == 0
"?#{gravatar_params.map { |key, value| "#{key}=#{URI.escape(value.is_a?(String) ? value : value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"}.join('&')}"
end

end
Expand Down
12 changes: 8 additions & 4 deletions spec/gravatar_image_tag_spec.rb
Expand Up @@ -8,6 +8,7 @@

email = 'mdeering@mdeering.com'
md5 = '4da9ad2bd4a2d1ce3c428e32c423588a'
default_filetype = :gif
default_image = 'http://mdeering.com/images/default_gravatar.png'
default_image_escaped = 'http%3A%2F%2Fmdeering.com%2Fimages%2Fdefault_gravatar.png'
default_rating = 'x'
Expand All @@ -27,14 +28,16 @@
it "#gravatar_image_tag should create the provided url with the provided options #{options}" do
view = ActionView::Base.new
image_tag = view.gravatar_image_tag(email, options)
image_tag.include?("#{params.delete(:gravatar_id)}").should be_true
params.all? {|key, value| image_tag.include?("#{key}=#{value}")}.should be_true
end
end

{
:default_gravatar_image => default_image,
:default_gravatar_rating => default_rating,
:default_gravatar_size => default_size
:default_gravatar_image => default_image,
:default_gravatar_filetype => default_filetype,
:default_gravatar_rating => default_rating,
:default_gravatar_size => default_size
}.each do |singleton_variable, value|
it "should create gravatar class (singleton) variable #{singleton_variable} on its included class" do
ActionView::Base.send(singleton_variable).should == nil
Expand All @@ -53,6 +56,7 @@
it "#gravatar_image_tag should create the provided url when defaults have been set with the provided options #{options}" do
view = ActionView::Base.new
image_tag = view.gravatar_image_tag(email, options)
image_tag.include?("#{params.delete(:gravatar_id)}.#{default_filetype}").should be_true
params.all? {|key, value| image_tag.include?("#{key}=#{value}")}.should be_true
end
end
Expand All @@ -62,7 +66,7 @@
end

it 'should request the gravitar image from the secure server if the https => true option is given' do
(!!view.gravatar_image_tag(email, { :gravatar => { :secure => true } }).match(/src="https:\/\/secure.gravatar.com\/avatar.php/)).should be_true
(!!view.gravatar_image_tag(email, { :gravatar => { :secure => true } }).match(/src="https:\/\/secure.gravatar.com\/avatar\//)).should be_true
end

end

0 comments on commit a353d5f

Please sign in to comment.