gravatarify 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -9,7 +9,6 @@ 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>
13
12
  - **Gem**: <http://gemcutter.org/gems/gravatarify>
14
13
 
15
14
  **UPGRADE NOTES:** Version 2.x is a clean-up release which breaks backwards compatibility
data/VERSION.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  ---
2
2
  :major: 2
3
3
  :minor: 0
4
- :patch: 3
4
+ :build:
5
+ :patch: 4
@@ -1,15 +1,19 @@
1
1
  require 'digest/md5'
2
+ require 'cgi'
2
3
 
3
4
  module Gravatarify
4
- # Hash of 'ultra_long_option_name' => 'abbrevated option'
5
- # :nodoc:
6
- GRAVATAR_ABBREV_OPTIONS = { 'default' => 'd', 'rating' => 'r', 'size' => 's' }
5
+ # Hash of :ultra_long_option_name => :abbr_opt
6
+ GRAVATAR_ABBREV_OPTIONS = { 'default' => :d, :default => :d, 'rating' => :r, :rating => :r, 'size' => :s, :size => :s }
7
7
 
8
8
  class << self
9
- # Globally define options which are then merged on every call to
10
- # +gravatar_url+, this is useful to e.g. define the default image.
9
+
10
+ # Global options which are merged on every call to
11
+ # +gravatar_url+, this is useful to e.g. define a default image.
12
+ #
13
+ # When using Rails defining default options is best done in an
14
+ # initializer +config/initializers/gravatarify.rb+ (or similar).
11
15
  #
12
- # Setting global options should be done (for Rails apps) in an initializer:
16
+ # Usage examples:
13
17
  #
14
18
  # # set the default image using a Proc
15
19
  # Gravatarify.options[:default] = Proc.new { |*args| "http://example.com/avatar-#{args.first[:size] || 80}.jpg" }
@@ -115,11 +119,10 @@ module Gravatarify
115
119
  # Builds a query string from all passed in options.
116
120
  def build_gravatar_options(source, url_options = {})
117
121
  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
122
+ key = (GRAVATAR_ABBREV_OPTIONS[key] || key).to_sym # shorten key
123
+ unless key == :html
124
+ value = value.call(url_options, source) if key == :d and value.respond_to?(:call)
125
+ params << "#{key}=#{CGI.escape(value.to_s)}" if value
123
126
  end
124
127
  params
125
128
  end
@@ -1,6 +1,6 @@
1
1
  module Gravatarify::Helper
2
2
  include Gravatarify::Base
3
-
3
+
4
4
  # Helper method for HAML to return a neat hash to be used as attributes in an image tag.
5
5
  #
6
6
  # Now it's as simple as doing something like:
@@ -31,7 +31,8 @@ module Gravatarify::Helper
31
31
  # image tag.
32
32
  # @return [String] a complete and hopefully valid +img+ tag.
33
33
  def gravatar_tag(email, *params)
34
- html_attrs = gravatar_attrs(email, *params).map { |key,value| "#{key}=\"#{Gravatarify::Utils.escape_html(value)}\"" }.sort.join(" ")
35
- "<img #{html_attrs} />"
34
+ html_attrs = gravatar_attrs(email, *params).map { |key,value| "#{key}=\"#{CGI.escapeHTML(value.to_s)}\"" }.sort.join(" ")
35
+ html = "<img #{html_attrs} />"
36
+ Gravatarify::Utils.with_xss? ? html.html_safe! : html
36
37
  end
37
38
  end
@@ -1,24 +1,8 @@
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
-
1
+ module Gravatarify
2
+ # Set of common utility methods to e.g. deep merge options etc.
3
+ module Utils #:nodoc:
19
4
  # Merge supplied list of +params+ with the globally defined default options and
20
5
  # any params. Then merge remaining params as hash.
21
- #
22
6
  def self.merge_gravatar_options(*params)
23
7
  return (params[1] || {}) if params.first == false
24
8
  options = Gravatarify.options.dup
@@ -27,6 +11,7 @@ module Gravatarify
27
11
  options
28
12
  end
29
13
 
14
+ # Deeply merge the <tt>:html</tt> attribute.
30
15
  def self.deep_merge_html!(hash, to_merge)
31
16
  html = (hash[:html] || {}).merge(to_merge[:html] || {})
32
17
  hash.merge!(to_merge)
@@ -34,11 +19,13 @@ module Gravatarify
34
19
  end
35
20
 
36
21
  # Tries first to call +email+, then +mail+ then +to_s+ on supplied
37
- # object.
22
+ # object, also strips leading/trailing whitespace and downcases string
23
+ # (as specified by gravatar.com).
38
24
  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
25
+ (obj.respond_to?(:email) ? obj.send(:email) : (obj.respond_to?(:mail) ? obj.send(:mail) : obj)).to_s.strip.downcase
42
26
  end
27
+
28
+ # Returns +true+ when those XSS methods are available, else +false+ is returned.
29
+ def self.with_xss?; "".respond_to?(:html_safe!) end
43
30
  end
44
31
  end
