Chris@0
|
1 require 'digest/md5'
|
Chris@0
|
2 require 'cgi'
|
Chris@0
|
3
|
Chris@0
|
4 module GravatarHelper
|
Chris@0
|
5
|
Chris@0
|
6 # These are the options that control the default behavior of the public
|
Chris@0
|
7 # methods. They can be overridden during the actual call to the helper,
|
Chris@0
|
8 # or you can set them in your environment.rb as such:
|
Chris@0
|
9 #
|
Chris@0
|
10 # # Allow racier gravatars
|
Chris@0
|
11 # GravatarHelper::DEFAULT_OPTIONS[:rating] = 'R'
|
Chris@0
|
12 #
|
Chris@0
|
13 DEFAULT_OPTIONS = {
|
Chris@0
|
14 # The URL of a default image to display if the given email address does
|
Chris@0
|
15 # not have a gravatar.
|
Chris@0
|
16 :default => nil,
|
Chris@0
|
17
|
Chris@0
|
18 # The default size in pixels for the gravatar image (they're square).
|
Chris@0
|
19 :size => 50,
|
Chris@0
|
20
|
Chris@0
|
21 # The maximum allowed MPAA rating for gravatars. This allows you to
|
Chris@0
|
22 # exclude gravatars that may be out of character for your site.
|
Chris@0
|
23 :rating => 'PG',
|
Chris@0
|
24
|
Chris@0
|
25 # The alt text to use in the img tag for the gravatar. Since it's a
|
Chris@0
|
26 # decorational picture, the alt text should be empty according to the
|
Chris@0
|
27 # XHTML specs.
|
Chris@0
|
28 :alt => '',
|
chris@22
|
29
|
chris@22
|
30 # The title text to use for the img tag for the gravatar.
|
chris@22
|
31 :title => '',
|
Chris@0
|
32
|
Chris@0
|
33 # The class to assign to the img tag for the gravatar.
|
Chris@0
|
34 :class => 'gravatar',
|
Chris@0
|
35
|
Chris@0
|
36 # Whether or not to display the gravatars using HTTPS instead of HTTP
|
Chris@0
|
37 :ssl => false,
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 # The methods that will be made available to your views.
|
Chris@0
|
41 module PublicMethods
|
Chris@0
|
42
|
Chris@0
|
43 # Return the HTML img tag for the given user's gravatar. Presumes that
|
Chris@0
|
44 # the given user object will respond_to "email", and return the user's
|
Chris@0
|
45 # email address.
|
Chris@0
|
46 def gravatar_for(user, options={})
|
Chris@0
|
47 gravatar(user.email, options)
|
Chris@0
|
48 end
|
Chris@0
|
49
|
Chris@0
|
50 # Return the HTML img tag for the given email address's gravatar.
|
Chris@0
|
51 def gravatar(email, options={})
|
Chris@0
|
52 src = h(gravatar_url(email, options))
|
Chris@0
|
53 options = DEFAULT_OPTIONS.merge(options)
|
chris@22
|
54 [:class, :alt, :size, :title].each { |opt| options[opt] = h(options[opt]) }
|
chris@22
|
55 "<img class=\"#{options[:class]}\" alt=\"#{options[:alt]}\" title=\"#{options[:title]}\" width=\"#{options[:size]}\" height=\"#{options[:size]}\" src=\"#{src}\" />"
|
Chris@0
|
56 end
|
Chris@0
|
57
|
Chris@0
|
58 # Returns the base Gravatar URL for the given email hash. If ssl evaluates to true,
|
Chris@0
|
59 # a secure URL will be used instead. This is required when the gravatar is to be
|
Chris@0
|
60 # displayed on a HTTPS site.
|
Chris@0
|
61 def gravatar_api_url(hash, ssl=false)
|
Chris@0
|
62 if ssl
|
Chris@0
|
63 "https://secure.gravatar.com/avatar/#{hash}"
|
Chris@0
|
64 else
|
Chris@0
|
65 "http://www.gravatar.com/avatar/#{hash}"
|
Chris@0
|
66 end
|
Chris@0
|
67 end
|
Chris@0
|
68
|
Chris@0
|
69 # Return the gravatar URL for the given email address.
|
Chris@0
|
70 def gravatar_url(email, options={})
|
Chris@0
|
71 email_hash = Digest::MD5.hexdigest(email)
|
Chris@0
|
72 options = DEFAULT_OPTIONS.merge(options)
|
Chris@0
|
73 options[:default] = CGI::escape(options[:default]) unless options[:default].nil?
|
chris@37
|
74 gravatar_api_url(email_hash, options.delete(:ssl)).tap do |url|
|
Chris@0
|
75 opts = []
|
Chris@0
|
76 [:rating, :size, :default].each do |opt|
|
Chris@0
|
77 unless options[opt].nil?
|
Chris@0
|
78 value = h(options[opt])
|
Chris@0
|
79 opts << [opt, value].join('=')
|
Chris@0
|
80 end
|
Chris@0
|
81 end
|
Chris@0
|
82 url << "?#{opts.join('&')}" unless opts.empty?
|
Chris@0
|
83 end
|
Chris@0
|
84 end
|
Chris@0
|
85
|
Chris@0
|
86 end
|
Chris@0
|
87
|
chris@22
|
88 end
|