gravtastic 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ gem 'rspec'
2
+ gem 'rr'
data/README.md CHANGED
@@ -1,49 +1,70 @@
1
- # Gravtastic
1
+ _____ _ _ _
2
+ / ____| | | | | (_)
3
+ | | __ _ __ __ ___ _| |_ __ _ ___| |_ _ ___
4
+ | | |_ | '__/ _` \ \ / / __/ _` / __| __| |/ __|
5
+ | |__| | | | (_| |\ V /| || (_| \__ \ |_| | (__
6
+ \_____|_| \__,_| \_/ \__\__,_|___/\__|_|\___|
2
7
 
3
- <small>The super fantastic way of getting Gravatars. By [Chris](http://chrislloyd.com.au).</small>
4
8
 
5
- In less than 5 minutes you can add Gravatars to your Ruby project. It works in Rails, Merb _and_ plain ol' Ruby.
9
+ <center><small>The super fantastic way of getting Gravatars. By [The Poacher](http://thelincolnshirepoacher.com).</small></center>
10
+
11
+ In less than a minute you can add Gravatars to your Ruby project. It works in Rails, Merb & Sinatra.
12
+
13
+ The best way to learn more about Gravtastic is to [look through the annotated source](http://chrislloyd.github.com/gravtastic). It's one file, about 80 LOC and really pretty simple. If that isn't for you, then follow the instructions below!
6
14
 
7
- The best way to learn more about Gravtastic is to [look at the source](http://github.com/chrislloyd/gravtastic/blob/master/lib/gravtastic.rb). It's one file, about 80 LOC and really pretty simple. If that isn't for you, then follow the instructions below!
8
15
 
9
16
  ## Install
10
17
 
11
18
  sudo gem install gravtastic
12
19
 
20
+
13
21
  ## Usage
14
22
 
15
- Add this to your `Gemfile`:
23
+ 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.
24
+
25
+ First off, add this to your `Gemfile`:
16
26
 
17
27
  gem 'gravtastic'
18
28
 
19
- Next, say that you want a Gravatar for your model:
29
+ Next, in your model:
20
30
 
21
31
  class User < ActiveRecord::Base
22
- is_gravtastic!
32
+ include Gravtastic
33
+ gravtastic
23
34
  end
24
35
 
25
- And you are done! In your views you can now use the `#gravatar_url` method:
36
+ <small>_Note: You can use either `is_gravtastic!`, `is_gravtastic` or `has_gravatar`, they all do the same thing._</small>
37
+
38
+ And you are done! In your views you can now use the `#gravatar_url` method on instances of `User`:
26
39
 
27
40
  <%= image_tag @user.gravatar_url %>
28
41
 
29
- If you want to change the image, you can do this:
42
+ Gravatar gives you some options. You can use them like this:
30
43
 
31
44
  <%= image_tag @user.gravatar_url(:rating => 'R', :secure => true) %>
32
45
 
33
- 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:
46
+ 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:
47
+
48
+ gravtastic :secure => true,
49
+ :filetype => :gif,
50
+ :size => 120
51
+
52
+ Now all your Gravatars will come from a secure connection, be a GIF and be 120x120px.
34
53
 
35
- is_gravtastic :author_email, :secure => true,
36
- :filetype => :gif,
37
- :size => 120
54
+ Gravatar needs an email address to find the person's avatar. By default, Gravtastic calls the `#email` method to find this. You can customise this.
38
55
 
39
- 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).
56
+ gravtastic :author_email
40
57
 
41
- _Note: You can use either `is_gravtastic!` or `is_gravtastic`, they both do the same thing._
58
+ ### Defaults
59
+
60
+ 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.
61
+
62
+ 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.
42
63
 
43
64
  ### Complete List of Options
44
65
 
45
- <table>
46
- <tr>
66
+ <table width="100%">
67
+ <thead>
47
68
  <th>Option</th>
48
69
  <th>Description</th>
49
70
  <th>Default</th>
@@ -65,7 +86,7 @@ _Note: You can use either `is_gravtastic!` or `is_gravtastic`, they both do the
65
86
  <td><b>default</b></td>
66
87
  <td>The default avatar image</td>
67
88
  <td><i>none</i></td>
68
- <td>Any URL, or "identicon", "monsterid", "wavatar"</td>
89
+ <td>"identicon", "monsterid", "wavatar" or an absolute URL.</td>
69
90
  </tr>
70
91
  <tr>
71
92
  <td><b>rating</b></td>
@@ -81,67 +102,43 @@ _Note: You can use either `is_gravtastic!` or `is_gravtastic`, they both do the
81
102
  </tr>
82
103
  </table>
83
104
 
84
- ## Other ORM
85
105
 
86
- ### Plain Ruby
106
+ ### Other ORMs
87
107
 
88
- So you just have a regular ol' Ruby app? No Rails and ActiveRecord?
108
+ 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:
89
109
 
90
110
  require 'gravtastic'
91
- class BoringUser
111
+ class MyClass
92
112
  include Gravtastic
93
- is_gravtastic!
94
- end
95
-
96
- And wallah! That works exactly the same as in Rails! Now all instances of the BoringUser class will have `#gravatar_url` methods.
97
-
98
- _Note: the `#gravatar_url` methods don't get included until you specify the class `is_gravtastic!`_
99
-
100
- ### DataMapper
101
-
102
- require 'dm-core'
103
- require 'gravtastic'
104
- class User
105
- include DataMapper::Resource
106
- is :gravtastic
113
+ is_gravtastic
107
114
  end
108
115
 
109
- ### Sequel
116
+ For instance, with the excellent [MongoMapper](http://github.com/jnunemaker/mongomapper) you can use do this:
110
117
 
111
- require 'sequel'
112
- require 'gravtastic'
113
- class User < Sequel::Model
114
- plugin Gravtastic
115
- end
118
+ class Person
119
+ include MongoMapper::Document
120
+ include Gravtastic
116
121
 
117
- ### MongoMapper
122
+ is_gravtastic
118
123
 
119
- require 'mongo_mapper'
120
- require 'gravtastic'
121
- class User
122
- include MongoMapper::Document
123
- plugin Gravtastic
124
+ key :email
124
125
  end
125
126
 
126
- ### Mongoid
127
+ And wallah! It's exactly the same as with ActiveRecord! Now all instances of the `Person` class will have `#gravatar_url` methods.
127
128
 
128
- require 'mongoid'
129
- require 'gravtastic'
130
- class User
131
- include Mongoid::Document
132
- is_gravtastic
133
- end
129
+ _Note: the `#gravatar_url` methods don't get included until you specify the class `is_gravtastic!`_
134
130
 
135
131
 
136
132
  ## Making Changes Yourself
137
133
 
138
- Fork the project, submit a pull request and I'll get to it straight away. Or you can just view the source like:
134
+ 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.
139
135
 
140
136
  git clone git://github.com/chrislloyd/gravtastic.git
141
137
 
138
+
142
139
  ## Thanks
143
140
 
144
- * [Xavier Shay](http://rhnh.net) and others for [Enki](http://enkiblog.com) (the reason this was originally written)
141
+ * [Xavier Shay](http://github.com/xaviershay) and others for [Enki](http://enkiblog.com) (the reason this was originally written)
145
142
  * [Matthew Moore](http://github.com/moorage)
146
143
  * [Galen O'Hanlon](http://github.com/gohanlon)
147
144
  * [Jason Cheow](http://jasoncheow.com)
data/Rakefile CHANGED
@@ -1,20 +1,31 @@
1
- begin
2
- require 'jeweler'
3
- Jeweler::Tasks.new do |gs|
4
- gs.name = 'gravtastic'
5
- gs.homepage = 'http://github.com/chrislloyd/gravtastic'
6
- gs.description = 'Add Gravatars to your Rubies/Rails!'
7
- gs.summary = 'Ruby/Gravatar'
8
- gs.email = 'christopher.lloyd@gmail.com'
9
- gs.author = 'Chris Lloyd'
10
- gs.rubyforge_project = 'gravtastic'
11
- end
12
- Jeweler::GemcutterTasks.new
13
- rescue LoadError
1
+ require 'rake/gempackagetask'
2
+ require 'rake/clean'
3
+ require 'rspec/core/rake_task'
4
+
5
+ def name
6
+ @name ||= File.basename(Dir['*.gemspec'].first, '.gemspec')
7
+ end
8
+
9
+ def gemspec_file
10
+ "#{name}.gemspec"
11
+ end
12
+
13
+ load(gemspec_file)
14
+
15
+ Rake::GemPackageTask.new(@spec) do |t|
16
+ t.need_tar = true
17
+ t.need_zip = true
14
18
  end
19
+ CLEAN.add 'pkg'
15
20
 
16
21
  require 'spec/rake/spectask'
17
- Spec::Rake::SpecTask.new('spec') do |t|
18
- t.spec_opts = ['--color']
19
- t.spec_files = FileList['spec/*.rb']
22
+ RSpec::Core::RakeTask.new do |t|
23
+ t.rspec_opts = ['--color', '--require ./spec/helper']
24
+ # t.files = FileList['spec/*.rb']
25
+ end
26
+
27
+ desc 'Build documentation'
28
+ task :doc do
29
+ sh 'docco lib/gravtastic.rb'
20
30
  end
31
+ CLEAN.add 'docs'
@@ -0,0 +1,10 @@
1
+ module Gravtastic
2
+
3
+ VERSION = '3.1.0'
4
+
5
+ # Returns the version of Gravtastic
6
+ def self.version
7
+ VERSION
8
+ end
9
+
10
+ end
data/lib/gravtastic.rb CHANGED
@@ -1,20 +1,21 @@
1
+ # Gravtastic is a small module which quickly generates a Gravatar URL. Just
2
+ # include in your class!
3
+
4
+ # Gravatar uses an MD5 hash of the users email to locate their avatar.
1
5
  require 'digest/md5'
2
- require 'cgi'
3
- require 'uri'
6
+ require 'gravtastic/version'
4
7
 
5
8
  module Gravtastic
6
-
7
- def self.version
8
- File.read(__FILE__.sub('lib/gravtastic.rb','VERSION')).strip
9
- end
10
9
 
10
+ # When you `include Gravtastic`, Ruby automatically calls this method
11
+ # with the class you called it in. It allows us extend the class with the
12
+ # `#gravtastic` method.
11
13
  def self.included(model)
12
- model.extend ManualConfigure
13
- end
14
-
15
- def self.apply(model, *args, &blk)
14
+ model.extend StageOne
16
15
  end
17
16
 
17
+ # Sets the model's default attributes. It is called when you use
18
+ # `#gravtastic` in your model.
18
19
  def self.configure(model, *args, &blk)
19
20
  options = args.last.is_a?(Hash) ? args.pop : {}
20
21
 
@@ -24,24 +25,36 @@ module Gravtastic
24
25
  :filetype => :png
25
26
  }.merge(options)
26
27
 
28
+ # The method where Gravtastic get the users' email from defaults to `#email`.
27
29
  model.gravatar_source = args.first || :email
28
30
  end
29
31
 
30
- module ManualConfigure
31
- def is_gravtastic(*args, &blk)
32
+ # We include Gravtastic in multiple stages. This is mainly so that if you
33
+ # include Gravastic in a superclass (something like `ActiveRecord::Base`)
34
+ # then it only adds the relevant methods to the classes which _actually_ use
35
+ # it.
36
+ module StageOne
37
+ def gravtastic(*args, &blk)
32
38
  extend ClassMethods
33
39
  include InstanceMethods
34
40
  Gravtastic.configure(self, *args, &blk)
35
41
  self
36
42
  end
37
-
38
- alias_method :has_gravatar, :is_gravtastic
39
- alias_method :is_gravtastic!, :is_gravtastic
43
+
44
+ # All these aliases deal with pervious bad design decisions. Let that be a
45
+ # lesson, name things simply, try not to follow fads and try not to break
46
+ # backwards compatibility.
47
+ alias_method :gravtastic!, :gravtastic
48
+ alias_method :has_gravatar, :gravtastic
49
+ alias_method :is_gravtastic, :gravtastic
50
+ alias_method :is_gravtastic!, :gravtastic
40
51
  end
41
52
 
42
53
  module ClassMethods
43
54
  attr_accessor :gravatar_source, :gravatar_defaults
44
-
55
+
56
+ # Gravtastic abbreviates certain params so that it produces the smallest
57
+ # possible URL. Every byte counts.
45
58
  def gravatar_abbreviations
46
59
  { :size => 's',
47
60
  :default => 'd',
@@ -52,44 +65,41 @@ module Gravtastic
52
65
 
53
66
  module InstanceMethods
54
67
 
68
+ # The raw MD5 hash of the users' email. Gravatar is particularly tricky as
69
+ # it downcases all emails. This is really the guts of the module,
70
+ # everything else is just convenience.
55
71
  def gravatar_id
56
72
  Digest::MD5.hexdigest(send(self.class.gravatar_source).to_s.downcase)
57
73
  end
58
-
74
+
75
+ # Constructs the full Gravatar url.
59
76
  def gravatar_url(options={})
60
77
  options = self.class.gravatar_defaults.merge(options)
61
- path = gravatar_hostname(options.delete(:secure)) +
78
+ gravatar_hostname(options.delete(:secure)) +
62
79
  gravatar_filename(options.delete(:filetype)) +
63
80
  url_params_from_hash(options)
64
-
65
- # This would be alot cleaner with .try(:html_safe)
66
- path.respond_to?(:html_safe) ? path.html_safe : path
67
81
  end
68
82
 
69
83
  private
70
-
84
+
85
+ # Creates a params hash like "?foo=bar" from a hash like {'foo' => 'bar'}.
86
+ # The values are sorted so it produces deterministic output (and can
87
+ # therefore be tested easily).
71
88
  def url_params_from_hash(hash)
72
89
  '?' + hash.map do |key, val|
73
- [self.class.gravatar_abbreviations[key.to_sym] || key.to_s, CGI::escape(val.to_s) ].join('=')
74
- end.sort.join('&amp;')
90
+ [self.class.gravatar_abbreviations[key.to_sym] || key.to_s, val.to_s ].join('=')
91
+ end.sort.join('&')
75
92
  end
76
93
 
94
+ # Returns either Gravatar's secure hostname or not.
77
95
  def gravatar_hostname(secure)
78
96
  'http' + (secure ? 's://secure.' : '://') + 'gravatar.com/avatar/'
79
97
  end
80
-
98
+
99
+ # Munges the ID and the filetype into one. Like "abc123.png"
81
100
  def gravatar_filename(filetype)
82
101
  "#{gravatar_id}.#{filetype}"
83
102
  end
84
103
  end
85
104
 
86
105
  end
87
-
88
- # All these ORMs suck balls. See Sequel or MongoMapper for examples of good
89
- # plugin systems.
90
-
91
- ActiveRecord::Base.send(:include, Gravtastic) if defined?(ActiveRecord) # :nodoc:
92
- DataMapper::Model.append_extensions(Gravtastic::ManualConfigure) if defined?(DataMapper) # :nodoc:
93
- Mongoid::Document.included do
94
- include Gravtastic
95
- end if defined?(Mongoid) # :nodoc:
@@ -31,9 +31,9 @@ describe Gravtastic do
31
31
 
32
32
  it "downcases email" do
33
33
  a = @g.new
34
- stub(a).email{ 'USER@EXAMPLE.COM' }
34
+ stub(a).email do 'USER@EXAMPLE.COM' end
35
35
  b = @g.new
36
- stub(b).email{ 'user@example.com' }
36
+ stub(b).email do 'user@example.com' end
37
37
  a.gravatar_id.should == b.gravatar_id
38
38
  end
39
39
 
@@ -63,12 +63,12 @@ describe Gravtastic do
63
63
  end
64
64
 
65
65
  it "abides to some new fancy feature" do
66
- @user.gravatar_url(:extreme => true).should == 'http://gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.png?extreme=true&amp;r=PG'
66
+ @user.gravatar_url(:extreme => true).should == 'http://gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.png?extreme=true&r=PG'
67
67
  end
68
68
 
69
69
  it "makes a URL from the defaults" do
70
70
  stub(@user.class).gravatar_defaults{ {:size => 20, :rating => 'R18', :secure => true, :filetype => :png} }
71
- @user.gravatar_url.should == 'https://secure.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.png?r=R18&amp;s=20'
71
+ @user.gravatar_url.should == 'https://secure.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af.png?r=R18&s=20'
72
72
  end
73
73
 
74
74
  end
data/spec/helper.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  $:.push File.join(File.dirname(__FILE__), '..', 'lib')
2
2
 
3
- Spec::Runner.configure do |config|
3
+ require 'gravtastic'
4
+
5
+ RSpec.configure do |config|
4
6
  config.mock_with :rr
5
7
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 3
7
+ - 1
7
8
  - 0
8
- - 0
9
- version: 3.0.0
9
+ version: 3.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Chris Lloyd
@@ -14,34 +14,33 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-11 00:00:00 +02:00
17
+ date: 2010-10-18 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: Add Gravatars to your Rubies/Rails!
21
+ description: A Ruby wrapper for Gravatar URLs
22
22
  email: christopher.lloyd@gmail.com
23
23
  executables: []
24
24
 
25
25
  extensions: []
26
26
 
27
- extra_rdoc_files:
28
- - README.md
27
+ extra_rdoc_files: []
28
+
29
29
  files:
30
- - .gitignore
31
30
  - README.md
32
31
  - Rakefile
33
- - VERSION
32
+ - Gemfile
33
+ - lib/gravtastic/version.rb
34
34
  - lib/gravtastic.rb
35
- - spec/gravtastic_integration_spec.rb
36
35
  - spec/gravtastic_spec.rb
37
36
  - spec/helper.rb
38
- has_rdoc: true
37
+ has_rdoc: false
39
38
  homepage: http://github.com/chrislloyd/gravtastic
40
39
  licenses: []
41
40
 
42
41
  post_install_message:
43
- rdoc_options:
44
- - --charset=UTF-8
42
+ rdoc_options: []
43
+
45
44
  require_paths:
46
45
  - lib
47
46
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -66,8 +65,6 @@ rubyforge_project: gravtastic
66
65
  rubygems_version: 1.3.7
67
66
  signing_key:
68
67
  specification_version: 3
69
- summary: Ruby/Gravatar
70
- test_files:
71
- - spec/gravtastic_integration_spec.rb
72
- - spec/gravtastic_spec.rb
73
- - spec/helper.rb
68
+ summary: A Ruby wrapper for Gravatar URLs
69
+ test_files: []
70
+
data/.gitignore DELETED
@@ -1,6 +0,0 @@
1
- coverage
2
- html
3
- pkg
4
- doc
5
- .ditz-config
6
- dist
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 3.0.0
@@ -1,52 +0,0 @@
1
- require 'active_record'
2
- require 'dm-core'
3
- require 'sequel'
4
- require 'mongo_mapper'
5
- require 'mongoid'
6
- require 'gravtastic'
7
-
8
- describe ActiveRecord do
9
- it "includes Gravtastic" do
10
- ActiveRecord::Base.should respond_to(:is_gravtastic)
11
-
12
- # Class.new(ActiveRecord::Base) do
13
- # is_gravtastic
14
- # end.new.should respond_to(:gravatar_url)
15
- end
16
- end
17
-
18
- describe DataMapper do
19
- it "includes Gravtastic" do
20
- Class.new do
21
- include DataMapper::Resource
22
- is :gravtastic
23
- end.new.should respond_to(:gravatar_url)
24
- end
25
- end
26
-
27
- describe Sequel do
28
- it "includes Gravtastic" do
29
- Sequel::Model.plugin Gravtastic
30
- # Class.new(Sequel::Model) do
31
- # plugin Gravtastic
32
- # end.new.should respond_to(:gravatar_url)
33
- end
34
- end
35
-
36
- describe MongoMapper do
37
- it "includes Gravtastic" do
38
- Class.new do
39
- include MongoMapper::Document
40
- plugin Gravtastic
41
- end.new.should respond_to(:gravatar_url)
42
- end
43
- end
44
-
45
- describe Mongoid do
46
- it "includes Gravtastic" do
47
- Class.new do
48
- include Mongoid::Document
49
- is_gravtastic
50
- end.new.should respond_to(:gravatar_url)
51
- end
52
- end