gravatarify 1.2.1 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +115 -73
- data/Rakefile +2 -2
- data/VERSION.yml +3 -3
- data/lib/gravatarify.rb +1 -6
- data/lib/gravatarify/base.rb +40 -48
- data/lib/gravatarify/helper.rb +11 -25
- data/lib/gravatarify/utils.rb +44 -0
- data/test/test_helper.rb +1 -9
- data/test/unit/gravatarify_base_test.rb +54 -46
- data/test/unit/gravatarify_helper_test.rb +12 -30
- data/test/unit/gravatarify_rack_vs_cgi_test.rb +7 -7
- data/test/unit/gravatarify_styles_test.rb +92 -0
- data/test/unit/gravatarify_subdomain_test.rb +13 -21
- metadata +7 -9
- data/lib/gravatarify/object_support.rb +0 -59
- data/test/unit/gravatarify_ar_dm_test.rb +0 -60
- data/test/unit/gravatarify_object_support_test.rb +0 -107
data/README.md
CHANGED
@@ -2,15 +2,22 @@ Gravatarify
|
|
2
2
|
===========
|
3
3
|
|
4
4
|
Hassle-free construction of those pesky gravatar.com urls, with out-of-the-box support for
|
5
|
-
Rails,
|
5
|
+
Rails, Haml and _your favorite framework_. It's not that there aren't any alternatives [out](http://github.com/mdeering/gravitar_image_tag),
|
6
6
|
[there](http://github.com/chrislloyd/gravtastic), but none seem to support stuff like `Proc`s
|
7
7
|
for the default picture url, or the multiple host names supported by gravatar.com (great when
|
8
8
|
displaying lots of avatars).
|
9
9
|
|
10
10
|
- **Source**: <http://github.com/lwe/gravatarify>
|
11
11
|
- **Docs**: <http://rdoc.info/projects/lwe/gravatarify>
|
12
|
+
- **List**: <http://groups.google.com/group/gravatarify-plugin>
|
12
13
|
- **Gem**: <http://gemcutter.org/gems/gravatarify>
|
13
14
|
|
15
|
+
**UPGRADE NOTES:** Version 2.x is a clean-up release which breaks backwards compatibility
|
16
|
+
with 1.x releases (in some cases!). HTML attributes must be passed like:
|
17
|
+
`gravatar_tag(@user, :size => 30, :html => { :class => "gravatar" })`, i.e. in a `:html` hash.
|
18
|
+
Furthermore the `gravatarify` method for ActiveRecord and DataMapper no longer exists,
|
19
|
+
see "Upgrading from 1.x" for more.
|
20
|
+
|
14
21
|
Ready, Set, Go!
|
15
22
|
---------------
|
16
23
|
|
@@ -38,10 +45,9 @@ and then `require 'gravatarify'`'d somehow.
|
|
38
45
|
**More!?** Allright, that was just the quickstart, to get up and running with ease. However, this library provides
|
39
46
|
quite a bit more, like:
|
40
47
|
|
41
|
-
*
|
42
|
-
*
|
43
|
-
|
44
|
-
* ...and finally, a base module which provides the gravatar url generation, ready to be integrated into
|
48
|
+
* View helpers, namely `gravatar_url` and `gravatar_tag`, see "Using the view helpers".
|
49
|
+
* Styles are like reusable definitions of options, nice to DRY-up your code, see "Using styles".
|
50
|
+
* A base module which provides the gravatar url generation, ready to be integrated into
|
45
51
|
custom helpers, plain ruby code or whatever, see "Back to the roots".
|
46
52
|
|
47
53
|
Using the view helpers
|
@@ -67,73 +73,55 @@ or a string containing the e-mail address:
|
|
67
73
|
This builds a neat `<img/>`-tag. To display an "X" rated avatar which is 25x25 pixel in size
|
68
74
|
and the `<img/>` tag should have a class attribute, do:
|
69
75
|
|
70
|
-
<%= gravatar_tag @user, :size => 25, :rating => :x, :class => "gravatar" %>
|
76
|
+
<%= gravatar_tag @user, :size => 25, :rating => :x, :html => { :class => "gravatar" } %>
|
71
77
|
|
72
|
-
If
|
73
|
-
|
78
|
+
If any additional HTML attributes are needed on the tag, or in the `gravatar_attrs`, just
|
79
|
+
pass them in the `:html` option as hash. If more control is needed, or just the plain URL is
|
80
|
+
required, resort to `gravatar_url`, which returns a string with the (unescaped) url:
|
74
81
|
|
75
82
|
<img src="<%= h(gravatar_url(@user.author_email, :size => 16)) %>" alt="Gravatar"/>
|
76
|
-
|
77
|
-
Using
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
Defaults can even be passed to the `gravatarify` call, so no need to repeat them on every `gravatar_url` call.
|
96
|
-
|
97
|
-
gravatarify :employee_mail, :size => 16, :rating => :r
|
98
|
-
|
99
|
-
All gravatars will now come from the `employee_mail` field, not the default `email` or `mail` field and be in 16x16px in size
|
100
|
-
and have a rating of 'r'. Of course these can be overriden in calls to `gravatar_url` like before. Pretty cool is also the
|
101
|
-
fact that an object can be passed directly to `gravatar_tag` if it responds to `gravatar_url`, like:
|
102
|
-
|
103
|
-
# model:
|
104
|
-
class User < ActiveRecord::Base
|
105
|
-
gravatarify :size => 16, :secure => true
|
106
|
-
end
|
107
|
-
|
108
|
-
# view:
|
109
|
-
<%= gravatar_tag @user %> # -> <img ... width="16" src="https://secure.gravatar..." height="16" />
|
83
|
+
|
84
|
+
Using styles
|
85
|
+
------------
|
86
|
+
|
87
|
+
With styles it's possible to easily change e.g. the size of all gravatars based on a name,
|
88
|
+
these are reusable presets of options:
|
89
|
+
|
90
|
+
# in config/initializers/gravatarify.rb or similar:
|
91
|
+
Gravatarify.styles[:mini] = { :size => 16, :html => { :class => 'gravatar gravatar-mini' } }
|
92
|
+
Gravatarify.styles[:default] = { :size => 45, :html => { :class => 'gravatar' } }
|
93
|
+
|
94
|
+
# then in/some/view.html.erb:
|
95
|
+
<%= gravatar_tag @user, :mini %> # => <img alt="" class="gravatar gravatar-mini" height="16" src.... />
|
96
|
+
|
97
|
+
# or in/another/view.html.haml:
|
98
|
+
%img{gravatar_attrs(@user, :default)/ # => <img alt="" class="gravatar" height="45" ... />
|
99
|
+
|
100
|
+
Need to change to size of all `:mini` gravatars? Easy, just change the definition in `Gravatarify.styles`.
|
101
|
+
Of course settings via `Gravatarify.options` are "mixed-in" as well, so:
|
110
102
|
|
111
|
-
|
112
|
-
|
103
|
+
Gravatarify.options[:default] = Proc.new { |*params| "http://example.com/avatar-#{params.first[:size] || 80}.jpg" }
|
104
|
+
Gravatarify.styles[:mini] => { :size => 16 }
|
105
|
+
|
106
|
+
<%= gravatar_tag @user, :mini %> # => <img alt="" height="16" src=".....?d=http://example.com/avatar-16.jpg" width="16" />
|
113
107
|
|
114
|
-
|
108
|
+
All methods accept a style, i.e. a style parameter can be passed in to `gravatar_attrs`, `gravatar_tag` and
|
109
|
+
of course to `gravatar_url`. Any option can be passed in after a style, to customize the gravatar
|
110
|
+
if the styles needs slight alteration:
|
115
111
|
|
116
|
-
|
117
|
-
class:
|
112
|
+
gravatar_url(@user, :mini, :filetype => false, :rating => :x) # => "http://........123ab12?s=16&r=x"
|
118
113
|
|
119
|
-
require 'gravatarify'
|
120
|
-
class PoroUser
|
121
|
-
include Gravatarify::ObjectSupport
|
122
|
-
gravatarify
|
123
|
-
end
|
124
|
-
|
125
|
-
Tadaaa! Works exactly like the model helpers, so it's now possible to call `gravatar_url` on instances
|
126
|
-
of `PoroUser`.
|
127
114
|
|
128
|
-
|
115
|
+
Back to the roots?
|
116
|
+
------------------
|
129
117
|
|
130
|
-
No need for sophisticated stuff like view helpers
|
131
|
-
Then feel free to use `Gravatarify::Base#
|
118
|
+
No need for sophisticated stuff like view helpers, want to go back to the roots?
|
119
|
+
Then feel free to use `Gravatarify::Base#gravatar_url` directly.
|
132
120
|
|
133
121
|
When the ability to display image tags is required in different view frameworks (like liquid!?),
|
134
122
|
then just ensure that `Gravatarify::Helper` is included in the framework in question.
|
135
|
-
See {Gravatarify::Base#
|
136
|
-
|
123
|
+
See {Gravatarify::Base#gravatar_url} and of course {Gravatarify::Helper} for more informations
|
124
|
+
and usage examples.
|
137
125
|
|
138
126
|
Need more control?
|
139
127
|
==================
|
@@ -186,6 +174,13 @@ Need more control?
|
|
186
174
|
then a URL without an extension will be built (and gravatar.com then returns a JPG image).</td>
|
187
175
|
<td><tt>:jpg</tt></td>
|
188
176
|
</tr>
|
177
|
+
<tr>
|
178
|
+
<td><tt>:html</tt></td>
|
179
|
+
<td>Hash</td>
|
180
|
+
<td>Ignored in URL generation, only used in <tt>gravatar_attrs</tt> and <tt>gravatar_tag</tt> to pass in additional
|
181
|
+
HTML attributes (like <tt>class</tt>, <tt>id</tt> etc.).</td>
|
182
|
+
<td>-</td>
|
183
|
+
</tr>
|
189
184
|
</table>
|
190
185
|
|
191
186
|
To set the options globally, access the `Gravatarify.options` hash and set any options which should apply to all
|
@@ -198,13 +193,12 @@ gravatar urls there. Of course all settings can be overridden locally:
|
|
198
193
|
gravatar_url(@user.email) # => http://0.gravatar.com/avatar/..f93ff1e?s=16
|
199
194
|
gravatar_url(@user.email, :filetype => :png) # => http://0.gravatar.com/avatar/..f93ff1e.png?s=16
|
200
195
|
|
201
|
-
|
202
|
-
to always add a title attribute:
|
196
|
+
To define global HTML attributes go ahead and do something like:
|
203
197
|
|
204
198
|
# add title attribute
|
205
|
-
Gravatarify
|
199
|
+
Gravatarify.options[:html] = { :title => "Gravatar" }
|
206
200
|
|
207
|
-
|
201
|
+
## Not yet enough?
|
208
202
|
|
209
203
|
The `:default` option can be passed in a `Proc`, so this is certainly useful to for example
|
210
204
|
to generate an image url, based on the request size:
|
@@ -231,6 +225,53 @@ Into the block is passed the options hash and as second parameter the object its
|
|
231
225
|
Not only the `:default` option accepts a Proc, but also `:secure`, can be useful to handle cases where
|
232
226
|
it should evaluate against `request.ssl?` for example.
|
233
227
|
|
228
|
+
## Upgrading from 1.x
|
229
|
+
|
230
|
+
All HTML options must be passed in the `:html` attribute. This allows for predictable results for
|
231
|
+
all methods and no magic involved! So instead of doing:
|
232
|
+
|
233
|
+
gravatar_tag(@user, :size => 30, :class => "gravatar", :title => "Gravatar")
|
234
|
+
# Note: in Version 2.0 this would build an image src like http://..gravatar.com/...../?class=gravatar&s=30&title=Gravatar
|
235
|
+
|
236
|
+
do:
|
237
|
+
|
238
|
+
gravatar_tag(@user, :size => 30, :html => { :class => "Gravatar", :title => "Gravatar" })
|
239
|
+
|
240
|
+
An important thing to know is that the `:html` is deep merged, with defaults, so stuff like:
|
241
|
+
|
242
|
+
Gravatarify.options[:html] = { :title => "Gravatar" }
|
243
|
+
gravatar_tag(@user, :html => { :class => "gravatar" }) # => <img alt="" class="gravatar" ... title="Gravatar" .../>
|
244
|
+
|
245
|
+
Furthermore the option `:html` is ignored when building the url parameters.
|
246
|
+
|
247
|
+
If the `gravatarify` method was not used, there's no need to change anything at all :) Though if
|
248
|
+
it's used, then...
|
249
|
+
|
250
|
+
1. Remove all occurences of `gravatarify` in the models
|
251
|
+
2. Change calls from `<%= image_tag @user.gravatar_url %>` to
|
252
|
+
saner `<%= gravatar_tag @user %>` calls or of course if just the url
|
253
|
+
is required to `gravatar_url(@user)`.
|
254
|
+
|
255
|
+
If the model used `gravatarify :author_email`, then changes in the views must reflect that and use it
|
256
|
+
directly: `<%= gravatar_tag @user.author_email %>`. If the model defines `author_email`, but **not**
|
257
|
+
`email` (and has no attribute named `email`), then it could be safely aliased like:
|
258
|
+
|
259
|
+
# Fields: (id, name, author_email, ...)
|
260
|
+
class Author < ActiveRecord::Base
|
261
|
+
alias_attribute :email, :author_email
|
262
|
+
end
|
263
|
+
|
264
|
+
# and in the views:
|
265
|
+
<%= gravatar_tag @author %>
|
266
|
+
|
267
|
+
In most cases the migration path should be pretty easy, just always use the helper
|
268
|
+
methods! The `gravatarify` method was introduced to provide compatibility with
|
269
|
+
`gravtastic`, yet it's not way to go in my opinion. If for some reason it's required
|
270
|
+
then feel free to fallback and use an older version (i.e. 1.2.1):
|
271
|
+
|
272
|
+
[sudo] gem install gravatarify -v 1.2.1
|
273
|
+
|
274
|
+
|
234
275
|
About the code
|
235
276
|
==============
|
236
277
|
|
@@ -239,20 +280,21 @@ of an overkill, though I like neat and tidy classes :)
|
|
239
280
|
|
240
281
|
lib/gravatarify.rb # loads the other files from lib/gravatarify
|
241
282
|
# and hooks the necessary modules into
|
242
|
-
#
|
243
|
-
# (if available)
|
283
|
+
# HAML or ActionView.
|
244
284
|
|
245
285
|
lib/gravatarify/base.rb # Provides all logic required to generate
|
246
286
|
# gravatar.com urls from an email address.
|
247
|
-
# Check out Gravatarify::Base.
|
287
|
+
# Check out Gravatarify::Base.gravatar_url,
|
248
288
|
# this is the absolute core method.
|
289
|
+
|
290
|
+
lib/gravatarify/helper.rb # Defines the view helpers: gravatar_attrs
|
291
|
+
# and gravatar_tag
|
249
292
|
|
250
|
-
lib/gravatarify/
|
251
|
-
#
|
252
|
-
#
|
293
|
+
lib/gravatarify/utils.rb # Provides commonly used methods, like helpers to
|
294
|
+
# uri and html escape strings or deep mergeing
|
295
|
+
# hashes. Though these utils are only for internal
|
296
|
+
# uses :)
|
253
297
|
|
254
|
-
lib/gravatarify/helper.rb # Defines those view helpers, mainly gravatar_attrs
|
255
|
-
# and gravatar_tag
|
256
298
|
### Contribute
|
257
299
|
|
258
300
|
1. Fork the project and hack away
|
@@ -285,4 +327,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
285
327
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
286
328
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
287
329
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
288
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
330
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -30,9 +30,9 @@ begin
|
|
30
30
|
require 'jeweler'
|
31
31
|
Jeweler::Tasks.new do |gemspec|
|
32
32
|
gemspec.name = "gravatarify"
|
33
|
-
gemspec.summary = "Awesome gravatar support for Ruby (and Rails
|
33
|
+
gemspec.summary = "Awesome gravatar support for Ruby (and Rails)."
|
34
34
|
description = <<-DESC
|
35
|
-
Awesome gravatar support for Ruby (and Rails
|
35
|
+
Awesome gravatar support for Ruby (and Rails) -
|
36
36
|
with unique options like Proc's for default images, or
|
37
37
|
support for gravatar.com's multiple host names.
|
38
38
|
DESC
|
data/VERSION.yml
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
---
|
2
|
-
:major:
|
3
|
-
:minor:
|
4
|
-
:patch:
|
2
|
+
:major: 2
|
3
|
+
:minor: 0
|
4
|
+
:patch: 3
|
data/lib/gravatarify.rb
CHANGED
@@ -3,13 +3,8 @@
|
|
3
3
|
# Base -> provides the basic gravatar_url method, can also be used for
|
4
4
|
# custom implementations, just include Gravatarify::Base.
|
5
5
|
require 'gravatarify/base'
|
6
|
-
require 'gravatarify/
|
6
|
+
require 'gravatarify/utils'
|
7
7
|
require 'gravatarify/helper'
|
8
8
|
|
9
|
-
# setup for AR und DataMapper, note: DataMapper yet untested :) but I suppose it works, because
|
10
|
-
# it works as expected on plain old ruby objects!
|
11
|
-
ActiveRecord::Base.send(:include, Gravatarify::ObjectSupport) if defined?(ActiveRecord)
|
12
|
-
DataMapper::Model.append_inclusions(Gravatarify::ObjectSupport) if defined?(DataMapper)
|
13
|
-
|
14
9
|
# and HAML support (if defined)
|
15
10
|
Haml::Helpers.send(:include, Gravatarify::Helper) if defined?(Haml)
|
data/lib/gravatarify/base.rb
CHANGED
@@ -1,16 +1,13 @@
|
|
1
1
|
require 'digest/md5'
|
2
|
-
begin; require 'rack/utils'; rescue LoadError; require 'cgi' end
|
3
2
|
|
4
|
-
module Gravatarify
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
# Hash of :ultra_long_option_name => 'abbrevated option'
|
9
|
-
GRAVATAR_ABBREV_OPTIONS = { :default => 'd', :rating => 'r', :size => 's' }
|
3
|
+
module Gravatarify
|
4
|
+
# Hash of 'ultra_long_option_name' => 'abbrevated option'
|
5
|
+
# :nodoc:
|
6
|
+
GRAVATAR_ABBREV_OPTIONS = { 'default' => 'd', 'rating' => 'r', 'size' => 's' }
|
10
7
|
|
11
8
|
class << self
|
12
9
|
# Globally define options which are then merged on every call to
|
13
|
-
# +
|
10
|
+
# +gravatar_url+, this is useful to e.g. define the default image.
|
14
11
|
#
|
15
12
|
# Setting global options should be done (for Rails apps) in an initializer:
|
16
13
|
#
|
@@ -25,6 +22,15 @@ module Gravatarify
|
|
25
22
|
#
|
26
23
|
def options; @options ||= { :filetype => :jpg } end
|
27
24
|
|
25
|
+
# Allows to define some styles, makes it simpler to call by name, instead of always giving a size etc.
|
26
|
+
#
|
27
|
+
# Gravatarify.styles[:mini] => { :size => 30, :default => "http://example.com/gravatar-mini.jpg" }
|
28
|
+
#
|
29
|
+
# # in the views, it will then use the stuff defined by styles[:mini]:
|
30
|
+
# <%= gravatar_tag @user, :mini %>
|
31
|
+
#
|
32
|
+
def styles; @styles ||= {} end
|
33
|
+
|
28
34
|
# Globally overide subdomains used to build gravatar urls, normally
|
29
35
|
# +gravatarify+ picks from either +0.gravatar.com+, +1.gravatar.com+,
|
30
36
|
# +2.gravatar.com+ or +www.gravatar.com+ when building hosts, to use a custom
|
@@ -32,27 +38,13 @@ module Gravatarify
|
|
32
38
|
#
|
33
39
|
# Gravatarify.subdomains = %w{ 0 www } # only use 0.gravatar.com and www.gravatar.com
|
34
40
|
#
|
35
|
-
# Gravatarify.subdomain = 'www' # only use www! (PS: subdomain= is an alias)
|
36
|
-
#
|
37
41
|
def subdomains=(subdomains) @subdomains = [*subdomains] end
|
38
|
-
alias_method :subdomain=, :subdomains=
|
39
|
-
|
40
|
-
# Shortcut method to reset subdomains to only build +www.gravatar.com+ urls,
|
41
|
-
# i.e. disable host balancing!
|
42
|
-
def use_www_only!; self.subdomains = %w{ www } end
|
43
|
-
|
44
|
-
# Access currently defined subdomains, defaults are +%w{ 0 1 2 www }+.
|
45
|
-
def subdomains; @subdomains ||= %w{ 0 1 2 www } end
|
46
42
|
|
47
43
|
# Get subdomain for supplied string or returns +www+ if none is
|
48
44
|
# defined.
|
49
|
-
def subdomain(str)
|
50
|
-
|
51
|
-
|
52
|
-
# fallback to <tt>CGI#escape</tt>.
|
53
|
-
def escape(str) #:nodoc:
|
54
|
-
str = str.to_s unless str.is_a?(String) # convert to string!
|
55
|
-
defined?(Rack::Utils) ? Rack::Utils.escape(str) : CGI.escape(str)
|
45
|
+
def subdomain(str) #:nodoc:
|
46
|
+
@subdomains ||= %w{ 0 1 2 www }
|
47
|
+
@subdomains[str.hash % @subdomains.size] || 'www'
|
56
48
|
end
|
57
49
|
end
|
58
50
|
|
@@ -62,7 +54,7 @@ module Gravatarify
|
|
62
54
|
# Method which builds a gravatar url based on a supplied email and options as
|
63
55
|
# defined by gravatar.com (http://en.gravatar.com/site/implement/url).
|
64
56
|
#
|
65
|
-
#
|
57
|
+
# gravatar_url('peter.gibbons@initech.com', :size => 16) # => "http://0.gravatar.com/avatar/cb7865556d41a3d800ae7dbb31d51d54.jpg?s=16"
|
66
58
|
#
|
67
59
|
# It supports multiple gravatar hosts (based on email hash), i.e. depending
|
68
60
|
# on the hash, either <tt>0.gravatar.com</tt>, <tt>1.gravatar.com</tt>, <tt>2.gravatar.com</tt> or <tt>www.gravatar.com</tt>
|
@@ -72,18 +64,18 @@ module Gravatarify
|
|
72
64
|
# is used instead to build the gravatar hash. Very useful to just pass in ActiveRecord object for instance:
|
73
65
|
#
|
74
66
|
# @user = User.find_by_email("samir@initech.com")
|
75
|
-
#
|
76
|
-
#
|
67
|
+
# gravatar_url(@user) # => "http://2.gravatar.com/avatar/58cf31c817d76605d5180ce1a550d0d0.jpg"
|
68
|
+
# gravatar_url(@user.email) # same as above!
|
77
69
|
#
|
78
70
|
# Among all options as defined by gravatar.com's specification, there also exist some special options:
|
79
71
|
#
|
80
|
-
#
|
72
|
+
# gravatar_url(@user, :secure => true) # => https://secure.gravatar.com/ava....
|
81
73
|
#
|
82
74
|
# Useful when working on SSL enabled sites. Of course often used options should be set through
|
83
75
|
# +Gravatarify.options+.
|
84
76
|
#
|
85
77
|
# @param [String, #email, #mail] email a string representing an email, or object which responds to +email+ or +mail+
|
86
|
-
# @param [Hash]
|
78
|
+
# @param [Symbol, Hash] *params customize generated gravatar.com url. First argument can also be a style.
|
87
79
|
# @option url_options [String, Proc] :default (nil) URL of an image to use as default when no gravatar exists. Gravatar.com
|
88
80
|
# also accepts special values like +identicon+, +monsterid+ or +wavater+ which just displays
|
89
81
|
# a generic icon based on the hash or <tt>404</tt> which return a HTTP Status 404.
|
@@ -96,12 +88,15 @@ module Gravatarify
|
|
96
88
|
# if an set to +false+, +nil+ or an empty string no extension is added.
|
97
89
|
# @return [String] In any case (even if supplied +email+ is +nil+) returns a fully qualified gravatar.com URL.
|
98
90
|
# The returned string is not yet HTML escaped, *but* all +url_options+ have been URI escaped.
|
99
|
-
def
|
100
|
-
url_options =
|
101
|
-
email_hash = Digest::MD5.hexdigest(
|
91
|
+
def gravatar_url(email, *params)
|
92
|
+
url_options = Utils.merge_gravatar_options(*params)
|
93
|
+
email_hash = Digest::MD5.hexdigest(Utils.smart_email(email))
|
102
94
|
extension = (ext = url_options.delete(:filetype) and ext != '') ? ".#{ext || 'jpg'}" : '' # slightly adapted from gudleik's implementation
|
103
|
-
build_gravatar_host(email_hash, url_options.delete(:secure))
|
95
|
+
build_gravatar_host(email_hash, url_options.delete(:secure)) + "/avatar/#{email_hash}#{extension}#{build_gravatar_options(email, url_options)}"
|
104
96
|
end
|
97
|
+
|
98
|
+
# For backwards compatibility.
|
99
|
+
alias_method :build_gravatar_url, :gravatar_url
|
105
100
|
|
106
101
|
private
|
107
102
|
# Builds gravatar host name from supplied e-mail hash.
|
@@ -116,22 +111,19 @@ module Gravatarify
|
|
116
111
|
secure = secure.call(self) if secure.respond_to?(:call)
|
117
112
|
secure ? "https://secure.gravatar.com" : "http://#{Gravatarify.subdomain(str_hash)}.gravatar.com"
|
118
113
|
end
|
119
|
-
|
114
|
+
|
120
115
|
# Builds a query string from all passed in options.
|
121
116
|
def build_gravatar_options(source, url_options = {})
|
122
|
-
params = []
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
117
|
+
params = url_options.inject([]) do |params, (key, value)|
|
118
|
+
key = key.to_s
|
119
|
+
if key != 'html'
|
120
|
+
key = GRAVATAR_ABBREV_OPTIONS[key] if GRAVATAR_ABBREV_OPTIONS.include?(key) # shorten key!
|
121
|
+
value = value.call(url_options, source) if key == 'd' and value.respond_to?(:call)
|
122
|
+
params << "#{Utils.escape(key)}=#{Utils.escape(value)}" if value
|
123
|
+
end
|
124
|
+
params
|
127
125
|
end
|
128
126
|
"?#{params.sort * '&'}" unless params.empty?
|
129
|
-
end
|
130
|
-
|
131
|
-
# Tries first to call +email+, then +mail+ then +to_s+ on supplied
|
132
|
-
# object.
|
133
|
-
def self.get_smart_email_from(obj) #:nodoc:
|
134
|
-
(obj.respond_to?(:email) ? obj.email : (obj.respond_to?(:mail) ? obj.mail : obj)).to_s
|
135
|
-
end
|
136
|
-
end
|
127
|
+
end
|
128
|
+
end
|
137
129
|
end
|