gravatarify 1.2.1 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,6 @@
1
1
  module Gravatarify::Helper
2
- # so that it's possible to access that build_gravatar_url method
3
2
  include Gravatarify::Base
4
-
5
- # Allow HTML options to be overriden globally as well, useful
6
- # to e.g. define a common alt attribute, or class.
7
- #
8
- # Gravatarify::Helper.html_options[:title] = "Gravatar"
9
- #
10
- # @return [Hash] globally defined html attributes
11
- def self.html_options; @html_options ||= { :alt => '' } end
12
-
13
- # To simplify things a bit and have a neat-o naming
14
- alias_method :gravatar_url, :build_gravatar_url
15
-
3
+
16
4
  # Helper method for HAML to return a neat hash to be used as attributes in an image tag.
17
5
  #
18
6
  # Now it's as simple as doing something like:
@@ -23,14 +11,15 @@ module Gravatarify::Helper
23
11
  #
24
12
  # @param [String, #email, #mail, #gravatar_url] email a string or an object used
25
13
  # to generate to gravatar url for.
26
- # @param [Hash] options other gravatar or html options for building the resulting
14
+ # @param [Symbol, Hash] *params other gravatar or html options for building the resulting
27
15
  # hash.
28
16
  # @return [Hash] all html attributes required to build an +img+ tag.
29
- def gravatar_attrs(email, options = {})
30
- url_options = options.inject({}) { |hsh, (key, value)| hsh[key] = options.delete(key) if Gravatarify::GRAVATAR_OPTIONS.include?(key.to_sym); hsh }
31
- options[:width] = options[:height] = (url_options[:size] || 80) # customize size
32
- options[:src] = email.respond_to?(:gravatar_url) ? email.gravatar_url(url_options) : build_gravatar_url(email, url_options)
33
- Gravatarify::Helper.html_options.merge(options)
17
+ def gravatar_attrs(email, *params)
18
+ url_options = Gravatarify::Utils.merge_gravatar_options(*params)
19
+ options = url_options[:html] || {}
20
+ options[:src] = gravatar_url(email, false, url_options)
21
+ options[:width] = options[:height] = (url_options[:size] || 80) # customize size
22
+ { :alt => '' }.merge!(options) # to ensure validity merge with :alt => ''!
34
23
  end
35
24
 
36
25
  # Takes care of creating an <tt><img/></tt>-tag based on a gravatar url, it no longer
@@ -38,14 +27,11 @@ module Gravatarify::Helper
38
27
  #
39
28
  # @param [String, #email, #mail, #gravatar_url] email a string or an object used
40
29
  # to generate the gravatar url from
41
- # @param [Hash] options other gravatar or html options for building the resulting
30
+ # @param [Symbol, Hash] *params other gravatar or html options for building the resulting
42
31
  # image tag.
43
32
  # @return [String] a complete and hopefully valid +img+ tag.
44
- def gravatar_tag(email, options = {})
45
- html_attrs = gravatar_attrs(email, options).map do |key,value|
46
- escaped = defined?(Rack::Utils) ? Rack::Utils.escape_html(value) : CGI.escapeHTML(value)
47
- "#{key}=\"#{escaped}\""
48
- end.sort.join(" ")
33
+ def gravatar_tag(email, *params)
34
+ html_attrs = gravatar_attrs(email, *params).map { |key,value| "#{key}=\"#{Gravatarify::Utils.escape_html(value)}\"" }.sort.join(" ")
49
35
  "<img #{html_attrs} />"
50
36
  end
51
37
  end
