Module: GravatarHelper::PublicMethods

Included in:
ApplicationHelper
Defined in:
lib/plugins/gravatar/lib/gravatar.rb

Overview

The methods that will be made available to your views.

Instance Method Summary collapse

Instance Method Details

#gravatar(email, options = {}) ⇒ Object

Return the HTML img tag for the given email address's gravatar.


51
52
53
54
55
56
# File 'lib/plugins/gravatar/lib/gravatar.rb', line 51

def gravatar(email, options={})
  src = h(gravatar_url(email, options))
  options = DEFAULT_OPTIONS.merge(options)
  [:class, :alt, :title].each { |opt| options[opt] = h(options[opt]) }
  image_tag src, options
end

#gravatar_api_url(hash, ssl = false) ⇒ Object

Returns the base Gravatar URL for the given email hash. If ssl evaluates to true, a secure URL will be used instead. This is required when the gravatar is to be displayed on a HTTPS site.


61
62
63
64
65
66
67
# File 'lib/plugins/gravatar/lib/gravatar.rb', line 61

def gravatar_api_url(hash, ssl=false)
  if ssl
    "https://secure.gravatar.com/avatar/#{hash}"
  else
    "http://www.gravatar.com/avatar/#{hash}"
  end
end

#gravatar_for(user, options = {}) ⇒ Object

Return the HTML img tag for the given user's gravatar. Presumes that the given user object will respond_to “email”, and return the user's email address.


46
47
48
# File 'lib/plugins/gravatar/lib/gravatar.rb', line 46

def gravatar_for(user, options={})
  gravatar(user.email, options)
end

#gravatar_url(email, options = {}) ⇒ Object

Return the gravatar URL for the given email address.


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/plugins/gravatar/lib/gravatar.rb', line 70

def gravatar_url(email, options={})
  email_hash = Digest::MD5.hexdigest(email)
  options = DEFAULT_OPTIONS.merge(options)
  options[:default] = CGI::escape(options[:default]) unless options[:default].nil?
  gravatar_api_url(email_hash, options.delete(:ssl)).tap do |url|
    opts = []
    [:rating, :size, :default].each do |opt|
      unless options[opt].nil?
        value = h(options[opt])
        opts << [opt, value].join('=')
      end
    end
    url << "?#{opts.join('&')}" unless opts.empty?
  end
end