gravtastic 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Chris Lloyd
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,90 @@
1
+ Easily add "Gravatars":http://gravatar.com to your Ruby objects.
2
+
3
+
4
+ h2. Install
5
+
6
+ sudo gem install chrislloyd-gravtastic --source=http://gems.github.com
7
+
8
+
9
+ h2. Usage
10
+
11
+ Somewhere in your application you need to @require 'gravtastic'@. In Rails >= 2.1 you can forego this and just add a dependency gem dependency for Gravtastic.
12
+
13
+ config.gem 'gravtastic', :source => 'http://gems.github.com/'
14
+
15
+ The next step is to give your model a Gravatar:
16
+
17
+ class User
18
+ has_gravatar
19
+ end
20
+
21
+ If you are using a standard Ruby class or Datamapper resource you have to add the line @include Gravtastic::Model@ before @has_gravatar@.
22
+
23
+ This defaults to looking for the gravatar ID on the @email@ method. So, if your @User@ has an @email@ then it will send that to Gravatar to get their picture. You can change the default gravatar source like this:
24
+
25
+ has_gravatar :on => :author_email
26
+
27
+ Now, you can access your object's gravatar with the @gravatar_url@ method:
28
+
29
+ current_user.gravatar_url
30
+ => "http://gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG"
31
+
32
+ Note that it defaults to a PG rating. You can specify extra options with a hash:
33
+
34
+ current_user.gravatar_url(:rating => 'R18', :size => 512)
35
+ => "http://gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG&s=512"
36
+
37
+ current_user.gravatar_url(:secure => true)
38
+ => "https://secure.gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG"
39
+
40
+ PS. Bonus points for anybody who can tell me what that users email is!
41
+ PPS. Shame on Automattic for still using MD5!
42
+
43
+
44
+ h2. The Future
45
+
46
+ * "Datamapper":http://datamaper.org support
47
+
48
+
49
+ h2. Authors
50
+
51
+ * "Chris Lloyd":http://chrislloyd.com.au
52
+ * You
53
+
54
+
55
+ h2. Contribute
56
+
57
+ You can checkout the source or fork it yourself from Github.
58
+
59
+ git clone git://github.com/chrislloyd/gravtastic.git
60
+
61
+ If you submit a successful patch then you'll be given full commit rights to the project.
62
+
63
+
64
+ h2. Thanks
65
+
66
+ * "Xavier Shay":http://rhnh.net and others for "Enki":http://enkiblog.com (the reason this was written)
67
+
68
+
69
+ h2. License
70
+
71
+ Copyright (c) 2008 Chris Lloyd.
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining
74
+ a copy of this software and associated documentation files (the
75
+ 'Software'), to deal in the Software without restriction, including
76
+ without limitation the rights to use, copy, modify, merge, publish,
77
+ distribute, sublicense, and/or sell copies of the Software, and to
78
+ permit persons to whom the Software is furnished to do so, subject to
79
+ the following conditions:
80
+
81
+ The above copyright notice and this permission notice shall be
82
+ included in all copies or substantial portions of the Software.
83
+
84
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
85
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
86
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
87
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
88
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
89
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
90
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rubygems/specification'
4
+ require 'spec/rake/spectask'
5
+ require 'date'
6
+
7
+ GEM = "gravtastic"
8
+ GEM_VERSION = "1.5.2"
9
+ AUTHOR = "Chris Lloyd"
10
+ EMAIL = "christopher.lloyd@gmail.com"
11
+ HOMEPAGE = "http://github.com/chrislloyd/gravtastic"
12
+ SUMMARY = "Easily add Gravatars to your Ruby objects."
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.rubyforge_project = GEM
19
+ s.has_rdoc = true
20
+ s.extra_rdoc_files = ['README.textile', 'LICENSE']
21
+ s.summary = SUMMARY
22
+ s.description = s.summary
23
+ s.author = AUTHOR
24
+ s.email = EMAIL
25
+ s.homepage = HOMEPAGE
26
+
27
+ # Uncomment this to add a dependency
28
+ # s.add_dependency "foo"
29
+
30
+ s.require_path = 'lib'
31
+ s.files = %w(LICENSE README.textile Rakefile) + Dir.glob("{lib,spec}/**/*")
32
+ end
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.gem_spec = spec
36
+ end
37
+
38
+ task :default => :spec
39
+ task :specs => :spec
40
+
41
+ desc "Run all examples"
42
+ Spec::Rake::SpecTask.new('spec') do |t|
43
+ t.spec_opts = ['--options','spec/spec.opts']
44
+ t.spec_files = FileList['spec/**/*.rb']
45
+ t.rcov = true
46
+ end
47
+
48
+ desc "install the gem locally"
49
+ task :install => [:package] do
50
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
51
+ end
52
+
53
+ desc "create a gemspec file"
54
+ task :make_spec do
55
+ File.open("#{GEM}.gemspec", "w") do |file|
56
+ file.puts spec.to_ruby
57
+ end
58
+ end
59
+
60
+ task :bugs do
61
+ sh %{ditz html ; open html/index.html}
62
+ end
data/lib/gravtastic.rb ADDED
@@ -0,0 +1,137 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'digest/md5'
5
+ require 'cgi'
6
+
7
+ # = Gravtastic - Easily add Gravatars to your Ruby objects.
8
+ #
9
+ # Copyright (C) 2008 mailto:christopher.lloyd@gmail.com
10
+ #
11
+ # == Example
12
+ #
13
+ # Somewhere in your application you need to <tt>require 'gravtastic'</tt>. In Rails >= 2.1 you can forego this and just add a dependency gem dependency for Gravtastic.
14
+ #
15
+ # config.gem 'gravtastic', :source => 'http://gems.github.com/'
16
+ #
17
+ # The next step is to give your model a Gravatar:
18
+ #
19
+ # class User
20
+ # has_gravatar
21
+ # end
22
+ #
23
+ # If you are using a standard Ruby class or Datamapper resource you have to add the line <tt>include Gravtastic::Model</tt> before <tt>has_gravatar</tt>.
24
+ #
25
+ # This defaults to looking for the gravatar ID on the <tt>email</tt> method. So, if your <tt>User</tt> has an <tt>email</tt> then it will send that to Gravatar to get their picture. You can change the default gravatar source like this:
26
+ #
27
+ # has_gravatar :on => :author_email
28
+ #
29
+ # Now, you can access your object's gravatar with the <tt>gravatar_url</tt> method:
30
+ #
31
+ # current_user.gravatar_url
32
+ # => "http://gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG"
33
+ #
34
+ # Note that it defaults to a PG rating. You can specify extra options with a hash:
35
+ #
36
+ # current_user.gravatar_url(:rating => 'R18', :size => 512)
37
+ # => "http://gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG&s=512"
38
+ #
39
+ # current_user.gravatar_url(:secure => true)
40
+ # => "https://secure.gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG"
41
+ #
42
+ module Gravtastic
43
+ module Model
44
+
45
+ def self.included(base) # :nodoc:
46
+ base.extend(ClassMethods)
47
+ end
48
+
49
+ module ClassMethods
50
+
51
+ #
52
+ # Sets the gravatar_source. Basically starts this whole shindig.
53
+ #
54
+ # Examples:
55
+ #
56
+ # has_gravatar
57
+ #
58
+ # has_gravatar :on => :author_email
59
+ #
60
+ def has_gravatar(options={:on => :email})
61
+ @gravatar_source = options[:on]
62
+ end
63
+
64
+ #
65
+ # Returns a symbol of the instance method where the Gravatar is pulled from.
66
+ #
67
+ # For example, if your users email is returned by the method <tt>#gobagaldy_gook</tt> then it
68
+ # will return the symbol <tt>:'gobagaldy_gook'</tt>.
69
+ #
70
+ def gravatar_source
71
+ @gravatar_source
72
+ end
73
+
74
+ #
75
+ # Returns <tt>true</tt> if the gravatar_source is set. <tt>false</tt> if not. Easy!
76
+ #
77
+ def has_gravatar?
78
+ !!gravatar_source
79
+ end
80
+
81
+ end
82
+
83
+ #
84
+ # The raw MD5 hash used by Gravatar, generated from the ClassMethods#gravatar_source.
85
+ #
86
+ def gravatar_id
87
+ if self.class.gravatar_source && value = send(self.class.gravatar_source).downcase
88
+ @gravatar_id = Digest::MD5.hexdigest(value.to_s)
89
+ end
90
+ end
91
+
92
+ #
93
+ # Returns a string with the URL for the instance's Gravatar.
94
+ #
95
+ # It defaults to <tt>:rating => 'PG'</tt>
96
+ #
97
+ # Examples:
98
+ #
99
+ # current_user.gravatar_url
100
+ # => "http://gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG"
101
+ #
102
+ # current_user.gravatar_url(:rating => 'R18', :size => 512, :default => 'http://example.com/images/example.jpg')
103
+ # => "http://gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?d=http%3A%2F%2Fexample.com%2Fimages%2Fexample.jpg&r=R18&s=512"
104
+ #
105
+ # current_user.gravatar_url(:secure => true)
106
+ # => "https://secure.gravatar.com/e9e719b44653a9300e1567f09f6b2e9e.png?r=PG"
107
+ #
108
+ def gravatar_url(options={})
109
+ options[:rating] ||= 'PG'
110
+ options[:secure] ||= false
111
+ if gravatar_id
112
+ @gravatar_url = 'http' + (options[:secure] ? 's://secure.' : '://') + 'gravatar.com/avatar/' + gravatar_id + '.png' + parse_url_options_hash(options)
113
+ end
114
+ end
115
+
116
+ private
117
+
118
+ GRAVTASTIC_VALID_PARAMS = [:size, :rating, :default]
119
+
120
+ def parse_url_options_hash(options)
121
+
122
+ options.delete_if { |key,_| ![:size,:rating,:default].include?(key) }
123
+
124
+ unless options.empty?
125
+ '?' + options.map do |pair|
126
+ pair[0] = pair[0].to_s[0,1] # Get the first character of the option
127
+ pair.map{|item| item = CGI::escape(item.to_s) }.join('=') # Join key & value together
128
+ end.sort.join('&') # Join options together
129
+ else
130
+ ''
131
+ end
132
+ end
133
+
134
+ end
135
+ end
136
+
137
+ ActiveRecord::Base.send(:include, Gravtastic::Model) if defined?(ActiveRecord) # :nodoc:
@@ -0,0 +1,158 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "Gravtastic::Model" do
4
+
5
+ before(:each) do
6
+ @user = mock('User')
7
+ @user.class.send(:include, Gravtastic::Model)
8
+ @klass = @user.class
9
+ end
10
+
11
+ describe ".gravatar_source" do
12
+
13
+ it "is nil if unset" do
14
+ @klass.gravatar_source.should be_nil
15
+ end
16
+
17
+ end
18
+
19
+ describe ".has_gravatar" do
20
+
21
+ it "sets .gravatar_source to email by default" do
22
+ @klass.has_gravatar
23
+ @klass.gravatar_source.should == :email
24
+ end
25
+
26
+ it "changes .gravatar_source" do
27
+ lambda {
28
+ @klass.has_gravatar :on => :other_method
29
+ }.should change(@klass, :gravatar_source)
30
+ end
31
+
32
+ it "sets .gravatar_source to the value of :on" do
33
+ @klass.has_gravatar :on => :other_method
34
+ @klass.gravatar_source.should == :other_method
35
+ end
36
+
37
+ end
38
+
39
+ describe ".has_gravatar?" do
40
+
41
+ it "is true when .gravatar_source is not nil" do
42
+ @user.class.stub!(:gravatar_source).and_return(:email)
43
+ @user.class.should have_gravatar
44
+ end
45
+
46
+ it "is false when .gravatar_soruce is not is nil" do
47
+ @user.class.stub!(:gravatar_source).and_return(nil)
48
+ @user.class.should_not have_gravatar
49
+ end
50
+
51
+ end
52
+
53
+ describe "#gravatar_id" do
54
+
55
+ before(:each) do
56
+ @user.stub!(:email).and_return('joe@example.com')
57
+ @user.stub!(:name).and_return('Joe Bloggs')
58
+ @user.class.stub!(:gravatar_source).and_return(:email)
59
+ end
60
+
61
+ it "is not nil when .gravatar_source is not nil" do
62
+ @user.gravatar_id.should_not be_nil
63
+ end
64
+
65
+ it "is nil when .gravatar_source is nil" do
66
+ @user.class.stub!(:gravatar_source).and_return(nil)
67
+ @user.gravatar_id.should be_nil
68
+ end
69
+
70
+ it "changes when .gravatar_source is changed" do
71
+ lambda {
72
+ @user.class.stub!(:gravatar_source).and_return(:name)
73
+ }.should change(@user, :gravatar_id)
74
+ end
75
+
76
+ it "is a MD5 hash" do
77
+ @user.gravatar_id.should == 'f5b8fb60c6116331da07c65b96a8a1d1'
78
+ end
79
+
80
+ it "downcases any imput" do
81
+ @user.stub!(:email).and_return('JOE@EXAMPLE.COM')
82
+ @user.gravatar_id.should == 'f5b8fb60c6116331da07c65b96a8a1d1'
83
+ end
84
+
85
+ end
86
+
87
+ describe "#gravatar_url" do
88
+
89
+ before(:each) do
90
+ @user.stub!(:email).and_return('joe@example.com')
91
+ @user.stub!(:name).and_return('Joe Bloggs')
92
+ @user.class.stub!(:gravatar_source).and_return(:email)
93
+ end
94
+
95
+ it "is not nil when .gravatar_source is not nil" do
96
+ @user.gravatar_url.should_not be_nil
97
+ end
98
+
99
+ it "is nil when .gravatar_source is nil" do
100
+ @user.class.stub!(:gravatar_source).and_return(nil)
101
+ @user.gravatar_url.should be_nil
102
+ end
103
+
104
+ it "always specifies a png resource type" do
105
+ @user.gravatar_url.should match(/.png/)
106
+ end
107
+
108
+ it "returns a valid gravatar URL" do
109
+ @user.gravatar_url.should == valid_gravatar_url + '?r=PG'
110
+ end
111
+
112
+ it "returns a valid SSL gravatar URL" do
113
+ @user.gravatar_url(:secure => true).should == 'https://secure.gravatar.com/avatar/f5b8fb60c6116331da07c65b96a8a1d1.png?r=PG'
114
+ end
115
+
116
+ it "returns a url without the 'www'" do
117
+ @user.gravatar_url.should_not match(/www/)
118
+ end
119
+
120
+ it "returns a valid gravatar URL even when invalid option is passed" do
121
+ @user.gravatar_url(:foo => :bar).should == valid_gravatar_url + '?r=PG'
122
+ end
123
+
124
+ it "parses a size" do
125
+ @user.gravatar_url(:size => 512).should == valid_gravatar_url + '?r=PG&s=512'
126
+ end
127
+
128
+ it "parses a rating" do
129
+ @user.gravatar_url(:rating => 'MA').should == valid_gravatar_url + '?r=MA'
130
+ end
131
+
132
+ it "returns a valid gravatar URL when the rating option is unescaped" do
133
+ @user.gravatar_url(:rating => 'Unescaped String').should == valid_gravatar_url + '?r=Unescaped+String'
134
+ end
135
+
136
+ it "parses a default image type" do
137
+ @user.gravatar_url(:default => :identicon).should == valid_gravatar_url + '?d=identicon&r=PG'
138
+ end
139
+
140
+ it "parses a default image URL" do
141
+ @user.gravatar_url(:default => 'http://example.com/images/example.jpg').should == valid_gravatar_url + '?d=http%3A%2F%2Fexample.com%2Fimages%2Fexample.jpg&r=PG'
142
+ end
143
+
144
+ it "parses multiple options" do
145
+ @user.gravatar_url(:size => 20, :rating => 'R18', :default => :monsterid).should == valid_gravatar_url + '?d=monsterid&r=R18&s=20'
146
+ end
147
+
148
+ it "defaults to a 'PG' rating" do
149
+ @user.gravatar_url(:rating => 'PG').should == @user.gravatar_url
150
+ end
151
+
152
+ def valid_gravatar_url # :nodoc:
153
+ 'http://gravatar.com/avatar/f5b8fb60c6116331da07c65b96a8a1d1.png'
154
+ end
155
+
156
+ end
157
+
158
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,4 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'gravtastic'
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gravtastic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.5.2
5
+ platform: ruby
6
+ authors:
7
+ - Chris Lloyd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-15 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Easily add Gravatars to your Ruby objects.
17
+ email: christopher.lloyd@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ - LICENSE
25
+ files:
26
+ - LICENSE
27
+ - README.textile
28
+ - Rakefile
29
+ - lib/gravtastic.rb
30
+ - spec/gravtastic_spec.rb
31
+ - spec/spec.opts
32
+ - spec/spec_helper.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/chrislloyd/gravtastic
35
+ post_install_message:
36
+ rdoc_options: []
37
+
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: gravtastic
55
+ rubygems_version: 1.1.1
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: Easily add Gravatars to your Ruby objects.
59
+ test_files: []
60
+