lib/gravatarify/base.rb in gravatarify-1.2.0 vs lib/gravatarify/base.rb in gravatarify-1.2.1
- old
+ new
@@ -1,21 +1,9 @@
require 'digest/md5'
begin; require 'rack/utils'; rescue LoadError; require 'cgi' end
-module Gravatarify
- # Subdomains used for balancing
- GRAVATAR_SUBDOMAINS = %w{ 0 1 2 www }
-
- # Fallback if no subdomain is found
- GRAVATAR_DEFAULT_SUBDOMAIN = 'www'
-
- # If no size is specified, gravatar.com returns 80x80px images
- GRAVATAR_DEFAULT_SIZE = 80
-
- # Default filetype is JPG
- GRAVATAR_DEFAULT_FILETYPE = :jpg
-
+module Gravatarify
# List of known and valid gravatar options (includes shortened options).
GRAVATAR_OPTIONS = [ :default, :d, :rating, :r, :size, :s, :secure, :filetype ]
# Hash of :ultra_long_option_name => 'abbrevated option'
GRAVATAR_ABBREV_OPTIONS = { :default => 'd', :rating => 'r', :size => 's' }
@@ -33,11 +21,11 @@
# Gravatarify.options[:rating] = :R
#
# # or disable adding an extension
# Gravatarify.options[:filetype] = false
#
- def options; @options ||= {} end
+ def options; @options ||= { :filetype => :jpg } end
# Globally overide subdomains used to build gravatar urls, normally
# +gravatarify+ picks from either +0.gravatar.com+, +1.gravatar.com+,
# +2.gravatar.com+ or +www.gravatar.com+ when building hosts, to use a custom
# set of subdomains (or none!) do something like:
@@ -51,20 +39,20 @@
# Shortcut method to reset subdomains to only build +www.gravatar.com+ urls,
# i.e. disable host balancing!
def use_www_only!; self.subdomains = %w{ www } end
- # Access currently defined subdomains, defaults are +GRAVTAR_SUBDOMAINS+.
- def subdomains; @subdomains ||= GRAVATAR_SUBDOMAINS end
+ # Access currently defined subdomains, defaults are +%w{ 0 1 2 www }+.
+ def subdomains; @subdomains ||= %w{ 0 1 2 www } end
- # Get subdomain for supplied string or returns +GRAVATAR_DEFAULT_SUBDOMAIN+ if none is
+ # Get subdomain for supplied string or returns +www+ if none is
# defined.
- def subdomain(str); subdomains[str.hash % subdomains.size] || GRAVATAR_DEFAULT_SUBDOMAIN end
+ def subdomain(str); subdomains[str.hash % subdomains.size] || 'www' end
- # Helper method to escape string using either <tt>Rack::Utils</tt> if available or else
+ # Helper method to URI escape a string using either <tt>Rack::Utils#escape</tt> if available or else
# fallback to <tt>CGI#escape</tt>.
- def escape(str)
+ def escape(str) #:nodoc:
str = str.to_s unless str.is_a?(String) # convert to string!
defined?(Rack::Utils) ? Rack::Utils.escape(str) : CGI.escape(str)
end
end
@@ -102,18 +90,18 @@
# @option url_options [String, Symbol] :rating (:g) Specify the rating, gravatar.com supports <tt>:g</tt>, <tt>:pg</tt>,
# <tt>:r</tt> or <tt>:x</tt>, they correspond to movie ratings :)
# @option url_options [Integer] :size (80) The size of the (square) image.
# @option url_options [Boolean, Proc] :secure (false) If set to +true+, then uses the secure gravatar.com URL. If a Proc is
# supplied it's evaluated, the Proc should evaluate to +true+ or +false+.
- # @option url_options [String, Symbol] :filetype (:jpg) Gravatar.com supports only <tt>:gif</tt>, <tt>:jpg</tt> and <tt>:png</tt>
+ # @option url_options [String, Symbol] :filetype (:jpg) Gravatar.com supports only <tt>:gif</tt>, <tt>:jpg</tt> and <tt>:png</tt>.
+ # if an set to +false+, +nil+ or an empty string no extension is added.
# @return [String] In any case (even if supplied +email+ is +nil+) returns a fully qualified gravatar.com URL.
# The returned string is not yet HTML escaped, *but* all +url_options+ have been URI escaped.
def build_gravatar_url(email, url_options = {})
- # FIXME: add symbolize_keys again, maybe just write custom method, so we do not depend on ActiveSupport magic...
url_options = Gravatarify.options.merge(url_options)
email_hash = Digest::MD5.hexdigest(Base.get_smart_email_from(email).strip.downcase)
- extension = url_options[:filetype] == false ? '' : ".#{url_options.delete(:filetype) || GRAVATAR_DEFAULT_FILETYPE}"
+ extension = (ext = url_options.delete(:filetype) and ext != '') ? ".#{ext || 'jpg'}" : '' # slightly adapted from gudleik's implementation
build_gravatar_host(email_hash, url_options.delete(:secure)) << "/avatar/#{email_hash}#{extension}#{build_gravatar_options(email, url_options)}"
end
private
# Builds gravatar host name from supplied e-mail hash.
@@ -138,10 +126,12 @@
params << "#{Gravatarify.escape(key)}=#{Gravatarify.escape(value)}" if value
end
"?#{params.sort * '&'}" unless params.empty?
end
- def self.get_smart_email_from(obj)
+ # Tries first to call +email+, then +mail+ then +to_s+ on supplied
+ # object.
+ def self.get_smart_email_from(obj) #:nodoc:
(obj.respond_to?(:email) ? obj.email : (obj.respond_to?(:mail) ? obj.mail : obj)).to_s
end
end
end
\ No newline at end of file