gravatarify 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,82 +1,101 @@
1
- # Gravatarify
2
-
3
- Removes any hassles building those pesky gravatar urls, it's not there arent any alternatives [out](http://github.com/mdeering/gravitar_image_tag),
4
- [there](http://github.com/chrislloyd/gravtastic), but none seem to support stuff like `Proc`s for the default picture url, or
5
- the multiple host names supported by gravatar.com (great when displaying lots of avatars).
1
+ Gravatarify
2
+ ===========
6
3
 
7
- And it integrates seamlessly with Rails, DataMapper and even plain old Ruby.
4
+ Hassle-free construction of those pesky gravatar.com urls, with out-of-the-box support for
5
+ Rails, DataMapper and Haml. It's not that there aren't any alternatives [out](http://github.com/mdeering/gravitar_image_tag),
6
+ [there](http://github.com/chrislloyd/gravtastic), but none seem to support stuff like `Proc`s
7
+ for the default picture url, or the multiple host names supported by gravatar.com (great when
8
+ displaying lots of avatars).
8
9
 
9
10
  - **Source**: <http://github.com/lwe/gravatarify>
10
11
  - **Docs**: <http://rdoc.info/projects/lwe/gravatarify>
11
12
  - **Gem**: <http://gemcutter.org/gems/gravatarify>
12
13
 
13
- ## Install
14
+ Ready, Set, Go!
15
+ ---------------
14
16
 
15
- Just install the gem (ensure you have gemcutter in your sources!)
17
+ **READY** Install gravatarify as a gem (requires gemcutter):
16
18
 
17
19
  [sudo] gem install gravatarify
18
-
19
- Ready to go! Using Rails? Either add as gem (in `config/environment.rb`):
20
-
21
- config.gem 'gravatarify', :source => 'http://gemcutter.org'
22
20
 
23
- or install as Rails plugin:
24
-
21
+ or as Rails plugin:
22
+
25
23
  ./script/plugin install git://github.com/lwe/gravatarify.git
26
24
 
27
- Of course it's also possible to just add the library onto the `$LOAD_PATH`
28
- and then `require 'gravatarify'` it.
25
+ **SET** When using the Rails plugin, skip this step. Anyhow, just ensure that when installed as a gem
26
+ it's bundled using `bundler` or defined in `config/environment.rb`, or just that it's on the `$LOAD_PATH`
27
+ and then `require 'gravatarify'`'d somehow.
29
28
 
30
- # Usage
29
+ **GO** Use it! When using Rails or Haml then just give it an email and it will return the gravatar url:
31
30
 
32
- This library provides...
31
+ # creates an 20x20 pixel <img/> tag in your Rails ERB views:
32
+ <%= gravatar_tag @user.email, :size => 20 %>
33
+
34
+ # or in HAML views
35
+ # (Note: how it's possible to skip the email attribute, btw - that's a feature)
36
+ %img{ gravatar_attrs(@user, :size => 20) }/
37
+
38
+ **More!?** Allright, that was just the quickstart, to get up and running with ease. However, this library provides
39
+ quite a bit more, like:
33
40
 
41
+ * ...view helpers, namely `gravatar_url` and `gravatar_tag`, see "Using the view helpers".
34
42
  * ...object/model helpers, so that an object responds to `gravatar_url`, see "Using the model helpers"
35
43
  Works also very well with plain old ruby objects.
36
- * ...Rails view helpers, namely `gravatar_url` and `gravatar_tag`, see "Using the view helpers". This is rails only though!
37
44
  * ...and finally, a base module which provides the gravatar url generation, ready to be integrated into
38
45
  custom helpers, plain ruby code or whatever, see "Back to the roots".
39
46
 
40
- ## Using the view helpers (Rails only!)
47
+ Using the view helpers
48
+ ----------------------
41
49
 
42
- Probably one of the easiest ways to add support for gravatar images is with the included view helpers:
50
+ Probably one of the easiest ways to add support for gravatar images is with the included view helpers.
51
+ When using Rails or HAML these should be automatically available, if not do something like:
43
52
 
44
- <%= gravatar_tag @user %> # assumes @user has email or mail field!
53
+ # e.g. for Sinatra
54
+ helpers Gravatarify::Helper
45
55
 
46
- This builds a neat `<img/>`-tag, if you need to pass in stuff like the size etc. just:
56
+ # or include for Haml
57
+ Haml::Helpers.send(:include, Gravatarify::Helper)
47
58
 
48
- <%= gravatar_tag @user, :size => 25, :rating => :x, :class => "gravatar" %>
59
+ # NOTE: basically just include the Gravatarify::Helper module
49
60
 
50
- This will display an "X" rated avatar which is 25x25 pixel in size and the image tag will have the class `"gravatar"`.
51
- If more control is required, or just the URL, well then go ahead and use `gravatar_url` instead:
61
+ This then provides three helper methods: `gravatar_url`, `gravatar_attrs` and `gravatar_tag`.
62
+ To just build a simple `<img/>` tag, pass in an object (if it responds to `email` or `mail`)
63
+ or a string containg the e-mail address:
52
64
 
53
- <%= image_tag gravatar_url(@user.author_email, :size => 16), :size => "16x16",
54
- :alt => @user.name, :class => "avatar avatar-16"}/
65
+ <%= gravatar_tag @user %> # => assumes @user has email or mail field!
55
66
 
56
- Using rails `image_tag` to create an `<img/>`-tag with `gravatar_url`. It's important to know that
57
- also an object can be passed to `gravatar_url`, if it responds to either `email` or `mail`. If not (like
58
- in the example above), the email address must be passed in.
67
+ This builds a neat `<img/>`-tag. To display an "X" rated avatar which is 25x25 pixel in size
68
+ and the `<img/>` tag should have a class attribute, do:
59
69
 
60
- ## Using the model helpers
70
+ <%= gravatar_tag @user, :size => 25, :rating => :x, :class => "gravatar" %>
71
+
72
+ If more control is needed, or just the plain URL is required, resort to `gravatar_url`, which
73
+ returns a string with the (unescaped) url:
74
+
75
+ <img src="<%= h(gravatar_url(@user.author_email, :size => 16)) %>" alt="Gravatar"/>
76
+
77
+ Using the model helpers
78
+ -----------------------
61
79
 
62
80
  A very simple method to add `gravatar_url` support to models is by using the `gravatarify` class method.
63
81
 
82
+ # Assuming User has a field named email or mail!
64
83
  class User < ActiveRecord::Base
65
- gravatarify
84
+ gravatarify
66
85
  end
67
-
86
+
68
87
  Thats it! Well, at least if the `User` model responds to `email` or `mail`. Then in the views all left to do is:
69
88
 
70
89
  <%= image_tag @user.gravatar_url %>
71
-
90
+
72
91
  Neat, isn't it? Of course passing options works just like with the view helpers:
73
92
 
74
93
  <%= image_tag @user.gravatar_url(:size => 16, :rating => :r) %>
75
-
94
+
76
95
  Defaults can even be passed to the `gravatarify` call, so no need to repeat them on every `gravatar_url` call.
77
96
 
78
97
  gravatarify :employee_mail, :size => 16, :rating => :r
79
-
98
+
80
99
  All gravatars will now come from the `employee_mail` field, not the default `email` or `mail` field and be in 16x16px in size
81
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
82
101
  fact that an object can be passed directly to `gravatar_tag` if it responds to `gravatar_url`, like:
@@ -85,10 +104,10 @@ fact that an object can be passed directly to `gravatar_tag` if it responds to `
85
104
  class User < ActiveRecord::Base
86
105
  gravatarify :size => 16, :secure => true
87
106
  end
88
-
107
+
89
108
  # view:
90
109
  <%= gravatar_tag @user %> # -> <img ... width="16" src="https://secure.gravatar..." height="16" />
91
-
110
+
92
111
  The `gravatar_tag` looks if the object responds to `gravatar_url` and if so, just passes the options to it,
93
112
  it works also with plain old ruby objects, of course :)
94
113
 
@@ -110,14 +129,14 @@ of `PoroUser`.
110
129
 
111
130
  No need for sophisticated stuff like view helpers and ActiveRecord integration, want to go back to the roots?
112
131
  Then feel free to use `Gravatarify::Base#build_gravatar_url` directly.
113
-
114
- For example, want to use `build_gravatar_url` in a Sinatra app?
115
-
116
- helpers Gravatarify::Base
117
132
 
118
- Yeah, that should work :). See {Gravatarify::Base#build_gravatar_url} for more informations and usage examples.
133
+ When the ability to display image tags is required in different view frameworks (like liquid!?),
134
+ then just ensure that `Gravatarify::Helper` is included in the framework in question.
135
+ See {Gravatarify::Base#build_gravatar_url} and of course {Gravatarify::Helper}
136
+ for more informations and usage examples.
119
137
 
120
- ## Need more control?
138
+ Need more control?
139
+ ==================
121
140
 
122
141
  <table>
123
142
  <tr>
@@ -163,12 +182,23 @@ Yeah, that should work :). See {Gravatarify::Base#build_gravatar_url} for more i
163
182
  <tr>
164
183
  <td><tt>:filetype</tt></td>
165
184
  <td>String, Symbol</td>
166
- <td>Change image type, gravatar.com supports <tt>:gif</tt>, <tt>:png</tt> and <tt>:jpg</tt>.</td>
185
+ <td>Change image type, gravatar.com supports <tt>:gif</tt>, <tt>:png</tt> and <tt>:jpg</tt>. If set to <tt>false</tt>
186
+ then a URL without an extension will be built (and gravatar.com then returns a JPG image).</td>
167
187
  <td><tt>:jpg</tt></td>
168
188
  </tr>
169
189
  </table>
170
190
 
171
- ## Not yet enough?
191
+ To options globally, access the `Gravatarify.options` hash and set any options which should apply to all
192
+ gravatar urls there. Of course all settings can be overridden locally:
193
+
194
+ # disable suffix and set default size to 16x16px
195
+ Gravatarify.options[:filetype] = false
196
+ Gravatarify.options[:size] = 16
197
+
198
+ gravatar_url(@user.email) # => http://0.gravatar.com/avatar/..f93ff1e?s=16
199
+ gravatar_url(@user.email, :filetype => :png) # => http://0.gravatar.com/avatar/..f93ff1e.png?s=16
200
+
201
+ ### Not yet enough?
172
202
 
173
203
  The `:default` option can be passed in a `Proc`, so this is certainly useful to for example
174
204
  to generate an image url, based on the request size:
@@ -195,7 +225,8 @@ Into the block is passed the options hash and as second parameter the object its
195
225
  Not only the `:default` option accepts a Proc, but also `:secure`, can be useful to handle cases where
196
226
  it should evaluate against `request.ssl?` for example.
197
227
 
198
- ## About the code
228
+ About the code
229
+ ==============
199
230
 
200
231
  Eventhough this library has less than 100 LOC, it's split into four files, maybe a bit
201
232
  of an overkill, though I like neat and tidy classes :)
@@ -214,9 +245,16 @@ of an overkill, though I like neat and tidy classes :)
214
245
  # gravatarify class method to add a gravatar_url
215
246
  # to any object.
216
247
 
217
- lib/gravatarify/view_helper.rb # Defines rails view helpers.
248
+ lib/gravatarify/helper.rb # Defines those view helpers, mainly gravatar_tag
249
+
250
+ ### Contribute
251
+
252
+ 1. Fork the project and hack away
253
+ 2. Ensure that the changes are well tested
254
+ 3. Send me a pull request
218
255
 
219
- ## Licence
256
+ Licence
257
+ =======
220
258
 
221
259
  Copyright (c) 2009 Lukas Westermann
222
260
 
data/Rakefile CHANGED
@@ -64,7 +64,7 @@ namespace :metrics do
64
64
  require 'code_statistics'
65
65
  dirs = {
66
66
  'Libraries' => 'lib',
67
- 'Unit tests' => 'test'
67
+ 'Unit tests' => 'test/unit'
68
68
  }.map { |name,dir| [name, File.join(File.dirname(__FILE__), dir)] }
69
69
  CodeStatistics.new(*dirs).to_s
70
70
  end
@@ -73,7 +73,7 @@ namespace :metrics do
73
73
  task :coverage do |t|
74
74
  rm_f "doc/coverage"
75
75
  mkdir_p "doc/coverage"
76
- rcov = %(rcov -Ilib:test --exclude '\/gems\/' -o doc/coverage -T test/*_test.rb )
76
+ rcov = %(rcov -Ilib:test --exclude '\/gems\/' -o doc/coverage -T test/unit/*_test.rb )
77
77
  system rcov
78
78
  end
79
79
 
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
- :minor: 1
3
+ :minor: 2
4
4
  :patch: 0
data/lib/gravatarify.rb CHANGED
@@ -4,8 +4,12 @@
4
4
  # custom implementations, just include Gravatarify::Base.
5
5
  require 'gravatarify/base'
6
6
  require 'gravatarify/object_support'
7
+ require 'gravatarify/helper'
7
8
 
8
9
  # setup for AR und DataMapper, note: DataMapper yet untested :) but I suppose it works, because
9
10
  # it works as expected on plain old ruby objects!
10
11
  ActiveRecord::Base.send(:include, Gravatarify::ObjectSupport) if defined?(ActiveRecord)
11
12
  DataMapper::Model.append_inclusions(Gravatarify::ObjectSupport) if defined?(DataMapper)
13
+
14
+ # and HAML support (if defined)
15
+ Haml::Helpers.send(:include, Gravatarify::Helper) if defined?(Haml)
@@ -32,6 +32,9 @@ module Gravatarify
32
32
  # # or set a custom default rating
33
33
  # Gravatarify.options[:rating] = :R
34
34
  #
35
+ # # or disable adding an extension
36
+ # Gravatarify.options[:filetype] = false
37
+ #
35
38
  def options; @options ||= {} end
36
39
 
37
40
  # Globally overide subdomains used to build gravatar urls, normally
@@ -57,7 +60,12 @@ module Gravatarify
57
60
  # defined.
58
61
  def subdomain(str); subdomains[str.hash % subdomains.size] || GRAVATAR_DEFAULT_SUBDOMAIN end
59
62
 
60
- def escape(str); defined?(Rack::Utils) ? Rack::Utils.escape(str) : CGI.escape(str) end
63
+ # Helper method to escape string using either <tt>Rack::Utils</tt> if available or else
64
+ # fallback to <tt>CGI#escape</tt>.
65
+ def escape(str)
66
+ str = str.to_s unless str.is_a?(String) # convert to string!
67
+ defined?(Rack::Utils) ? Rack::Utils.escape(str) : CGI.escape(str)
68
+ end
61
69
  end
62
70
 
63
71
  # Provides core support to build gravatar urls based on supplied e-mail strings.
@@ -103,9 +111,8 @@ module Gravatarify
103
111
  # FIXME: add symbolize_keys again, maybe just write custom method, so we do not depend on ActiveSupport magic...
104
112
  url_options = Gravatarify.options.merge(url_options)
105
113
  email_hash = Digest::MD5.hexdigest(Base.get_smart_email_from(email).strip.downcase)
106
-
107
- build_gravatar_host(email_hash, url_options.delete(:secure)) <<
108
- "/avatar/#{email_hash}.#{url_options.delete(:filetype) || GRAVATAR_DEFAULT_FILETYPE}#{build_gravatar_options(email, url_options)}"
114
+ extension = url_options[:filetype] == false ? '' : ".#{url_options.delete(:filetype) || GRAVATAR_DEFAULT_FILETYPE}"
115
+ build_gravatar_host(email_hash, url_options.delete(:secure)) << "/avatar/#{email_hash}#{extension}#{build_gravatar_options(email, url_options)}"
109
116
  end
110
117
 
111
118
  private
@@ -128,7 +135,7 @@ module Gravatarify
128
135
  url_options.each_pair do |key, value|
129
136
  key = GRAVATAR_ABBREV_OPTIONS[key] if GRAVATAR_ABBREV_OPTIONS.include?(key) # shorten key!
130
137
  value = value.call(url_options, source.is_a?(String) ? self : source) if key.to_s == 'd' and value.respond_to?(:call)
131
- params << "#{Gravatarify.escape(key.to_s)}=#{Gravatarify.escape(value.to_s)}" if value
138
+ params << "#{Gravatarify.escape(key)}=#{Gravatarify.escape(value)}" if value
132
139
  end
133
140
  "?#{params.sort * '&'}" unless params.empty?
134
141
  end
@@ -0,0 +1,32 @@
1
+ module Gravatarify::Helper
2
+ # so that it's possible to access that build_gravatar_url method
3
+ include Gravatarify::Base
4
+
5
+ # to simplify things a bit and have a neat-o naming
6
+ alias_method :gravatar_url, :build_gravatar_url
7
+
8
+ # Helper method for HAML to return a neat hash to be used as attributes in an image tag.
9
+ #
10
+ # Now it's as simple as doing something like:
11
+ #
12
+ # %img{ gravatar_attrs(@user.mail, :size => 20) }/
13
+ #
14
+ def gravatar_attrs(email, options = {})
15
+ url_options = options.reject { |key, value| !Gravatarify::GRAVATAR_OPTIONS.include?(key) }
16
+ options = options.delete_if { |key, value| Gravatarify::GRAVATAR_OPTIONS.include?(key) }
17
+ options[:alt] ||= Gravatarify::Base.get_smart_email_from(email) # use email as :alt attribute
18
+ options[:width] = options[:height] = (url_options[:size] || Gravatarify::GRAVATAR_DEFAULT_SIZE) # customize size
19
+ options[:src] = email.respond_to?(:gravatar_url) ? email.gravatar_url(url_options) : build_gravatar_url(email, url_options)
20
+ options
21
+ end
22
+
23
+ # Takes care of creating an <tt><img/></tt>-tag based on a gravatar url, it no longer
24
+ # makes use of any Rails helper, so is totally useable in any other library.
25
+ def gravatar_tag(email, options = {})
26
+ html_attrs = gravatar_attrs(email, options).map do |key,value|
27
+ escaped = defined?(Rack::Utils) ? Rack::Utils.escape_html(value) : CGI.escapeHTML(value)
28
+ "#{key}=\"#{escaped}\""
29
+ end.sort.join(" ")
30
+ "<img #{html_attrs} />"
31
+ end
32
+ end
data/rails/init.rb CHANGED
@@ -1,7 +1,4 @@
1
1
  require 'gravatarify'
2
2
 
3
- # load view helpers only if ActionView is available
4
- if defined?(ActionView)
5
- require 'gravatarify/view_helper'
6
- ActionView::Base.send(:include, Gravatarify::ViewHelper)
7
- end
3
+ # include view helpers only if ActionView is available
4
+ ActionView::Base.send(:include, Gravatarify::Helper) if defined?(ActionView)
data/test/test_helper.rb CHANGED
@@ -3,10 +3,20 @@ require 'test/unit'
3
3
  require 'shoulda'
4
4
  require 'rr'
5
5
 
6
+ require 'active_support'
7
+ begin; require 'activerecord'; rescue LoadError; end
8
+ begin; require 'dm-core'; rescue LoadError; end
9
+ require 'gravatarify'
10
+
6
11
  Test::Unit::TestCase.send :include, RR::Adapters::TestUnit
7
12
 
8
13
  # Reset +Gravatarify+ to default hosts and cleared options
9
14
  def reset_gravatarify!
10
15
  Gravatarify.options.clear
11
16
  Gravatarify.subdomains = Gravatarify::GRAVATAR_SUBDOMAINS
12
- end
17
+ end
18
+
19
+ # some often used values...
20
+ BELLA_AT_GMAIL = "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d"
21
+ BELLA_AT_GMAIL_JPG = "#{BELLA_AT_GMAIL}.jpg"
22
+ NIL_JPG = "http://1.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg"
@@ -1,9 +1,6 @@
1
1
  require 'test_helper'
2
- begin; require 'activerecord'; rescue LoadError; end
3
- begin; require 'dm-core'; rescue LoadError; end
4
- require 'gravatarify'
5
2
 
6
- class GravatarifyIntegrationTest < Test::Unit::TestCase
3
+ class GravatarifyArDmTest < Test::Unit::TestCase
7
4
  def setup; reset_gravatarify! end
8
5
 
9
6
  context "ActiveRecord::Base" do
@@ -1,5 +1,4 @@
1
1
  require 'test_helper'
2
- require 'gravatarify/base'
3
2
 
4
3
  class MockView
5
4
  include Gravatarify::Base
@@ -12,53 +11,51 @@ class GravatarifyBaseTest < Test::Unit::TestCase
12
11
 
13
12
  context "#build_gravatar_url, but without any options yet" do
14
13
  should "generate correct url for hash without options" do
15
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url('bella@gmail.com')
14
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url('bella@gmail.com')
16
15
  end
17
16
 
18
17
  should "trim and lowercase email address (as according to gravatar docs)" do
19
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url("\tbella@gmail.com \n\t")
20
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url("BELLA@gmail.COM")
21
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(" BELLA@GMAIL.com")
18
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url("\tbella@gmail.com \n\t")
19
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url("BELLA@gmail.COM")
20
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url(" BELLA@GMAIL.com")
22
21
  end
23
22
 
24
23
  should "handle a nil email as if it were an empty string" do
25
- assert_equal "http://1.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg", build_gravatar_url(nil)
26
- assert_equal "http://1.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg", build_gravatar_url('')
24
+ assert_equal NIL_JPG, build_gravatar_url(nil)
25
+ assert_equal NIL_JPG, build_gravatar_url('')
27
26
  end
28
27
  end
29
28
 
30
29
  context "#build_gravatar_url, with options" do
31
30
  should "add well known options like size, rating or default and always in alphabetical order" do
32
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16", build_gravatar_url('bella@gmail.com', :size => 16)
33
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Ftest.jpg&s=20",
34
- build_gravatar_url('bella@gmail.com', :size => 20, :default => 'http://example.com/test.jpg')
35
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?other=escaped%26yes%3F&r=x&s=30",
36
- build_gravatar_url('bella@gmail.com', :size => 30, :rating => :x, :other => "escaped&yes?")
31
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?s=16", build_gravatar_url('bella@gmail.com', :size => 16)
32
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Ftest.jpg&s=20", build_gravatar_url('bella@gmail.com', :size => 20, :default => 'http://example.com/test.jpg')
33
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?other=escaped%26yes%3F&r=x&s=30", build_gravatar_url('bella@gmail.com', :size => 30, :rating => :x, :other => "escaped&yes?")
37
34
  end
38
35
 
39
36
  should "ensure that all options as well as keys are escaped correctly" do
40
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?escaped%2Fme=escaped%2Fme",
41
- build_gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
37
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", build_gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
42
38
  end
43
39
 
44
40
  should "ignore false or nil options" do
45
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=24",
46
- build_gravatar_url('bella@gmail.com', :s => 24, :invalid => false, :other => nil)
41
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?s=24", build_gravatar_url('bella@gmail.com', :s => 24, :invalid => false, :other => nil)
47
42
  end
48
43
 
49
44
  should "allow different :filetype to be set, like 'gif' or 'png'" do
50
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.gif", build_gravatar_url('bella@gmail.com', :filetype => :gif)
51
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.png", build_gravatar_url('bella@gmail.com', :filetype => :png)
45
+ assert_equal "#{BELLA_AT_GMAIL}.gif", build_gravatar_url('bella@gmail.com', :filetype => :gif)
46
+ assert_equal "#{BELLA_AT_GMAIL}.png", build_gravatar_url('bella@gmail.com', :filetype => :png)
52
47
  end
53
48
 
49
+ should "skip :filetype if set to false" do
50
+ assert_equal "#{BELLA_AT_GMAIL}", build_gravatar_url('bella@gmail.com', :filetype => false)
51
+ assert_equal "#{BELLA_AT_GMAIL_JPG}", build_gravatar_url('bella@gmail.com', :filetype => nil)
52
+ end
53
+
54
54
  should "handle Procs as :default, to easily generate default urls based on supplied :size" do
55
55
  default = Proc.new { |*args| "http://example.com/gravatar#{args.first[:size] ? '-' + args.first[:size].to_s : ''}.jpg" }
56
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg",
57
- build_gravatar_url('bella@gmail.com', :default => default)
58
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar-25.jpg&s=25",
59
- build_gravatar_url('bella@gmail.com', :size => 25, :d => default)
60
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar-20.jpg&s=20",
61
- build_gravatar_url('bella@gmail.com', :size => 20, 'd' => default)
56
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com', :default => default)
57
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar-25.jpg&s=25", build_gravatar_url('bella@gmail.com', :size => 25, :d => default)
58
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar-20.jpg&s=20", build_gravatar_url('bella@gmail.com', :size => 20, 'd' => default)
62
59
  end
63
60
  end
64
61
 
@@ -67,21 +64,21 @@ class GravatarifyBaseTest < Test::Unit::TestCase
67
64
  obj = Object.new
68
65
  mock(obj).email { "bella@gmail.com" }
69
66
 
70
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(obj)
67
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url(obj)
71
68
  end
72
69
 
73
70
  should "look for :mail of field :email does not exist" do
74
71
  obj = Object.new
75
72
  mock(obj).mail { "bella@gmail.com" }
76
73
 
77
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(obj)
74
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url(obj)
78
75
  end
79
76
 
80
77
  should "finally just use to_s... if neither :email nor :mail exists" do
81
78
  obj = Object.new
82
79
  mock(obj).to_s { "bella@gmail.com" }
83
80
 
84
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(obj)
81
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url(obj)
85
82
  end
86
83
 
87
84
  should "handle Procs as :default and pass in the 'object' as second parameter" do
@@ -89,7 +86,7 @@ class GravatarifyBaseTest < Test::Unit::TestCase
89
86
  girl = Object.new
90
87
  mock(girl).email { "bella@gmail.com" }
91
88
  mock(girl).female? { true }
92
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar_girl.jpg", build_gravatar_url(girl, :default => default)
89
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar_girl.jpg", build_gravatar_url(girl, :default => default)
93
90
 
94
91
  boy = Object.new
95
92
  mock(boy).email { "hans@gmail.com" }
@@ -122,7 +119,7 @@ class GravatarifyBaseTest < Test::Unit::TestCase
122
119
 
123
120
  mock_no_ssl = MockView.new
124
121
  mock(mock_no_ssl).request.stub!.ssl? { false }
125
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", mock_no_ssl.build_gravatar_url('bella@gmail.com')
122
+ assert_equal BELLA_AT_GMAIL_JPG, mock_no_ssl.build_gravatar_url('bella@gmail.com')
126
123
  end
127
124
  end
128
125
 
@@ -134,13 +131,17 @@ class GravatarifyBaseTest < Test::Unit::TestCase
134
131
  end
135
132
 
136
133
  should "ensure that default options are always added" do
137
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg",
138
- build_gravatar_url('bella@gmail.com')
134
+ assert_equal "#{BELLA_AT_GMAIL}.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com')
139
135
  end
140
136
 
141
137
  should "ensure that default options can be overriden by passing options into build_gravatar_url call" do
142
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.gif?anything=else&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg",
143
- build_gravatar_url('bella@gmail.com', :anything => "else", :filetype => :gif)
138
+ assert_equal "#{BELLA_AT_GMAIL}.gif?anything=else&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com', :anything => "else", :filetype => :gif)
139
+ end
140
+
141
+ should "ensure that no filetypes are added when :filetype set to false, unless locally specified" do
142
+ Gravatarify.options[:filetype] = false
143
+ assert_equal "#{BELLA_AT_GMAIL}?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com')
144
+ assert_equal "#{BELLA_AT_GMAIL}.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com', :filetype => 'png')
144
145
  end
145
146
  end
146
147
  end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+ require 'gravatarify/helper'
3
+
4
+ class GravatarifyHelpersTest < Test::Unit::TestCase
5
+ include Gravatarify::Helper
6
+
7
+ def setup
8
+ # just ensure that no global options are defined when starting next test
9
+ reset_gravatarify!
10
+ end
11
+
12
+ context "#gravatar_url" do
13
+ should "return same urls as build_gravatar_url" do
14
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url('bella@gmail.com')
15
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=x&s=16", gravatar_url('bella@gmail.com', :d => 'x', :s => 16)
16
+ end
17
+ end
18
+
19
+ context "#gravatar_attrs" do
20
+ should "return hash with :height, :width, :alt and :src defined" do
21
+ hash = gravatar_attrs('bella@gmail.com', :size => 16)
22
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?s=16", hash[:src]
23
+ assert_equal 16, hash[:width]
24
+ assert_equal 16, hash[:height]
25
+ assert_equal 'bella@gmail.com', hash[:alt]
26
+ assert_nil hash[:size]
27
+ end
28
+
29
+ should "allow any param to be defined/overridden, except src, width and heigth" do
30
+ hash = gravatar_attrs('bella@gmail.com', :size => 20, :r => :x, :height => 40, :alt => 'bella', :id => 'test', :title => 'something', :class => 'gravatar')
31
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?r=x&s=20", hash[:src]
32
+ assert_equal 20, hash[:width]
33
+ assert_equal 20, hash[:height]
34
+ assert_equal 'bella', hash[:alt]
35
+ assert_equal 'test', hash[:id]
36
+ assert_equal 'something', hash[:title]
37
+ assert_equal 'gravatar', hash[:class]
38
+ assert_nil hash[:size]
39
+ assert_nil hash[:r]
40
+ end
41
+ end
42
+
43
+ context "#gravatar_tag helper" do
44
+ should "create <img/> tag with correct gravatar urls" do
45
+ assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />', gravatar_tag('bella@gmail.com')
46
+ end
47
+
48
+ should "create <img/> tags and handle all options correctly, other options should be passed to Rails' image_tag" do
49
+ assert_equal '<img alt="bella@gmail.com" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
50
+ gravatar_tag('bella@gmail.com', :size => 16)
51
+ assert_equal '<img alt="bella@gmail.com" class="gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=x&amp;s=16" width="16" />',
52
+ gravatar_tag('bella@gmail.com', :class => "gravatar", :size => 16, :d => "x")
53
+ end
54
+
55
+ should "ensure that all values are correctly html-esacped!" do
56
+ assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" title="&lt;&gt;" width="80" />',
57
+ gravatar_tag('bella@gmail.com', :title => '<>')
58
+ end
59
+ end
60
+
61
+ context "#gravatar_tag when passed in an object" do
62
+ should "create <img/>-tag based on :email field" do
63
+ obj = Object.new
64
+ mock(obj).email.times(2) { "bella@gmail.com" }
65
+
66
+ assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
67
+ gravatar_tag(obj)
68
+ end
69
+
70
+ should "create <img/>-tag based on gravatar_url from object if object responds to gravatar_url" do
71
+ obj = Object.new
72
+ mock(obj).gravatar_url({ :size => 16 }) { "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" }
73
+
74
+ assert_equal '<img alt="Gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
75
+ gravatar_tag(obj, :size => 16, :alt => "Gravatar")
76
+ end
77
+ end
78
+ end
@@ -1,6 +1,4 @@
1
1
  require 'test_helper'
2
- require 'gravatarify/base'
3
- require 'gravatarify/object_support'
4
2
 
5
3
  class PoroUser
6
4
  include Gravatarify::ObjectSupport
@@ -1,9 +1,9 @@
1
1
  require 'test_helper'
2
- require 'gravatarify/base'
2
+ require 'cgi'
3
3
 
4
4
  class GravatarifyRackVsCgiTest < Test::Unit::TestCase
5
5
  include Gravatarify::Base
6
-
6
+
7
7
  # Reload Rack::Utils
8
8
  def teardown
9
9
  begin; require('rack/utils'); rescue LoadError; end
@@ -18,8 +18,7 @@ class GravatarifyRackVsCgiTest < Test::Unit::TestCase
18
18
  should "fallback to CGI#escape" do
19
19
  assert !defined?(Rack::Utils), 'Rack::Utils should no longer be defined'
20
20
  assert defined?(CGI), "CGI should be defined"
21
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?escaped%2Fme=escaped%2Fme",
22
- build_gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
21
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", build_gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
23
22
  end
24
23
  end
25
24
  end
@@ -1,5 +1,7 @@
1
1
  require 'test_helper'
2
- require 'gravatarify/base'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
3
5
 
4
6
  class GravatarifySubdomainTest < Test::Unit::TestCase
5
7
  include Gravatarify::Base
@@ -32,5 +34,31 @@ class GravatarifySubdomainTest < Test::Unit::TestCase
32
34
  assert_equal "http://www.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg", build_gravatar_url('info@initech.com')
33
35
  assert_equal "http://www.gravatar.com/avatar/d4489907918035d0bc6ff3f6c76e760d.jpg", build_gravatar_url('support@initech.com')
34
36
  end
35
- end
37
+ end
38
+
39
+ context "with Net::HTTP the gravatar.com subdomains" do
40
+ should "return an image of type image/jpeg" do
41
+ Gravatarify.subdomains.each do |subdomain|
42
+ response = Net::HTTP.get_response URI.parse("http://#{subdomain}.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg")
43
+ assert_equal 200, response.code.to_i
44
+ assert_equal "image/jpeg", response.content_type
45
+ end
46
+ end
47
+
48
+ should "not respond to 3.gravatar.com, if so add to subdomains dude!!!" do
49
+ assert_raises(SocketError) { Net::HTTP.get_response URI.parse('http://3.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg') }
50
+ end
51
+
52
+ should "respond to https://secure.gravatar.com/ urls as well" do
53
+ http = Net::HTTP.new('secure.gravatar.com', 443)
54
+ http.use_ssl = true
55
+
56
+ # do not verify peer certificate (get rid of that warning dude!)
57
+ http.instance_variable_get('@ssl_context').verify_mode = OpenSSL::SSL::VERIFY_NONE
58
+
59
+ response = http.get '/avatar/4979dd9653e759c78a81d4997f56bae2.jpg'
60
+ assert_equal 200, response.code.to_i
61
+ assert_equal "image/jpeg", response.content_type
62
+ end
63
+ end
36
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gravatarify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lukas Westermann
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-09 00:00:00 +02:00
12
+ date: 2009-10-23 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -32,16 +32,16 @@ files:
32
32
  - init.rb
33
33
  - lib/gravatarify.rb
34
34
  - lib/gravatarify/base.rb
35
+ - lib/gravatarify/helper.rb
35
36
  - lib/gravatarify/object_support.rb
36
- - lib/gravatarify/view_helper.rb
37
37
  - rails/init.rb
38
- - test/gravatarify_base_test.rb
39
- - test/gravatarify_integration_test.rb
40
- - test/gravatarify_object_support_test.rb
41
- - test/gravatarify_rack_vs_cgi_test.rb
42
- - test/gravatarify_subdomain_test.rb
43
- - test/gravatarify_view_helper_test.rb
44
38
  - test/test_helper.rb
39
+ - test/unit/gravatarify_ar_dm_test.rb
40
+ - test/unit/gravatarify_base_test.rb
41
+ - test/unit/gravatarify_helper_test.rb
42
+ - test/unit/gravatarify_object_support_test.rb
43
+ - test/unit/gravatarify_rack_vs_cgi_test.rb
44
+ - test/unit/gravatarify_subdomain_test.rb
45
45
  has_rdoc: true
46
46
  homepage: http://github.com/lwe/gravatarify
47
47
  licenses:
@@ -71,10 +71,10 @@ signing_key:
71
71
  specification_version: 3
72
72
  summary: Awesome gravatar support for ruby (and rails).
73
73
  test_files:
74
- - test/gravatarify_base_test.rb
75
- - test/gravatarify_integration_test.rb
76
- - test/gravatarify_object_support_test.rb
77
- - test/gravatarify_rack_vs_cgi_test.rb
78
- - test/gravatarify_subdomain_test.rb
79
- - test/gravatarify_view_helper_test.rb
80
74
  - test/test_helper.rb
75
+ - test/unit/gravatarify_ar_dm_test.rb
76
+ - test/unit/gravatarify_base_test.rb
77
+ - test/unit/gravatarify_helper_test.rb
78
+ - test/unit/gravatarify_object_support_test.rb
79
+ - test/unit/gravatarify_rack_vs_cgi_test.rb
80
+ - test/unit/gravatarify_subdomain_test.rb
@@ -1,24 +0,0 @@
1
- # The view helper, especially for the +gravatar_tag+ helper, requires
2
- # rails!
3
- module Gravatarify::ViewHelper
4
- include Gravatarify::Base
5
-
6
- # Ensure proper gravatar_url method is available!
7
- alias_method :gravatar_url, :build_gravatar_url
8
-
9
- # Create <img .../> tag by passing +email+ to +gravatar_url+, is based
10
- # on rails +image_tag+ helper method.
11
- #
12
- # <%= gravatar_tag(current_user.email, :size => 20) %> # -> <img alt="... height="20" src="http://grava... width="20" />
13
- # <%= gravatar_tag('foo@bar.com', :class => "gravatar") # -> <img alt="foo@bar.com" class="gravatar" height="80" ... width="80" />
14
- #
15
- # Note: this method tries to be very clever about which options need to be passed to
16
- # +gravatar_url+ and which to +image_tag+, so using this method it's not possible to
17
- # send arbitary attributes to +gravatar_url+ and have them included in the url.
18
- def gravatar_tag(email, options = {})
19
- url_options = options.symbolize_keys.reject { |key,value| !Gravatarify::GRAVATAR_OPTIONS.include?(key) }
20
- options[:alt] ||= Gravatarify::Base.get_smart_email_from(email) # use email as :alt attribute
21
- options[:width] = options[:height] = (url_options[:size] || Gravatarify::GRAVATAR_DEFAULT_SIZE) # customize size
22
- image_tag(email.respond_to?(:gravatar_url) ? email.gravatar_url(url_options) : gravatar_url(email, url_options), options)
23
- end
24
- end
@@ -1,47 +0,0 @@
1
- require 'test_helper'
2
- require 'active_support'
3
- require 'action_view/helpers'
4
- require 'gravatarify'
5
- require 'gravatarify/view_helper'
6
-
7
- class GravatarifyViewHelperTest < Test::Unit::TestCase
8
- include ActionView::Helpers
9
- include Gravatarify::ViewHelper
10
-
11
- def setup
12
- # just ensure that no global options are defined when starting next test
13
- reset_gravatarify!
14
- end
15
-
16
- context "#gravatar_tag helper" do
17
- should "create <img/> tag with correct gravatar urls" do
18
- assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
19
- gravatar_tag('bella@gmail.com')
20
- end
21
-
22
- should "create <img/> tags and handle all options correctly, other options should be passed to Rails' image_tag" do
23
- assert_equal '<img alt="bella@gmail.com" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
24
- gravatar_tag('bella@gmail.com', :size => 16)
25
- assert_equal '<img alt="bella@gmail.com" class="gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
26
- gravatar_tag('bella@gmail.com', :class => "gravatar", :size => 16)
27
- end
28
- end
29
-
30
- context "#gravatar_tag when passed in an object" do
31
- should "create <img/>-tag based on :email field" do
32
- obj = Object.new
33
- mock(obj).email.times(2) { "bella@gmail.com" }
34
-
35
- assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
36
- gravatar_tag(obj)
37
- end
38
-
39
- should "create <img/>-tag based on gravatar_url from object if object responds to gravatar_url" do
40
- obj = Object.new
41
- mock(obj).gravatar_url({ :size => 16 }) { "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" }
42
-
43
- assert_equal '<img alt="Gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
44
- gravatar_tag(obj, :size => 16, :alt => "Gravatar")
45
- end
46
- end
47
- end