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
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
-
#build_options_parameters(options = {}) ⇒ Object
protected
Filters, Validates and build the options parameters for the URL.
-
#gravatar_image(email, options = {}) ⇒ Object
Generate the image tag to the gravatar of the supplied email.
-
#gravatar_url(email, options = {}) ⇒ Object
Generate image URL.
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
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/nanoc/toolbox/helpers/gravatar.rb', line 97 def (={}) # Remove unecessary options .delete_if {|key, value| !GRAVATAR_OPTIONS.include?(key) } # Return now if the options hash is empty after cleanup return '' if .empty? # Build the parameters string '?' + .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
44 45 46 47 48 |
# File 'lib/nanoc/toolbox/helpers/gravatar.rb', line 44 def gravatar_image(email, ={}) image_url = gravatar_url(email, ) .delete_if {|key, value| GRAVATAR_OPTIONS.include?(key) } tag('img', .merge(:src => image_url)) end |
#gravatar_url(email, options = {}) ⇒ Object
Generate image URL
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, ={}) # Prepare the email email = clean_email(email) # Prepare the options = DEFAULT_VALUES.merge() = () # Retreive the URL depending on the protocole and build it protocole = [:secure] ? :https : :http host = URL[protocole] # generate the image name and append a the extension if requested file = hash_email(email) file += '.jpg' if [:ext] # Build the query string parameters = () "#{host}#{file}#{parameters}" end |