@@ -0,0 +1,15 @@
1
+ require 'gravatarify'
2
+ require 'benchmark'
3
+
4
+ include Gravatarify::Helper
5
+
6
+ emails = ['foo@bar.com', 'foobar_didum_asdf@asdasd.com',
7
+ 'ASDASDSA@aasd_ASDSAd.com', ' sad@asdASdssasd .ch', 'Master <asdASD@asdasd.com >']
8
+ n = 10000
9
+ Benchmark.bm(23) do |bm|
10
+ bm.report("gravatar_url w/o args: ") { for i in 1..n do gravatar_url(emails[i % 5]) end }
11
+ bm.report("gravatar_url w/ args: ") { for i in 1..n do gravatar_url(emails[i % 5], :size => 30, :x => 'foo', 'other' => 'abcdef') end }
12
+ bm.report("gravatar_tag w/o args: ") { for i in 1..n do gravatar_tag(emails[i % 5]) end }
13
+ bm.report("gravatar_tag w/ args: ") { for i in 1..n do gravatar_tag(emails[i % 5], :size => 30, :x => 'foo', :html => { :class => 'abcdef'}) end }
14
+ end
15
+ puts " -> each measured #{n} times using for-loop"
@@ -37,8 +37,8 @@ class GravatarifyBaseTest < Test::Unit::TestCase
37
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?")
38
38
  end
39
39
 
40
- should "ensure that all options as well as keys are escaped correctly" do
41
- assert_equal "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
40
+ should "ensure that all options are escaped correctly" do
41
+ assert_equal "#{BELLA_AT_GMAIL_JPG}?unescaped=escaped%2Fme", gravatar_url('bella@gmail.com', 'unescaped' => 'escaped/me')
42
42
  end
43
43
 
44
44
  should "ignore false or nil options" do
@@ -99,11 +99,7 @@ class GravatarifyBaseTest < Test::Unit::TestCase
99
99
  mock(boy).email { "hans@gmail.com" }
100
100
  mock(boy).female? { false }
101
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
102
+ end
107
103
  end
108
104
 
109
105
  context "Gravatar hosts support" do
@@ -46,6 +46,11 @@ class GravatarifyHelpersTest < Test::Unit::TestCase
46
46
  assert_equal '<img alt="" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" title="&lt;&gt;" width="80" />',
47
47
  gravatar_tag('bella@gmail.com', :html => { :title => '<>' })
48
48
  end
49
+
50
+ should "be html_safe if rails 2.3.5" do
51
+ require 'active_support'
52
+ assert gravatar_tag('bella@gmail.com').html_safe?, "#html_safe? expected to return <true>"
53
+ end
49
54
  end
50
55
 
51
56
  context "#gravatar_tag when passed in an object" do
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: 2.0.3
4
+ version: 2.0.4
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-11-03 00:00:00 +01:00
12
+ date: 2010-01-08 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -36,10 +36,10 @@ files:
36
36
  - lib/gravatarify/helper.rb
37
37
  - lib/gravatarify/utils.rb
38
38
  - rails/init.rb
39
+ - test/benchmark/benchmark.rb
39
40
  - test/test_helper.rb
40
41
  - test/unit/gravatarify_base_test.rb
41
42
  - test/unit/gravatarify_helper_test.rb
42
- - test/unit/gravatarify_rack_vs_cgi_test.rb
43
43
  - test/unit/gravatarify_styles_test.rb
44
44
  - test/unit/gravatarify_subdomain_test.rb
45
45
  has_rdoc: true
@@ -71,9 +71,9 @@ signing_key:
71
71
  specification_version: 3
72
72
  summary: Awesome gravatar support for Ruby (and Rails).
73
73
  test_files:
74
+ - test/benchmark/benchmark.rb
74
75
  - test/test_helper.rb
75
76
  - test/unit/gravatarify_base_test.rb
76
77
  - test/unit/gravatarify_helper_test.rb
77
- - test/unit/gravatarify_rack_vs_cgi_test.rb
78
78
  - test/unit/gravatarify_styles_test.rb
79
79
  - test/unit/gravatarify_subdomain_test.rb
@@ -1,24 +0,0 @@
1
- require 'test_helper'
2
- require 'cgi'
3
-
4
- class GravatarifyRackVsCgiTest < Test::Unit::TestCase
5
- include Gravatarify::Base
6
-
7
- # Remove Rack if defined
8
- def setup
9
- Object.send(:remove_const, :Rack) if defined?(Rack)
10
- end
11
-
12
- # Reload Rack::Utils
13
- def teardown
14
- begin; require('rack/utils'); rescue LoadError; end
15
- end
16
-
17
- context "if Rack::Utils is not available, #gravatar_url" do
18
- should "fallback to CGI#escape" do
19
- assert !defined?(Rack::Utils), 'Rack::Utils should no longer be defined'
20
- assert defined?(CGI), "CGI should be defined"
21
- assert_equal "#{BELLA_AT_GMAIL_JPG}?escaped%2Fme=escaped%2Fme", gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
22
- end
23
- end
24
- end