@@ -0,0 +1,44 @@
1
+ begin; require 'rack/utils'; rescue LoadError; require 'cgi' end
2
+
3
+ module Gravatarify
4
+ module Utils #:nodoc:
5
+ # Helper method to URI escape a string using either <tt>Rack::Utils#escape</tt> if available or else
6
+ # fallback to <tt>CGI#escape</tt>.
7
+ def self.escape(str)
8
+ str = str.to_s
9
+ defined?(Rack::Utils) ? Rack::Utils.escape(str) : CGI.escape(str)
10
+ end
11
+
12
+ # Escape HTML entities in string, basically falls back to either <tt>RackUtils#escape_html</tt>
13
+ # or <tt>CGI#escapeHTML</tt>.
14
+ def self.escape_html(str)
15
+ str = str.to_s
16
+ defined?(Rack::Utils) ? Rack::Utils.escape_html(str) : CGI.escapeHTML(str)
17
+ end
18
+
19
+ # Merge supplied list of +params+ with the globally defined default options and
20
+ # any params. Then merge remaining params as hash.
21
+ #
22
+ def self.merge_gravatar_options(*params)
23
+ return (params[1] || {}) if params.first == false
24
+ options = Gravatarify.options.dup
25
+ deep_merge_html!(options, Gravatarify.styles[params.shift] || {}) unless params.first.is_a?(Hash)
26
+ deep_merge_html!(options, params.first) unless params.empty?
27
+ options
28
+ end
29
+
30
+ def self.deep_merge_html!(hash, to_merge)
31
+ html = (hash[:html] || {}).merge(to_merge[:html] || {})
32
+ hash.merge!(to_merge)
33
+ hash[:html] = html unless html.empty?
34
+ end
35
+
36
+ # Tries first to call +email+, then +mail+ then +to_s+ on supplied
37
+ # object.
38
+ def self.smart_email(obj)
39
+ str = (obj.respond_to?(:email) ? obj.send(:email) : (obj.respond_to?(:mail) ? obj.send(:mail) : obj)).to_s
40
+ str = str.sub(/\A.*?<(.+?)>.*\z/, '\1') if str =~ /<.+>/
41
+ str.strip.downcase
42
+ end
43
+ end
44
+ end
data/test/test_helper.rb CHANGED
@@ -3,9 +3,6 @@ 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
6
  require 'gravatarify'
10
7
 
11
8
  Test::Unit::TestCase.send :include, RR::Adapters::TestUnit
@@ -14,12 +11,7 @@ Test::Unit::TestCase.send :include, RR::Adapters::TestUnit
14
11
  def reset_gravatarify!
15
12
  Gravatarify.options.clear
16
13
  Gravatarify.options[:filetype] = :jpg
17
- Gravatarify.subdomains = %w{ 0 1 2 www }
18
-
19
- if defined?(Gravatarify::Helper)
20
- Gravatarify::Helper.html_options.clear
21
- Gravatarify::Helper.html_options[:alt] = ''
22
- end
14
+ Gravatarify.subdomains = %w{ 0 1 2 www }
23
15
  end
24
16
 
25
17
  # some often used values...
@@ -9,79 +9,83 @@ class GravatarifyBaseTest < Test::Unit::TestCase
9
9
 
10
10
  def setup; reset_gravatarify! end
11
11
 
12
- context "#build_gravatar_url, but without any options yet" do
12
+ context "#gravatar_url, but without any options yet" do
13
13
  should "generate correct url for hash without options" do
14
- assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url('bella@gmail.com')
14
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url('bella@gmail.com')
15
15
  end
16
16
 
17
17
  should "trim and lowercase email address (as according to gravatar docs)" do
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")
18
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url("\tbella@gmail.com \n\t")
19
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url("BELLA@gmail.COM")
20
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url(" BELLA@GMAIL.com")
21
21
  end
22
22
 
23
23
  should "handle a nil email as if it were an empty string" do
24
- assert_equal NIL_JPG, build_gravatar_url(nil)
25
- assert_equal NIL_JPG, build_gravatar_url('')
24
+ assert_equal NIL_JPG, gravatar_url(nil)
25
+ assert_equal NIL_JPG, gravatar_url('')
26
+ end
27
+
28
+ should "be aliased to #build_gravatar_url, for backwards compatibility" do
29
+ assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url('bella@gmail.com')
26
30
  end
27
31
  end
28
32
 
29
- context "#build_gravatar_url, with options" do
33
+ context "#gravatar_url, with options" do
30
34
  should "add well known options like size, rating or default and always in alphabetical order" do
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?")
35
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?s=16", gravatar_url('bella@gmail.com', :size => 16)
36
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Ftest.jpg&s=20", gravatar_url('bella@gmail.com', :size => 20, :default => 'http://example.com/test.jpg')
37
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?other=escaped%26yes%3F&r=x&s=30", gravatar_url('bella@gmail.com', :size => 30, :rating => :x, :other => "escaped&yes?")
34
38
  end
35
39
 
36
40
  should "ensure that all options as well as keys are escaped correctly" do
