Module: Nanoc::Toolbox::Helpers::Gravatar

Includes:
HtmlTag
Defined in:
lib/nanoc/toolbox/helpers/gravatar.rb

Overview

NANOC Helper for the Gravatar avatars

This module contains functions for generating URL or IMG tags to display the Gravatar linked to an address email, or the default gravatar

See Also:

Author:

Constant Summary collapse

URL =

Gravatar avatar image base URL

{ :http   => "http://gravatar.com/avatar/",
:https  => "https://secure.gratatar.com/avatar/" }
RATING =

Collection of allowed ratings

%w{g pg r x}
ICONS =

Default Icon sets

%w{none identicon monsterid wavatar 404}
SIZE =

Size range available

1..512
HELPER_OPTION =

Available options that could be configure

[:ext, :secure]
GRAVATAR_OPTIONS =
[:default_icon, :size, :rating]
AVAILABLE_OPTIONS =
HELPER_OPTION + GRAVATAR_OPTIONS
DEFAULT_VALUES =

Default values set to the options

{ :size => nil, :rating => nil, :ext => false, :secure => false }

Instance Method Summary collapse

Methods included from HtmlTag

#content_tag, #tag, #tag_options

Instance Method Details

#build_options_parameters(options = {}) ⇒ Object (protected)

Filters, Validates and build the options parameters for the URL. It simply consists in returning the URL query string based on the options passed after validating the availability the correctness of the option

Examples:

{:size => 225, :rating => 'g', :ext => false, :secure => false}
#=> "?size=225&rating=g"

Parameters:

  • options (Hash) (defaults to: {})

    the options to pass to the gravatar URL

Options Hash (options):

  • default_icon (String)
  • size (String) — default: nil

    Size of the image, between 1 to 512

  • rating (String) — default: nil

    The rating allowed, a value in g, pg, r or x

  • ext (Boolean) — default: false

    Use or not a file extension

  • secure (Boolean) — default: false

    Use or not https



97
98
99
100
101
102
103
104
105
106
# File 'lib/nanoc/toolbox/helpers/gravatar.rb', line 97

def build_options_parameters(options={})
  # Remove unecessary options
  options.delete_if {|key, value| !GRAVATAR_OPTIONS.include?(key) }

  # Return now if the options hash is empty after cleanup
  return '' if options.empty?

  # Build the parameters string
  '?' + options.sort{|a,b| a.to_s <=> b.to_s}.map{ |e| e = e.join('=') if e.size == 2}.join('&')
end

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

Generate the image tag to the gravatar of the supplied email

Examples:

gravatar_image('[email protected]', :size => "100")
#=> <img src="http://gravatar.com/avatar/4d076af1db60b16e1ce080505baf821c?size=100" />

Parameters:

  • email (String)
    • the email address of the gravatar account

  • options (Hash) (defaults to: {})

    the options to pass to the gravatar URL

Options Hash (options):

  • default_icon (String)
  • size (String) — default: nil

    Size of the image, between 1 to 512

  • rating (String) — default: nil

    The rating allowed, a value in g, pg, r or x

  • ext (Boolean) — default: false

    Use or not a file extension

  • secure (Boolean) — default: false

    Use or not https



44
45
46
47
48
# File 'lib/nanoc/toolbox/helpers/gravatar.rb', line 44

def gravatar_image(email, options={})
  image_url = gravatar_url(email, options)
  options.delete_if {|key, value| GRAVATAR_OPTIONS.include?(key) }
  tag('img', options.merge(:src => image_url))
end

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

Generate image URL

Examples:

gravatar_url('[email protected]', :size => "512", :ext => true)
#=> "http://gravatar.com/avatar/4d076af1db60b16e1ce080505baf821c.jpg?size=512"

Parameters:

  • email (String)
    • the email address of the gravatar account

  • options (Hash) (defaults to: {})

    the options to pass to the gravatar URL

Options Hash (options):

  • default_icon (String)
  • size (String) — default: nil

    Size of the image, between 1 to 512

  • rating (String) — default: nil

    The rating allowed, a value in g, pg, r or x

  • ext (Boolean) — default: false

    Use or not a file extension

  • secure (Boolean) — default: false

    Use or not https



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nanoc/toolbox/helpers/gravatar.rb', line 60

def gravatar_url(email, options={})
  # Prepare the email
  email = clean_email(email)

  # Prepare the options
  options = DEFAULT_VALUES.merge(options)
  options = clean_options(options)

  # Retreive the URL depending on the protocole and build it
  protocole = options[:secure] ? :https : :http
  host = URL[protocole]

  # generate the image name and append a the extension if requested
  file = hash_email(email)
  file += '.jpg' if options[:ext]

  # Build the query string
  parameters = build_options_parameters(options)

  "#{host}#{file}#{parameters}"
end