To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 60 / 6078dc0e2b2b4a50e4f4a1933cc7de20b96803b2.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (2.85 KB)

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