37
- assert_equal "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", build_gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
41
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
38
42
  end
39
43
 
40
44
  should "ignore false or nil options" do
41
- assert_equal "#{BELLA_AT_GMAIL_JPG}?s=24", build_gravatar_url('bella@gmail.com', :s => 24, :invalid => false, :other => nil)
45
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?s=24", gravatar_url('bella@gmail.com', :s => 24, :invalid => false, :other => nil)
42
46
  end
43
47
 
44
48
  should "allow different :filetype to be set, like 'gif' or 'png'" do
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)
49
+ assert_equal "#{BELLA_AT_GMAIL}.gif", gravatar_url('bella@gmail.com', :filetype => :gif)
50
+ assert_equal "#{BELLA_AT_GMAIL}.png", gravatar_url('bella@gmail.com', :filetype => :png)
47
51
  end
48
52
 
49
53
  should "skip :filetype if set to false, nil or ''" do
50
- assert_equal "#{BELLA_AT_GMAIL}", build_gravatar_url('bella@gmail.com', :filetype => false)
51
- assert_equal "#{BELLA_AT_GMAIL}", build_gravatar_url('bella@gmail.com', :filetype => nil)
52
- assert_equal "#{BELLA_AT_GMAIL}", build_gravatar_url('bella@gmail.com', :filetype => '')
53
- assert_equal "#{BELLA_AT_GMAIL}.foobar", build_gravatar_url('bella@gmail.com', :filetype => 'foobar')
54
- assert_equal "#{BELLA_AT_GMAIL}.gif", build_gravatar_url('bella@gmail.com', :filetype => :gif)
54
+ assert_equal "#{BELLA_AT_GMAIL}", gravatar_url('bella@gmail.com', :filetype => false)
55
+ assert_equal "#{BELLA_AT_GMAIL}", gravatar_url('bella@gmail.com', :filetype => nil)
56
+ assert_equal "#{BELLA_AT_GMAIL}", gravatar_url('bella@gmail.com', :filetype => '')
57
+ assert_equal "#{BELLA_AT_GMAIL}.foobar", gravatar_url('bella@gmail.com', :filetype => 'foobar')
58
+ assert_equal "#{BELLA_AT_GMAIL}.gif", gravatar_url('bella@gmail.com', :filetype => :gif)
55
59
  end
56
60
 
57
61
  should "handle Procs as :default, to easily generate default urls based on supplied :size" do
58
62
  default = Proc.new { |*args| "http://example.com/gravatar#{args.first[:size] ? '-' + args.first[:size].to_s : ''}.jpg" }
59
- assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com', :default => default)
60
- 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)
61
- 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)
63
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", gravatar_url('bella@gmail.com', :default => default)
64
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar-25.jpg&s=25", gravatar_url('bella@gmail.com', :size => 25, :d => default)
65
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar-20.jpg&s=20", gravatar_url('bella@gmail.com', :size => 20, 'd' => default)
62
66
  end
63
67
  end
64
68
 
65
- context "#build_gravatar_url when passed in an object" do
66
- should "look for :email method and use it to generate build_gravatar_url from" do
69
+ context "#gravatar_url when passed in an object" do
70
+ should "look for :email method and use it to generate gravatar_url from" do
67
71
  obj = Object.new
68
72
  mock(obj).email { "bella@gmail.com" }
69
73
 
70
- assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url(obj)
74
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url(obj)
71
75
  end
72
76
 
73
77
  should "look for :mail of field :email does not exist" do
74
78
  obj = Object.new
75
79
  mock(obj).mail { "bella@gmail.com" }
76
80
 
77
- assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url(obj)
81
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url(obj)
78
82
  end
79
83
 
80
84
  should "finally just use to_s... if neither :email nor :mail exists" do
81
85
  obj = Object.new
82
86
  mock(obj).to_s { "bella@gmail.com" }
83
87
 
84
- assert_equal BELLA_AT_GMAIL_JPG, build_gravatar_url(obj)
88
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url(obj)
85
89
  end
86
90
 
87
91
  should "handle Procs as :default and pass in the 'object' as second parameter" do
@@ -89,28 +93,32 @@ class GravatarifyBaseTest < Test::Unit::TestCase
89
93
  girl = Object.new
90
94
  mock(girl).email { "bella@gmail.com" }
