Module: GravatarFor::ViewHelpers

Defined in:
lib/gravatar_for/gravatar_for.rb

Instance Method Summary collapse

Instance Method Details

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

Returns an HTML5 image tag to the Gravatar image for email.

Options

You can add HTML attributes and modify Gravatar results using the options

  • :size - If no size is given; Gravatar’s default of 80px by 80px is used.

  • :default - The default URL for an image to display if the email has no Gravatar image. By default, Gravatar returns its logo if there is no image to reference. To cover all basis, you can use the following symbols to use some of Gravtar’s other options:

    * :mm        - Mystery Man
    * :identicon - A geometric pattern based on an email has
    * :monsterid - A generated monster
    * :wavatar   - Computer generated faces
    * :retro     - 8-bit generated arcade-styled face
    * :blank     - A transparent PNG image
    * 404        - HTTP 404 response (File Not Found)
    
  • :rating - Rating to request, either :g or ‘g’ (default); :pg or ‘pg’; :r or ‘r’; :x or ‘x’

  • :forcedefault - Force default image, default: false

  • :alt - Override the HTML alternate text attribute. By default the email provided will be placed into this attribute.

  • :ssl - Use regular or secure URL, default false

  • Any additional options will be passed to ActionView::Helpers::TagHelper::tag

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gravatar_for/gravatar_for.rb', line 34

def gravatar_for(email, options = {})
  raise ArgumentError, 'Email must be provided' unless email && !email.strip.empty?

  base_url = options[:ssl]? GRAVATAR_SECURE_BASE_URL : GRAVATAR_BASE_URL
  options.delete(:ssl) if options.has_key?(:ssh)

  options[:alt] ||= email

  query = ''
  [:size, :default, :rating, :forcedefault].each do |key|
    if options.has_key?(key)
      query << (query.empty? ? '?' : '&')
      query << "#{key}=#{options.delete(key)}"
    end
  end

  src = base_url + Digest::MD5.hexdigest(email)
  src << query unless query.empty?

  options[:src] = src

  tag('img', options, true)
end