gravatarify 0.6.0

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.
@@ -0,0 +1,3 @@
1
+ doc/
2
+ pkg/
3
+ .yardoc
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Lukas Westermann, Zurich (CHE)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,81 @@
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).
6
+
7
+ Best of it? It works with Rails, probably Merb and even plain old Ruby :)
8
+
9
+ ## Install
10
+
11
+ TODO: need to gemify it...
12
+
13
+ ## Using the view helpers
14
+
15
+ Probably one of the easiest ways to add support for gravatar images is with the included view helpers:
16
+
17
+ <%= gravatar_tag @user %> # assumes @user has email or mail field!
18
+
19
+ This builds a neat `<img/>`-tag, if you need to pass in stuff like the size etc. just:
20
+
21
+ <%= gravatar_tag @user, :size => 25, :rating => :x, :class => "gravatar" %>
22
+
23
+ This will display an "X" rated avatar which is 25x25 pixel in size and the image tag will have the class `"gravatar"`.
24
+ If more control is required, or just the URL, well then go ahead and use `gravatar_url` instead:
25
+
26
+ <%= image_tag gravatar_url(@user.author_email, :size => 16), :size => "16x16",
27
+ :alt => @user.name, :class => "avatar avatar-16"}/
28
+
29
+ Using rails `image_tag` to create an `<img/>`-tag with `gravatar_url`. It's important to know that
30
+ also an object can be passed to `gravatar_url`, if it responds to either `email` or `mail`. If not (like
31
+ in the example above), the email address must be passed in.
32
+
33
+ ## Using the model helpers
34
+
35
+ Another way (especially cool) for models is to do:
36
+
37
+ class User < ActiveRecord::Base
38
+ gravatarify
39
+ end
40
+
41
+ Thats it! Well, at least if the `User` model responds to `email` or `mail`. Then in the views all left to do is:
42
+
43
+ <%= image_tag @user.gravatar_url %>
44
+
45
+ Neat, isn't it? Of course passing options works just like with the view helpers:
46
+
47
+ <%= image_tag @user.gravatar_url(:size => 16, :rating => :r) %>
48
+
49
+ Defaults can even be passed to the `gravatarify` call, so no need to repeat them on every `gravatar_url` call.
50
+
51
+ gravatarify :employee_mail, :size => 16, :rating => :r
52
+
53
+ All gravatars will now come from the `employee_mail` field, not the default `email` or `mail` field and be in 16x16px in size
54
+ and have a rating of 'r'. Of course these can be overriden in calls to `gravatar_url` like before. Pretty cool is also the
55
+ fact that an object can be passed directly to `gravatar_tag` if it responds to `gravatar_url`, like:
56
+
57
+ # model:
58
+ class User < ActiveRecord::Base
59
+ gravatarify :size => 16, :secure => true
60
+ end
61
+
62
+ # view:
63
+ <%= gravatar_tag @user %> # -> <img ... width="16" src="https://secure.gravatar..." height="16" />
64
+
65
+ The `gravatar_tag` looks if the object responds to `gravatar_url` and if so, just passes the options to it,
66
+ it works also with plain old ruby objects, of course :)
67
+
68
+ ### PORO - plain old ruby objects (yeah, POJO sounds smoother :D)
69
+
70
+ Not using Rails, ActiveRecord or DataMapper? It's as easy as including `Gravatarify::ObjectSupport` to your
71
+ class:
72
+
73
+ require 'gravatarify'
74
+ class PoroUser
75
+ include Gravatarify::ObjectSupport
76
+ gravatarify
77
+ end
78
+
79
+ Tadaaa! Works exactly like the model helpers, so it's now possible to call `gravatar_url` on instances
80
+ of `PoroUser`.
81
+
@@ -0,0 +1,77 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'yard'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the gravatarify plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for gravatarify. (requires yard)'
17
+ YARD::Rake::YardocTask.new(:doc) do |t|
18
+ t.files = ['lib/**/*.rb']
19
+ t.options = [
20
+ "--readme", "README.md",
21
+ "--title", "Gravatarify API Documentation"
22
+ ]
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "gravatarify"
29
+ gemspec.summary = "Gravatar URLs for your ruby"
30
+ gemspec.email = "lukas.westermann@gmail.com"
31
+ gemspec.homepage = "http://github.com/lwe/gravatarify"
32
+ gemspec.authors = ["Lukas Westermann"]
33
+ end
34
+ Jeweler::GemcutterTasks.new
35
+ rescue LoadError
36
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
37
+ end
38
+
39
+ desc 'Clean all generated files (.yardoc and doc/*)'
40
+ task :clean do |t|
41
+ FileUtils.rm_rf "doc"
42
+ FileUtils.rm_rf "pkg"
43
+ FileUtils.rm_rf ".yardoc"
44
+ end
45
+
46
+ namespace :metrics do
47
+ desc 'Report all metrics, i.e. stats and code coverage.'
48
+ task :all => [:stats, :coverage]
49
+
50
+ desc 'Report code statistics for library and tests to shell.'
51
+ task :stats do |t|
52
+ require 'code_statistics'
53
+ dirs = {
54
+ 'Libraries' => 'lib',
55
+ 'Unit tests' => 'test'
56
+ }.map { |name,dir| [name, File.join(File.dirname(__FILE__), dir)] }
57
+ CodeStatistics.new(*dirs).to_s
58
+ end
59
+
60
+ desc 'Report code coverage to HTML (doc/coverage) and shell (requires rcov).'
61
+ task :coverage do |t|
62
+ rm_f "doc/coverage"
63
+ mkdir_p "doc/coverage"
64
+ rcov = %(rcov -Ilib:test --exclude '\/gems\/' -o doc/coverage -T test/*_test.rb )
65
+ system rcov
66
+ end
67
+
68
+ desc 'Report the fishy smell of bad code (requires reek)'
69
+ task :smelly do |t|
70
+ puts
71
+ puts "* * * NOTE: reek currently reports several false positives,"
72
+ puts " eventhough it's probably good to check once in a while!"
73
+ puts
74
+ reek = %(reek -s lib)
75
+ system reek
76
+ end
77
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 0
3
+ :major: 0
4
+ :minor: 6
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require File.join(File.dirname(__FILE__), 'lib', 'gravatarify')
@@ -0,0 +1,15 @@
1
+ # Loads all required submodules
2
+
3
+ # Base -> provides the basic gravatar_url method, can also be used for
4
+ # custom implementations, just include Gravatarify::Base.
5
+ require 'gravatarify/base'
6
+ require 'gravatarify/view_helper'
7
+ require 'gravatarify/object_support'
8
+
9
+ # include helper for rails
10
+ ActionView::Base.send(:include, Gravatarify::ViewHelper) if defined?(ActionView)
11
+
12
+ # setup for AR und DataMapper, note: DataMapper yet untested :) but I suppose it works, because
13
+ # it works as expected on plain old ruby objects!
14
+ ActiveRecord::Base.send(:include, Gravatarify::ObjectSupport) if defined?(ActiveRecord)
15
+ DataMapper::Model.append_inclusions(Gravatarify::ObjectSupport) if defined?(DataMapper)
@@ -0,0 +1,90 @@
1
+ require 'digest/md5'
2
+ require 'cgi'
3
+
4
+ module Gravatarify
5
+
6
+ # Hosts used for balancing
7
+ GRAVATAR_HOSTS = %w{ 0 1 2 www }
8
+
9
+ # If no size is specified, gravatar.com returns 80x80px images
10
+ GRAVATAR_DEFAULT_SIZE = 80
11
+
12
+ # Default filetype is JPG
13
+ GRAVATAR_DEFAULT_FILETYPE = 'jpg'
14
+
15
+ # List of known and valid gravatar options (includes shortened options).
16
+ GRAVATAR_OPTIONS = [ :default, :d, :rating, :r, :size, :s, :secure, :filetype ]
17
+
18
+ # Hash of :ultra_long_option_name => 'abbrevated option'
19
+ GRAVATAR_ABBREV_OPTIONS = { :default => 'd', :rating => 'r', :size => 's' }
20
+
21
+ # Options which can be globally overriden by the application
22
+ def self.options; @options ||= {} end
23
+
24
+ # Provides core support to build gravatar urls based on supplied e-mail strings.
25
+ module Base
26
+
27
+ # Method which builds a gravatar url based on a supplied email and options as
28
+ # defined by gravatar.com (http://en.gravatar.com/site/implement/url).
29
+ #
30
+ # build_gravatar_url('peter.gibbons@initech.com', :size => 16) # => "http://0.gravatar.com/avatar/cb7865556d41a3d800ae7dbb31d51d54.jpg?s=16"
31
+ #
32
+ # It supports multiple gravatar hosts (based on email hash), i.e. depending
33
+ # 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>
34
+ # is used.
35
+ #
36
+ # If supplied +email+ responds to either a method named +email+ or +mail+, the value of that method
37
+ # is used instead to build the gravatar hash. Very useful to just pass in ActiveRecord object for instance:
38
+ #
39
+ # @user = User.find_by_email("samir@initech.com")
40
+ # build_gravatar_url(@user) # => "http://2.gravatar.com/avatar/58cf31c817d76605d5180ce1a550d0d0.jpg"
41
+ # build_gravatar_url(@user.email) # same as above!
42
+ #
43
+ # Among all options as defined by gravatar.com's specification, there also exist some special options:
44
+ #
45
+ # build_gravatar_url(@user, :secure => true) # => https://secure.gravatar.com/ava....
46
+ #
47
+ # Useful when working on SSL enabled sites. Of course often used options should be set through
48
+ # +Gravatarify.options+.
49
+ #
50
+ # @param [String, #email, #mail] email a string representing an email, or object which responds to +email+ or +mail+
51
+ # @param [Hash] url_options customize generated gravatar.com url
52
+ # @option url_options [String, Proc] :default (nil) URL of an image to use as default when no gravatar exists. Gravatar.com
53
+ # also accepts special values like +identicon+, +monsterid+ or +wavater+ which just displays
54
+ # a generic icon based on the hash or <tt>404</tt> which return a HTTP Status 404.
55
+ # @option url_options [String, Symbol] :rating (:g) Specify the rating, gravatar.com supports <tt>:g</tt>, <tt>:pg</tt>,
56
+ # <tt>:r</tt> or <tt>:x</tt>, they correspond to movie ratings :)
57
+ # @option url_options [Integer] :size (80) The size of the (square) image.
58
+ # @option url_options [Boolean, Proc] :secure (false) If set to +true+, then uses the secure gravatar.com URL. If a Proc is
59
+ # supplied it's evaluated, the Proc should evaluate to +true+ or +false+.
60
+ # @option url_options [String, Symbol] :filetype (:jpg) Gravatar.com supports only <tt>:gif</tt>, <tt>:jpg</tt> and <tt>:png</tt>
61
+ # @return [String] In any case (even if supplied +email+ is +nil+) returns a fully qualified gravatar.com URL.
62
+ # The returned string is not yet HTML escaped, *but* all +url_options+ have been URI escaped.
63
+ def build_gravatar_url(email, url_options = {})
64
+ # FIXME: add symbolize_keys again, maybe just write custom method, so we do not depend on ActiveSupport magic...
65
+ url_options = Gravatarify.options.merge(url_options)
66
+ email_hash = Digest::MD5.hexdigest(Base.get_smart_email_from(email).strip.downcase)
67
+ build_gravatar_host(email_hash, url_options.delete(:secure)) << "/avatar/#{email_hash}.#{url_options.delete(:filetype) || GRAVATAR_DEFAULT_FILETYPE}#{build_gravatar_options(url_options)}"
68
+ end
69
+
70
+ private
71
+ def build_gravatar_host(str_hash, secure = false)
72
+ secure = secure.call(respond_to?(:request) ? request : nil) if secure.respond_to?(:call)
73
+ secure ? "https://secure.gravatar.com" : "http://#{GRAVATAR_HOSTS[str_hash.hash % GRAVATAR_HOSTS.size] || 'www'}.gravatar.com"
74
+ end
75
+
76
+ def build_gravatar_options(url_options = {})
77
+ params = []
78
+ url_options.each_pair do |key, value|
79
+ key = GRAVATAR_ABBREV_OPTIONS[key] if GRAVATAR_ABBREV_OPTIONS.include?(key) # shorten key!
80
+ value = value.call(url_options) if key.to_s == 'd' and value.respond_to?(:call)
81
+ params << "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" if value
82
+ end
83
+ "?#{params.sort * '&'}" unless params.empty?
84
+ end
85
+
86
+ def self.get_smart_email_from(obj)
87
+ (obj.respond_to?(:email) ? obj.email : (obj.respond_to?(:mail) ? obj.mail : obj)).to_s
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,58 @@
1
+ # Enables +gravatarify+ support in any plain old ruby object, ActiveRecord, DataMapper or wherever you like :)
2
+ #
3
+ # Provides the {ClassMethods#gravatarify} method to handle the creation of
4
+ # a +gravatar_url+ method for a ruby object.
5
+ #
6
+ # An ActiveRecord example:
7
+ #
8
+ # class User < ActiveRecord::Base
9
+ # gravatarify
10
+ # end
11
+ # @user.gravatar_url # that's it!
12
+ #
13
+ # A DataMapper example:
14
+ #
15
+ # class User
16
+ # include DataMapper::Resource
17
+ # property ...
18
+ # property :author_email, String
19
+ # end
20
+ # @user.gravatar_url :author_email # that's it, using the specified field!
21
+ #
22
+ # And finally, using a plain old ruby object:
23
+ #
24
+ # class SimpleUser
25
+ # include Gravatarify::ObjectSupport
26
+ # attr_accessor :email
27
+ # gravatarify
28
+ # end
29
+ # @user.gravatar_url # that's it!!!
30
+ #
31
+ # If more fine grained controller is required, feel free to use {Gravatarify::Base#build_gravatar_url}
32
+ # directly.
33
+ module Gravatarify::ObjectSupport
34
+ include Gravatarify::Base
35
+
36
+ def self.included(base) #:nodoc:
37
+ base.send :extend, ClassMethods
38
+ end
39
+
40
+ module ClassMethods
41
+ def gravatarify(*args)
42
+ options = args.last.is_a?(Hash) ? args.pop : {}
43
+ source = args.shift
44
+ define_method(:gravatar_url) do |*params|
45
+ source = :email if !source and respond_to?(:email)
46
+ source = :mail if !source and respond_to?(:mail)
47
+ build_gravatar_url send(source || :email), options.merge(params.first || {})
48
+ end
49
+ # has more
50
+ args.each do |src|
51
+ method = "#{src}_gravatar_url".sub(/_e?mail/, '')
52
+ define_method(method) do |*params|
53
+ build_gravatar_url send(src), options.merge(params.first || {})
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,22 @@
1
+ module Gravatarify::ViewHelper
2
+ include Gravatarify::Base
3
+
4
+ # Ensure proper gravatar_url method is available!
5
+ alias_method :gravatar_url, :build_gravatar_url
6
+
7
+ # Create <img .../> tag by passing +email+ to +gravatar_url+, is based
8
+ # on rails +image_tag+ helper method.
9
+ #
10
+ # <%= gravatar_tag(current_user.email, :size => 20) %> # -> <img alt="... height="20" src="http://grava... width="20" />
11
+ # <%= gravatar_tag('foo@bar.com', :class => "gravatar") # -> <img alt="foo@bar.com" class="gravatar" height="80" ... width="80" />
12
+ #
13
+ # Note: this method tries to be very clever about which options need to be passed to
14
+ # +gravatar_url+ and which to +image_tag+, so using this method it's not possible to
15
+ # send arbitary attributes to +gravatar_url+ and have them included in the url.
16
+ def gravatar_tag(email, options = {})
17
+ url_options = options.symbolize_keys.reject { |key,value| !Gravatarify::GRAVATAR_OPTIONS.include?(key) }
18
+ options[:alt] ||= Gravatarify::Base.get_smart_email_from(email) # use email as :alt attribute
19
+ options[:width] = options[:height] = (url_options[:size] || Gravatarify::GRAVATAR_DEFAULT_SIZE) # customize size
20
+ image_tag(email.respond_to?(:gravatar_url) ? email.gravatar_url(url_options) : gravatar_url(email, url_options), options)
21
+ end
22
+ end
@@ -0,0 +1,136 @@
1
+ require 'test_helper'
2
+ require 'gravatarify/base'
3
+
4
+ class MockView
5
+ include Gravatarify::Base
6
+ end
7
+
8
+ class GravatarifyBaseTest < Test::Unit::TestCase
9
+ include Gravatarify::Base
10
+
11
+ def setup
12
+ # just ensure that no global options are defined when starting next test
13
+ Gravatarify.options.clear
14
+ end
15
+
16
+ context "#build_gravatar_url, but without any options yet" do
17
+ should "generate correct url for hash without options" do
18
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url('bella@gmail.com')
19
+ end
20
+
21
+ should "trim and lowercase email address (as according to gravatar docs)" do
22
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url("\tbella@gmail.com \n\t")
23
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url("BELLA@gmail.COM")
24
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(" BELLA@GMAIL.com")
25
+ end
26
+
27
+ should "handle a nil email as if it were an empty string" do
28
+ assert_equal "http://1.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg", build_gravatar_url(nil)
29
+ assert_equal "http://1.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e.jpg", build_gravatar_url('')
30
+ end
31
+ end
32
+
33
+ context "#build_gravatar_url, with options" do
34
+ should "add well known options like size, rating or default and always in alphabetical order" do
35
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16", build_gravatar_url('bella@gmail.com', :size => 16)
36
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Ftest.jpg&s=20",
37
+ build_gravatar_url('bella@gmail.com', :size => 20, :default => 'http://example.com/test.jpg')
38
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?other=escaped%26yes%3F&r=x&s=30",
39
+ build_gravatar_url('bella@gmail.com', :size => 30, :rating => :x, :other => "escaped&yes?")
40
+ end
41
+
42
+ should "ensure that all options as well as keys are escaped correctly" do
43
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?escaped%2Fme=escaped%2Fme",
44
+ build_gravatar_url('bella@gmail.com', 'escaped/me' => 'escaped/me')
45
+ end
46
+
47
+ should "ignore false or nil options" do
48
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=24",
49
+ build_gravatar_url('bella@gmail.com', :s => 24, :invalid => false, :other => nil)
50
+ end
51
+
52
+ should "allow different :filetype to be set, like 'gif' or 'png'" do
53
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.gif", build_gravatar_url('bella@gmail.com', :filetype => :gif)
54
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.png", build_gravatar_url('bella@gmail.com', :filetype => :png)
55
+ end
56
+
57
+ should "handle Procs as :default, to easily generate default urls based on supplied :size" do
58
+ default = Proc.new { |o| "http://example.com/gravatar#{o[:size] ? '-' + o[:size].to_s : ''}.jpg" }
59
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar.jpg",
60
+ build_gravatar_url('bella@gmail.com', :default => default)
61
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar-25.jpg&s=25",
62
+ build_gravatar_url('bella@gmail.com', :size => 25, :d => default)
63
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?d=http%3A%2F%2Fexample.com%2Fgravatar-20.jpg&s=20",
64
+ build_gravatar_url('bella@gmail.com', :size => 20, 'd' => default)
65
+ end
66
+ end
67
+
68
+ context "#build_gravatar_url when passed in an object" do
69
+ should "look for :email method and use it to generate build_gravatar_url from" do
70
+ obj = Object.new
71
+ mock(obj).email { "bella@gmail.com" }
72
+
73
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(obj)
74
+ end
75
+
76
+ should "look for :mail of field :email does not exist" do
77
+ obj = Object.new
78
+ mock(obj).mail { "bella@gmail.com" }
79
+
80
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(obj)
81
+ end
82
+
83
+ should "finally just use to_s... if neither :email nor :mail exists" do
84
+ obj = Object.new
85
+ mock(obj).to_s { "bella@gmail.com" }
86
+
87
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url(obj)
88
+ end
89
+ end
90
+
91
+ context "Gravatar hosts support" do
92
+ should "switch to different hosts based on generated email hash, yet always the same for consecutive calls with the same email!" do
93
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url('bella@gmail.com')
94
+ assert_equal build_gravatar_url('bella@gmail.com'), build_gravatar_url('bella@gmail.com')
95
+ assert_equal "http://1.gravatar.com/avatar/41d86cad3dd465d6913d5a3232744441.jpg", build_gravatar_url('bella@bella.com')
96
+ assert_equal "http://2.gravatar.com/avatar/8f3af64e9c215d158b062a7b154e071e.jpg", build_gravatar_url('bella@hotmail.com')
97
+ assert_equal "http://www.gravatar.com/avatar/d2279c22a33da2cb57defd21c33c8ec5.jpg", build_gravatar_url('bella@yahoo.de')
98
+ end
99
+
100
+ should "switch to https://secure.gravatar.com if :secure => true is supplied" do
101
+ assert_equal "https://secure.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", build_gravatar_url('bella@gmail.com', :secure => true)
102
+ assert_equal "https://secure.gravatar.com/avatar/41d86cad3dd465d6913d5a3232744441.jpg", build_gravatar_url('bella@bella.com', :secure => true)
103
+ assert_equal "https://secure.gravatar.com/avatar/d2279c22a33da2cb57defd21c33c8ec5.jpg", build_gravatar_url('bella@yahoo.de', :secure => true)
104
+ end
105
+
106
+ should "allow Procs for :secure option, enables pretty cool stuff for stuff like request.ssl?" do
107
+ Gravatarify.options[:secure] = Proc.new { |request| request.respond_to?(:ssl?) ? request.ssl? : false }
108
+
109
+ mock_ssl = MockView.new
110
+ mock(mock_ssl).request.stub!.ssl? { true }
111
+ assert_equal "https://secure.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", mock_ssl.build_gravatar_url('bella@gmail.com')
112
+
113
+ mock_no_ssl = MockView.new
114
+ mock(mock_no_ssl).request.stub!.ssl? { false }
115
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg", mock_no_ssl.build_gravatar_url('bella@gmail.com')
116
+ end
117
+ end
118
+
119
+ context "Gravatarify#options" do
120
+ setup do
121
+ Gravatarify.options[:anything] = "test"
122
+ Gravatarify.options[:filetype] = "png"
123
+ Gravatarify.options[:default] = "http://example.com/gravatar.jpg"
124
+ end
125
+
126
+ should "ensure that default options are always added" do
127
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.png?anything=test&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg",
128
+ build_gravatar_url('bella@gmail.com')
129
+ end
130
+
131
+ should "ensure that default options can be overriden by passing options into build_gravatar_url call" do
132
+ assert_equal "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.gif?anything=else&d=http%3A%2F%2Fexample.com%2Fgravatar.jpg",
133
+ build_gravatar_url('bella@gmail.com', :anything => "else", :filetype => :gif)
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+ begin; require 'activerecord'; rescue LoadError; end
3
+ begin; require 'dm-core'; rescue LoadError; end
4
+ require 'gravatarify'
5
+
6
+ class GravatarifyIntegrationTest < Test::Unit::TestCase
7
+ def setup; Gravatarify.options.clear end
8
+
9
+ context "ActiveRecord::Base" do
10
+ if defined?(ActiveRecord)
11
+ should "include Gravatarify::ObjectSupport" do
12
+ assert ActiveRecord::Base.included_modules.include?(Gravatarify::ObjectSupport)
13
+ end
14
+
15
+ should "respond to #gravatarify" do
16
+ assert_respond_to ActiveRecord::Base, :gravatarify
17
+ end
18
+ else
19
+ context "tests" do
20
+ should "be run (but looks like ActiveRecord is not available)" do
21
+ flunk "ActiveRecord not available -> thus tests are incomplete (error can be ignored though!)"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ context "DataMapper model (User)" do
28
+ if defined?(DataMapper)
29
+ setup do
30
+ class User
31
+ include DataMapper::Resource
32
+ property :id, Serial
33
+ property :name, String
34
+ property :email, String
35
+ property :author_email, String
36
+
37
+ gravatarify
38
+ end
39
+ end
40
+
41
+ should "include Gravatarify::ObjectSupport" do
42
+ assert User.included_modules.include?(Gravatarify::ObjectSupport)
43
+ end
44
+
45
+ should "respond to #gravatarify" do
46
+ assert_respond_to User, :gravatarify
47
+ end
48
+
49
+ context "as instance" do
50
+ should "be able to build correct gravatar_url's!" do
51
+ u = User.new(:email => "peter.gibbons@initech.com")
52
+ assert_equal "http://0.gravatar.com/avatar/cb7865556d41a3d800ae7dbb31d51d54.jpg", u.gravatar_url
53
+ end
54
+ end
55
+ else
56
+ context "tests" do
57
+ should "be run (but looks like DataMapper is not available)" do
58
+ flunk "DataMapper not available -> thus tests are incomplete (error can be ignored though!)"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,99 @@
1
+ require 'test_helper'
2
+ require 'gravatarify/base'
3
+ require 'gravatarify/object_support'
4
+
5
+ class GravatarifyObjectSupportTest < Test::Unit::TestCase
6
+ def setup; Gravatarify.options.clear end
7
+
8
+ context "#gravatarify" do
9
+ should "add support for #gravatar_url to POROs (plain old ruby objects, yeah POJO sounds better!)" do
10
+ poro = Class.new do
11
+ include Gravatarify::ObjectSupport
12
+ gravatarify
13
+ def email; "mike@shiva.ch" end
14
+ end
15
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg", poro.new.gravatar_url
16
+ assert_equal "https://secure.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg", poro.new.gravatar_url(:secure => true)
17
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.gif?r=x&s=16", poro.new.gravatar_url(:filetype => :gif, :rating => :x, :size => 16)
18
+ end
19
+
20
+ should "allow some default options to be passed, which can be overriden locally, though" do
21
+ poro = Class.new do
22
+ include Gravatarify::ObjectSupport
23
+ gravatarify :secure => true, :filetype => :gif
24
+ def email; "mike@shiva.ch" end
25
+ end
26
+ assert_equal "https://secure.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.gif", poro.new.gravatar_url
27
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.gif", poro.new.gravatar_url(:secure => false)
28
+ assert_equal "https://secure.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg?r=x&s=16", poro.new.gravatar_url(:filetype => :jpg, :rating => :x, :size => 16)
29
+ end
30
+
31
+ should "still respect options set by Gravatarify#options" do
32
+ Gravatarify.options[:size] = 20
33
+ poro = Class.new do
34
+ include Gravatarify::ObjectSupport
35
+ gravatarify
36
+ def email; "mike@shiva.ch" end
37
+ end
38
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg?s=20", poro.new.gravatar_url
39
+ end
40
+
41
+ should "be able to override options set by Gravatarify#options" do
42
+ Gravatarify.options[:size] = 20
43
+ poro = Class.new do
44
+ include Gravatarify::ObjectSupport
45
+ gravatarify :size => 25
46
+ def email; "mike@shiva.ch" end
47
+ end
48
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg?s=25", poro.new.gravatar_url
49
+ end
50
+ end
51
+
52
+ context "#gravatarify with custom fields" do
53
+ should "detect and use the 'email' field automatically" do
54
+ poro = Class.new do
55
+ include Gravatarify::ObjectSupport
56
+ gravatarify
57
+ def email; "mike@shiva.ch" end
58
+ end
59
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg", poro.new.gravatar_url
60
+ end
61
+
62
+ should "fallback to 'mail' if 'email' is not defined" do
63
+ poro = Class.new do
64
+ include Gravatarify::ObjectSupport
65
+ gravatarify
66
+ def mail; "mike@shiva.ch" end
67
+ end
68
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg", poro.new.gravatar_url
69
+ end
70
+
71
+ should "raise NoMethodError if object does not respond to source" do
72
+ poro = Class.new do
73
+ include Gravatarify::ObjectSupport
74
+ gravatarify
75
+ end
76
+ assert_raise(NoMethodError) { poro.new.gravatar_url }
77
+ end
78
+
79
+ should "allow to set custom source, like author_email" do
80
+ poro = Class.new do
81
+ include Gravatarify::ObjectSupport
82
+ gravatarify :author_email
83
+ def author_email; "mike@shiva.ch" end
84
+ end
85
+ assert_equal "http://0.gravatar.com/avatar/1b2818d77eadd6c9dbbe7f3beb1492c3.jpg", poro.new.gravatar_url
86
+ end
87
+
88
+ should "allow multiple sources to be defined, yet still handle options!" do
89
+ poro = Class.new do
90
+ include Gravatarify::ObjectSupport
91
+ gravatarify :email, :employee_email, :default => Proc.new { |o| "http://initech.com/avatar-#{o[:size] || 80}.jpg" }
92
+ def email; "info@initech.com" end
93
+ def employee_email; "peter.gibbons@initech.com" end
94
+ end
95
+ assert_equal "http://2.gravatar.com/avatar/4979dd9653e759c78a81d4997f56bae2.jpg?d=http%3A%2F%2Finitech.com%2Favatar-20.jpg&s=20", poro.new.gravatar_url(:size => 20)
96
+ assert_equal "http://0.gravatar.com/avatar/cb7865556d41a3d800ae7dbb31d51d54.jpg?d=http%3A%2F%2Finitech.com%2Favatar-25.jpg&s=25", poro.new.employee_gravatar_url(:size => 25)
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+ require 'active_support'
3
+ require 'action_view/helpers'
4
+ require 'gravatarify/view_helper'
5
+
6
+ class GravatarifyViewHelperTest < Test::Unit::TestCase
7
+ include ActionView::Helpers
8
+ include Gravatarify::ViewHelper
9
+
10
+ def setup
11
+ # just ensure that no global options are defined when starting next test
12
+ Gravatarify.options.clear
13
+ end
14
+
15
+ context "#gravatar_tag helper" do
16
+ should "create <img/> tag with correct gravatar urls" do
17
+ assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
18
+ gravatar_tag('bella@gmail.com')
19
+ end
20
+
21
+ should "create <img/> tags and handle all options correctly, other options should be passed to Rails' image_tag" do
22
+ assert_equal '<img alt="bella@gmail.com" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
23
+ gravatar_tag('bella@gmail.com', :size => 16)
24
+ assert_equal '<img alt="bella@gmail.com" class="gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
25
+ gravatar_tag('bella@gmail.com', :class => "gravatar", :size => 16)
26
+ end
27
+ end
28
+
29
+ context "#gravatar_tag when passed in an object" do
30
+ should "create <img/>-tag based on :email field" do
31
+ obj = Object.new
32
+ mock(obj).email.times(2) { "bella@gmail.com" }
33
+
34
+ assert_equal '<img alt="bella@gmail.com" height="80" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg" width="80" />',
35
+ gravatar_tag(obj)
36
+ end
37
+
38
+ should "create <img/>-tag based on gravatar_url from object if object responds to gravatar_url" do
39
+ obj = Object.new
40
+ mock(obj).gravatar_url({ :size => 16 }) { "http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" }
41
+
42
+ assert_equal '<img alt="Gravatar" height="16" src="http://0.gravatar.com/avatar/1cacf1bc403efca2e7a58bcfa9574e4d.jpg?s=16" width="16" />',
43
+ gravatar_tag(obj, :size => 16, :alt => "Gravatar")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'rr'
5
+
6
+ Test::Unit::TestCase.send :include, RR::Adapters::TestUnit
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gravatarify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Lukas Westermann
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-29 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: lukas.westermann@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.md
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - init.rb
32
+ - lib/gravatarify.rb
33
+ - lib/gravatarify/base.rb
34
+ - lib/gravatarify/object_support.rb
35
+ - lib/gravatarify/view_helper.rb
36
+ - test/gravatarify_base_test.rb
37
+ - test/gravatarify_integration_test.rb
38
+ - test/gravatarify_object_support_test.rb
39
+ - test/gravatarify_view_helper_test.rb
40
+ - test/test_helper.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/lwe/gravatarify
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Gravatar URLs for your ruby
69
+ test_files:
70
+ - test/gravatarify_base_test.rb
71
+ - test/gravatarify_integration_test.rb
72
+ - test/gravatarify_object_support_test.rb
73
+ - test/gravatarify_view_helper_test.rb
74
+ - test/test_helper.rb