91
95
  mock(girl).female? { true }
92
- assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar_girl.jpg", build_gravatar_url(girl, :default => default)
96
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=http%3A%2F%2Fexample.com%2Fgravatar_girl.jpg", gravatar_url(girl, :default => default)
93
97
 
94
98
  boy = Object.new
95
99
  mock(boy).email { "hans@gmail.com" }
96
100
  mock(boy).female? { false }
97
- assert_equal "http://www.gravatar.com/avatar/b6987c8f1d734e684cf9721970b906e5.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url(boy, :default => default)
98
- end
101
+ assert_equal "http://www.gravatar.com/avatar/b6987c8f1d734e684cf9721970b906e5.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", gravatar_url(boy, :default => default)
102
+ end
103
+
104
+ should "work easily work with RFC emails encapsulated in < and >" do
105
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url('Bella <bella@gmail.com>')
106
+ end
99
107
  end
100
108
 
101
109
  context "Gravatar hosts support" do
102
110
  should "switch to different hosts based on generated email hash, yet always the same for consecutive calls with the same email!" do
103
- assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url('bella@gmail.com')
104
- assert_equal build_gravatar_url('bella@gmail.com'), build_gravatar_url('bella@gmail.com')
105
- assert_equal "http://1.gravatar.com/avatar/41d86cad3dd465d6913d5a3232744441.jpg", build_gravatar_url('bella@bella.com')
106
- assert_equal "http://2.gravatar.com/avatar/8f3af64e9c215d158b062a7b154e071e.jpg", build_gravatar_url('bella@hotmail.com')
107
- assert_equal "http://www.gravatar.com/avatar/d2279c22a33da2cb57defd21c33c8ec5.jpg", build_gravatar_url('bella@yahoo.de')
111
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", gravatar_url('bella@gmail.com')
112
+ assert_equal gravatar_url('bella@gmail.com'), gravatar_url('bella@gmail.com')
113
+ assert_equal "http://1.gravatar.com/avatar/41d86cad3dd465d6913d5a3232744441.jpg", gravatar_url('bella@bella.com')
114
+ assert_equal "http://2.gravatar.com/avatar/8f3af64e9c215d158b062a7b154e071e.jpg", gravatar_url('bella@hotmail.com')
115
+ assert_equal "http://www.gravatar.com/avatar/d2279c22a33da2cb57defd21c33c8ec5.jpg", gravatar_url('bella@yahoo.de')
108
116
  end
109
117
 
110
118
  should "switch to https://secure.gravatar.com if :secure => true is supplied" do
111
- assert_equal "https://secure.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url('bella@gmail.com', :secure => true)
112
- assert_equal "https://secure.gravatar.com/avatar/41d86cad3dd465d6913d5a3232744441.jpg", build_gravatar_url('bella@bella.com', :secure => true)
113
- assert_equal "https://secure.gravatar.com/avatar/d2279c22a33da2cb57defd21c33c8ec5.jpg", build_gravatar_url('bella@yahoo.de', :secure => true)
119
+ assert_equal "https://secure.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", gravatar_url('bella@gmail.com', :secure => true)
120
+ assert_equal "https://secure.gravatar.com/avatar/41d86cad3dd465d6913d5a3232744441.jpg", gravatar_url('bella@bella.com', :secure => true)
121
+ assert_equal "https://secure.gravatar.com/avatar/d2279c22a33da2cb57defd21c33c8ec5.jpg", gravatar_url('bella@yahoo.de', :secure => true)
114
122
  end
115
123
 
116
124
  should "allow Procs for :secure option, enables pretty cool stuff for stuff like request.ssl?" do
@@ -118,11 +126,11 @@ class GravatarifyBaseTest < Test::Unit::TestCase
118
126
 
119
127
  mock_ssl = MockView.new
120
128
  mock(mock_ssl).request.stub!.ssl? { true }
121
- assert_equal "https://secure.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", mock_ssl.build_gravatar_url('bella@gmail.com')
129
+ assert_equal "https://secure.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", mock_ssl.gravatar_url('bella@gmail.com')
122
130
 
123
131
  mock_no_ssl = MockView.new
124
132
  mock(mock_no_ssl).request.stub!.ssl? { false }
