gravatar_image_tag 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.textile +78 -0
- data/Rakefile +50 -0
- data/lib/gravatar_image_tag.rb +32 -0
- data/spec/gravatar_image_tag_spec.rb +57 -0
- data/spec/test_helper.rb +11 -0
- metadata +61 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Michael Deering
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
!http://s.gravatar.com/images/logo.png(Gravatar Logo)!
|
2
|
+
|
3
|
+
h1. Gravatar Image Tag Plugin
|
4
|
+
|
5
|
+
Rails view helper for grabbing "Gravatar":http://en.gravatar.com/ images. The goal here is to be configurable and have those configuration points documented!
|
6
|
+
|
7
|
+
h2. Install as a Ruby Gem
|
8
|
+
|
9
|
+
<pre>sudo gem install gravatar_image_tag</pre>
|
10
|
+
|
11
|
+
p. Include the following line in your Rails environment
|
12
|
+
|
13
|
+
<pre>config.gem 'gravatar_image_tag'</pre>
|
14
|
+
|
15
|
+
p. Then ensure the gem is installed if you did not already run the gem install as noted above.
|
16
|
+
|
17
|
+
<pre>rake gems:install</pre>
|
18
|
+
|
19
|
+
h2. Install as a Ruby on Rails Plugin
|
20
|
+
|
21
|
+
The traditional way.
|
22
|
+
<pre>./script/plugin install git://github.com/mdeering/gravatar_image_tag.git</pre>
|
23
|
+
|
24
|
+
or the old-school but still c00l way!
|
25
|
+
<pre>piston import git://github.com/mdeering/gravatar_image_tag.git vendor/plugins/gravatar_image_tag</pre>
|
26
|
+
|
27
|
+
or for all you hip gitsters.
|
28
|
+
<pre>git submodule add git://github.com/mdeering/gravatar_image_tag.git vendor/plugins/gravatar_image_tag git submodule init</pre>
|
29
|
+
|
30
|
+
h2. Usage
|
31
|
+
|
32
|
+
Once you have installed it as a plugin for your rails app usage is simple.
|
33
|
+
|
34
|
+
<pre>gravatar_image_tag('spam@spam.com'.gsub('spam', 'mdeering'), :alt => 'Michael Deering')</pre>
|
35
|
+
|
36
|
+
*Boom* here is my gravatar !http://www.gravatar.com/avatar.php?gravatar_id=4da9ad2bd4a2d1ce3c428e32c423588a(Michael Deering)!
|
37
|
+
|
38
|
+
h2. Configuration Points
|
39
|
+
|
40
|
+
h3. Setting the default image
|
41
|
+
|
42
|
+
If no default image is given in either an initializer or through the options passed to the gravatar_image_tag then the default image from the gravatar site will be displayed.
|
43
|
+
|
44
|
+
<pre>gravatar_image_tag('junk', :alt => 'Default Gravatar Image')</pre>
|
45
|
+
|
46
|
+
*Splat* the default gravatar image !http://www.gravatar.com/avatar.php?gravatar_id=0c821f675f132d790b3f25e79da739a7(Default Gravatar Image)!
|
47
|
+
|
48
|
+
You can specify the default image through an initializer as follows:
|
49
|
+
|
50
|
+
<pre># config/initializers/gravatar_defaults.rb
|
51
|
+
ActionView::Base.default_gravatar_image = 'http://assets.yourhost.com/images/default_gravatar.png'</pre>
|
52
|
+
|
53
|
+
You can also specify the default image to fallback to or override the one set in the above initializer by passing the _:gravatar => { :default => 'http://assets.yourhost.com/images/override_gravatar.png' }_ option to the gravatar_image_tag call. All other options will be forwarded onto the image_tag.
|
54
|
+
|
55
|
+
<pre>gravatar_image_tag('junk', :alt => 'Github Default Gravatar', :gravatar => { :default => 'http://github.com/images/gravatars/gravatar-80.png' })</pre>
|
56
|
+
|
57
|
+
*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)!
|
58
|
+
|
59
|
+
h3. Setting the default image size
|
60
|
+
|
61
|
+
If no default image size is given in either the plugin configuration or through the options passed to the gravatar_image_tag then the default image size from the gravatar site will be displayed (80px x 80px at the time of this writing).
|
62
|
+
|
63
|
+
You can specify your default image size through the plugin configuration by adding the following line to your rails environment.
|
64
|
+
|
65
|
+
<pre># config/initializers/gravatar_defaults.rb
|
66
|
+
ActionView::Base.default_gravatar_size = 120</pre>
|
67
|
+
|
68
|
+
You can also specify the image size or override the one set in the plugin configuration by passing the _:gravatar => { :size => 50 }_ option to the gravatar_image_tag call. All other options will be forwarded on to the image_tag call.
|
69
|
+
|
70
|
+
<pre>gravatar_image_tag('spam@spam.com'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :class => 'some-class', :gravatar => { :size => 15 })</pre>
|
71
|
+
|
72
|
+
*Mini Me!* !(some-class)http://www.gravatar.com/avatar.php?gravatar_id=4da9ad2bd4a2d1ce3c428e32c423588a&size=15(Michael Deering)!
|
73
|
+
|
74
|
+
h2. Credits
|
75
|
+
|
76
|
+
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
|
77
|
+
|
78
|
+
Copyright (c) 2009 "Michael Deering(Ruby on Rails Development Edmonton)":http://mdeering.com, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
AUTHOR = "Michael Deering"
|
7
|
+
EMAIL = "mdeering@mdeering.com"
|
8
|
+
GEM = "gravatar_image_tag"
|
9
|
+
HOMEPAGE = "http://github.com/mdeering/gravatar_image_tag"
|
10
|
+
SUMMARY = "A configurable and documented Rails view helper for adding gravatars into your Rails application."
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |s|
|
14
|
+
s.author = AUTHOR
|
15
|
+
s.email = EMAIL
|
16
|
+
s.files = %w(install.rb install.txt MIT-LICENSE README.textile Rakefile) + Dir.glob("{rails,lib,spec}/**/*")
|
17
|
+
s.homepage = HOMEPAGE
|
18
|
+
s.name = GEM
|
19
|
+
s.require_path = 'lib'
|
20
|
+
s.summary = SUMMARY
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Default: spec tests.'
|
28
|
+
task :default => :spec
|
29
|
+
|
30
|
+
desc 'Test the attribute_normalizer plugin.'
|
31
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
32
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
33
|
+
t.spec_opts = ["-c"]
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Run all examples with RCov"
|
37
|
+
Spec::Rake::SpecTask.new('examples_with_rcov') do |t|
|
38
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
39
|
+
t.rcov = true
|
40
|
+
t.rcov_opts = ['--exclude', '/opt,spec,Library']
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'Generate documentation for the attribute_normalizer plugin.'
|
44
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = 'AttributeNormalizer'
|
47
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
48
|
+
rdoc.rdoc_files.include('README.textile')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module GravatarImageTag
|
2
|
+
|
3
|
+
GRAVATAR_BASE_URL = 'http://www.gravatar.com/avatar.php'
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.cattr_accessor :default_gravatar_image, :default_gravatar_size
|
7
|
+
base.send :include, InstanceMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module InstanceMethods
|
11
|
+
|
12
|
+
def gravatar_image_tag(email, options = {})
|
13
|
+
options[:src] = gravatar_url( email, options.delete( :gravatar ) )
|
14
|
+
options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize
|
15
|
+
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
|
16
|
+
end
|
17
|
+
|
18
|
+
def gravatar_url(email, overrides)
|
19
|
+
overrides ||= {}
|
20
|
+
url_params = {
|
21
|
+
:default => ActionView::Base.default_gravatar_image,
|
22
|
+
:gravatar_id => Digest::MD5.hexdigest(email),
|
23
|
+
:size => ActionView::Base.default_gravatar_size
|
24
|
+
}.merge(overrides).delete_if { |key, value| value.nil? }
|
25
|
+
"#{GRAVATAR_BASE_URL}?#{url_params.map { |key, value| "#{key}=#{URI.escape(value.is_a?(String) ? value : value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"}.join('&')}"
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
ActionView::Base.send(:include, GravatarImageTag)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
require 'gravatar_image_tag'
|
4
|
+
|
5
|
+
ActionView::Base.send(:include, GravatarImageTag)
|
6
|
+
|
7
|
+
describe GravatarImageTag do
|
8
|
+
|
9
|
+
email = 'mdeering@mdeering.com'
|
10
|
+
md5 = '4da9ad2bd4a2d1ce3c428e32c423588a'
|
11
|
+
default_image = 'http://mdeering.com/images/default_gravatar.png'
|
12
|
+
default_image_escaped = 'http%3A%2F%2Fmdeering.com%2Fimages%2Fdefault_gravatar.png'
|
13
|
+
default_size = 50
|
14
|
+
other_image = 'http://mdeering.com/images/other_gravatar.png'
|
15
|
+
other_image_escaped = 'http%3A%2F%2Fmdeering.com%2Fimages%2Fother_gravatar.png'
|
16
|
+
|
17
|
+
view = ActionView::Base.new
|
18
|
+
|
19
|
+
{
|
20
|
+
{ :gravatar_id => md5 } => {},
|
21
|
+
{ :gravatar_id => md5, :size => 30 } => { :gravatar => {:size => 30 } },
|
22
|
+
{ :gravatar_id => md5, :default => other_image_escaped } => { :gravatar => {:default => other_image } },
|
23
|
+
{ :gravatar_id => md5, :default => other_image_escaped, :size => 30 } => { :gravatar => {:default => other_image, :size => 30 } }
|
24
|
+
}.each do |params, options|
|
25
|
+
it "#gravatar_image_tag should create the provided url with the provided options #{options}" do
|
26
|
+
view = ActionView::Base.new
|
27
|
+
image_tag = view.gravatar_image_tag(email, options)
|
28
|
+
params.all? {|key, value| image_tag.include?("#{key}=#{value}")}.should be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
{
|
33
|
+
:default_gravatar_image => default_image,
|
34
|
+
:default_gravatar_size => default_size
|
35
|
+
}.each do |singleton_variable, value|
|
36
|
+
it "should create gravatar class (singleton) variable #{singleton_variable} on its included class" do
|
37
|
+
ActionView::Base.send(singleton_variable).should == nil
|
38
|
+
ActionView::Base.send("#{singleton_variable}=", value)
|
39
|
+
ActionView::Base.send(singleton_variable).should == value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Now that the defaults are set...
|
44
|
+
{
|
45
|
+
{ :gravatar_id => md5, :size => default_size, :default => default_image_escaped } => {},
|
46
|
+
{ :gravatar_id => md5, :size => 30, :default => default_image_escaped } => { :gravatar => { :size => 30 }},
|
47
|
+
{ :gravatar_id => md5, :size => default_size, :default => other_image_escaped } => { :gravatar => {:default => other_image } },
|
48
|
+
{ :gravatar_id => md5, :size => 30, :default => other_image_escaped } => { :gravatar => { :default => other_image, :size => 30 }},
|
49
|
+
}.each do |params, options|
|
50
|
+
it "#gravatar_image_tag should create the provided url when defaults have been set with the provided options #{options}" do
|
51
|
+
view = ActionView::Base.new
|
52
|
+
image_tag = view.gravatar_image_tag(email, options)
|
53
|
+
params.all? {|key, value| image_tag.include?("#{key}=#{value}")}.should be_true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/spec/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gravatar_image_tag
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Deering
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-04 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: mdeering@mdeering.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.textile
|
24
|
+
files:
|
25
|
+
- MIT-LICENSE
|
26
|
+
- README.textile
|
27
|
+
- Rakefile
|
28
|
+
- lib/gravatar_image_tag.rb
|
29
|
+
- spec/gravatar_image_tag_spec.rb
|
30
|
+
- spec/test_helper.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/mdeering/gravatar_image_tag
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.3.5
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: A configurable and documented Rails view helper for adding gravatars into your Rails application.
|
59
|
+
test_files:
|
60
|
+
- spec/gravatar_image_tag_spec.rb
|
61
|
+
- spec/test_helper.rb
|