125
- assert_equal BELLA_AT_GMAIL_JPG, mock_no_ssl.build_gravatar_url('bella@gmail.com')
133
+ assert_equal BELLA_AT_GMAIL_JPG, mock_no_ssl.gravatar_url('bella@gmail.com')
126
134
  end
127
135
  end
128
136
 
@@ -134,17 +142,17 @@ class GravatarifyBaseTest < Test::Unit::TestCase
134
142
  end
135
143
 
136
144
  should "ensure that default options are always added" do
137
- assert_equal "#{BELLA_AT_GMAIL}.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com')
145
+ assert_equal "#{BELLA_AT_GMAIL}.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", gravatar_url('bella@gmail.com')
138
146
  end
139
147
 
140
- should "ensure that default options can be overriden by passing options into build_gravatar_url call" do
141
- 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)
148
+ should "ensure that default options can be overriden by passing options into gravatar_url call" do
149
+ assert_equal "#{BELLA_AT_GMAIL}.gif?anything=else&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", gravatar_url('bella@gmail.com', :anything => "else", :filetype => :gif)
142
150
  end
143
151
 
144
152
  should "ensure that no filetypes are added when :filetype set to false, unless locally specified" do
145
153
  Gravatarify.options[:filetype] = false
146
- assert_equal "#{BELLA_AT_GMAIL}?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com')
147
- assert_equal "#{BELLA_AT_GMAIL}.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", build_gravatar_url('bella@gmail.com', :filetype => 'png')
154
+ assert_equal "#{BELLA_AT_GMAIL}?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", gravatar_url('bella@gmail.com')
155
+ assert_equal "#{BELLA_AT_GMAIL}.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg", gravatar_url('bella@gmail.com', :filetype => 'png')
148
156
  end
149
157
  end
150
158
  end
@@ -8,13 +8,6 @@ class GravatarifyHelpersTest < Test::Unit::TestCase
8
8
  # just ensure that no global options are defined when starting next test
9
9
  reset_gravatarify!
10
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
11
 
19
12
  context "#gravatar_attrs" do
20
13
  should "return hash with :height, :width, :alt and :src defined" do
@@ -25,9 +18,10 @@ class GravatarifyHelpersTest < Test::Unit::TestCase
25
18
  end
26
19
 
27
20
  should "allow any param to be defined/overridden, except src, width and heigth" do
28
- hash = gravatar_attrs('bella@gmail.com', :size => 20, :r => :x, :height => 40, :alt => 'bella', :id => 'test', :title => 'something', :class => 'gravatar')
21
+ hash = gravatar_attrs('bella@gmail.com', :size => 20, :r => :x, :foo => 40,
22
+ :html => { :alt => 'bella', :id => 'test', :title => 'something', :class => 'gravatar'})
29
23
  expected = {
30
- :alt => 'bella', :src => "#{BELLA_AT_GMAIL_JPG}?r=x&s=20", :width => 20, :height => 20,
24
+ :alt => 'bella', :src => "#{BELLA_AT_GMAIL_JPG}?foo=40&r=x&s=20", :width => 20, :height => 20,
31
25
  :id => 'test', :title => 'something', :class => 'gravatar'
32
26
  }
33
27
  assert_equal expected, hash
@@ -45,12 +39,12 @@ class GravatarifyHelpersTest < Test::Unit::TestCase
45
39
  assert_equal '<img alt="" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
46
40
  gravatar_tag('bella@gmail.com', :size => 16)
47
41
  assert_equal '<img alt="" class="gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=x&amp;s=16" width="16" />',
48
- gravatar_tag('bella@gmail.com', :class => "gravatar", :size => 16, :d => "x")
42
+ gravatar_tag('bella@gmail.com', :html => { :class => "gravatar" }, :size => 16, :d => "x")
49
43
  end
50
44
 
51
45
  should "ensure that all values are correctly html-esacped!" do
52
46
  assert_equal '<img alt="" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" title="&lt;&gt;" width="80" />',
53
- gravatar_tag('bella@gmail.com', :title => '<>')
47
+ gravatar_tag('bella@gmail.com', :html => { :title => '<>' })
54
48
  end
55
49
  end
56
50
 
@@ -61,26 +55,16 @@ class GravatarifyHelpersTest < Test::Unit::TestCase
61
55
 
62
56
  assert_equal '<img alt="" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
63
57
  gravatar_tag(obj)
64
- end
65
-
66
- should "create <img/>-tag based on gravatar_url from object if object responds to gravatar_url" do
67
- obj = Object.new
68
- mock(obj).name { "Mr. X" }
69
- mock(obj).gravatar_url({ :size => 16 }) { "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" }
70
-
71
- assert_equal '<img alt="Gravatar for Mr. X" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
72
- gravatar_tag(obj, :size => 16, :alt => "Gravatar for #{obj.name}")
73
- end
58
+ end
74
59
  end
75
60
 
76
61
  context "Gravatarify::Helper#html_options" do
77
62
  should "add be added to all tags/hashes created by gravatar_tag or gravatar_attrs" do
78
- Gravatarify::Helper.html_options[:title] = "Gravatar" # add a title attribute, yeah neat-o!
79
- Gravatarify::Helper.html_options[:class] = "gravatar"
63
+ Gravatarify.options[:html] = { :title => "Gravatar", :class => "gravatar" } # add a title attribute, yeah neat-o!
80
64
 
81
65
  assert_equal '<img alt="" class="gravatar" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" title="Gravatar" width="80" />',
82
66
  gravatar_tag('bella@gmail.com')
83
- hash = gravatar_attrs('bella@gmail.com', :size => 20, :title => "Gravatar for Bella", :id => "test")
67
+ hash = gravatar_attrs('bella@gmail.com', :size => 20, :html => { :title => "Gravatar for Bella", :id => "test" })
84
68
  expected = {
85
69
  :alt => "", :width => 20, :height => 20, :src => "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=20",
86
70
  :title => "Gravatar for Bella", :id => "test", :class => "gravatar"
@@ -89,21 +73,19 @@ class GravatarifyHelpersTest < Test::Unit::TestCase
89
73
  end
90
74
 
91
75
  should "not allow :src, :height or :width to be set via global options and all local options should override!" do
92
- Gravatarify::Helper.html_options[:src] = "avatar-30.jpg"
93
- Gravatarify::Helper.html_options[:width] = 30
94
- Gravatarify::Helper.html_options[:title] = "Avatar"
76
+ Gravatarify.options[:html] = { :src => "avatar-30.jpg", :width => 30, :title => "Avatar" }
95
77
 
96
78
  assert_equal '<img alt="" height="25" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=25" title="Gravatar" width="25" />',
97
- gravatar_tag('bella@gmail.com', :size => 25, :title => 'Gravatar')
79
+ gravatar_tag('bella@gmail.com', :size => 25, :html => { :title => 'Gravatar' })
98
80
  end
99
81
 
100
82
  should "allow :alt to be set globally" do
101
- Gravatarify::Helper.html_options[:alt] = "Gravatar"
83
+ Gravatarify.options[:html] = { :alt => "Gravatar" }
102
84
 
103
85
  assert_equal '<img alt="Gravatar" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
104
86
  gravatar_tag('bella@gmail.com')
105
87
  assert_equal '<img alt="Avatar" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d" width="80" />',
106
- gravatar_tag('bella@gmail.com', :filetype => false, :alt => 'Avatar')
88
+ gravatar_tag('bella@gmail.com', :filetype => false, :html => { :alt => 'Avatar' })
107
89
  end
108
90
  end
109
91
  end
@@ -4,21 +4,21 @@ require 'cgi'
4
4
  class GravatarifyRackVsCgiTest < Test::Unit::TestCase
5
5
  include Gravatarify::Base
6
6
 
7
+ # Remove Rack if defined
8
+ def setup
9
+ Object.send(:remove_const, :Rack) if defined?(Rack)
10
+ end
11
+
7
12
  # Reload Rack::Utils
8
13
  def teardown
9
14
  begin; require('rack/utils'); rescue LoadError; end
10
15
  end
11
16
 
12
- context "if Rack::Utils is not available, #gravatarify" do
13
- setup do
14
- # Remove Rack if defined
15
- Object.send(:remove_const, :Rack) if defined?(Rack)
16
- end
17
-
17
+ context "if Rack::Utils is not available, #gravatar_url" do
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 "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", build_gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
21
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
22
22
  end
23
23
  end
24
24
  end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class GravatarifyStylesTest < Test::Unit::TestCase
4
+ include Gravatarify::Helper
5
+
6
+ def setup
7
+ reset_gravatarify!
8
+ Gravatarify.styles.clear
9
+ end
10
+
11
+ context "Gravatarify#styles" do
12
+ should "store and retrieve styles" do
13
+ Gravatarify.styles[:mini] = { :size => 16 }
14
+ exp = { :size => 16 }
15
+ assert_equal exp, Gravatarify.styles[:mini]
16
+ end
17
+ end
18
+
19
+ context "Gravatarify::Base#gravatar_url" do
20
+ setup { Gravatarify.styles[:mini] = { :size => 16, :default => :wavatar } }
21
+
22
+ should "still work without any argument" do
23
+ assert_equal BELLA_AT_GMAIL_JPG, gravatar_url('bella@gmail.com')
24
+ end
25
+
26
+ should "respect styles" do
27
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=wavatar&s=16", gravatar_url('bella@gmail.com', :mini)
28
+ end
29
+
30
+ should "allow to override styles" do
31
+ assert_equal "#{BELLA_AT_GMAIL}?d=404&s=16", gravatar_url('bella@gmail.com', :mini, :filetype => false, :default => 404)
32
+ end
33
+
34
+ should "override default options" do
35
+ Gravatarify.options[:size] = 45
36
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?d=wavatar&s=16", gravatar_url('bella@gmail.com', :mini)
37
+ end
38
+
39
+ should "inherit default options" do
40
+ Gravatarify.options[:size] = 45
41
+ Gravatarify.options[:filetype] = 'png'
42
+ assert_equal "#{BELLA_AT_GMAIL}.png?d=wavatar&s=16", gravatar_url('bella@gmail.com', :mini)
43
+ end
44
+
45
+ should "work with unknown style" do
46
+ assert_equal "#{BELLA_AT_GMAIL_JPG}", gravatar_url('bella@gmail.com', :some_style)
47
+ end
48
+
49
+ should "work with unknown style and custom options" do
50
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?s=20", gravatar_url('bella@gmail.com', :some_style, :size => 20)
51
+ end
52
+ end
53
+
54
+ context "Gravatarify::Helper#gravatar_attrs" do
55
+ setup { Gravatarify.styles[:mini] = { :size => 16 } }
56
+
57
+ should "still work as-is without options" do
58
+ expected = { :src => BELLA_AT_GMAIL_JPG, :alt => '', :width => 80, :height => 80 }
59
+ assert_equal expected, gravatar_attrs('bella@gmail.com')
60
+ end
61
+
62
+ should "work with style :mini" do
63
+ expected = { :src => "#{BELLA_AT_GMAIL_JPG}?s=16", :alt => '', :width => 16, :height => 16 }
64
+ assert_equal expected, gravatar_attrs('bella@gmail.com', :mini)
65
+ end
66
+ end
67
+
68
+ context "Gravatarify::Helper#gravatar_tag" do
69
+ setup { Gravatarify.styles[:mini] = { :size => 16, :html => { :alt => "x" } } }
70
+
71
+ should "work with styles and be able to override options locally" do
72
+ assert_equal "<img alt=\"x\" height=\"16\" src=\"#{BELLA_AT_GMAIL_JPG}?r=x&amp;s=16\" width=\"16\" />", gravatar_tag('bella@gmail.com', :mini, :rating => :x)
73
+ end
74
+ end
75
+
76
+ context "Gravatarify#options and Gravatarify#styles" do
77
+ should "deep merge the :html attribute and inherit correctly" do
78
+ Gravatarify.options[:a] = "global"
79
+ Gravatarify.options[:b] = "global"
80
+ Gravatarify.options[:c] = "global"
81
+ Gravatarify.options[:html] = { :a => "global", :b => "global", :c => "global" }
82
+ Gravatarify.styles[:test] = { :b => "style", :c => "style", :html => { :b => "style", :c => "style" } }
83
+
84
+ expected = {
85
+ :alt => '', :width => 80, :height => 80,
86
+ :src => "#{BELLA_AT_GMAIL_JPG}?a=global&b=style&c=local",
87
+ :a => "global", :b => "style", :c => "local"
88
+ }
89
+ assert_equal expected, gravatar_attrs('bella@gmail.com', :test, :c => "local", :html => { :c => "local" })
90
+ end
91
+ end
92
+ end