lib/gravatarify/base.rb in gravatarify-1.1.0 vs lib/gravatarify/base.rb in gravatarify-1.2.0
- old
+ new
@@ -30,10 +30,13 @@
# Gravatarify.options[:default] = Proc.new { |*args| "http://example.com/avatar-#{args.first[:size] || 80}.jpg" }
#
# # or set a custom default rating
# Gravatarify.options[:rating] = :R
#
+ # # or disable adding an extension
+ # Gravatarify.options[:filetype] = false
+ #
def options; @options ||= {} 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
@@ -55,11 +58,16 @@
# Get subdomain for supplied string or returns +GRAVATAR_DEFAULT_SUBDOMAIN+ if none is
# defined.
def subdomain(str); subdomains[str.hash % subdomains.size] || GRAVATAR_DEFAULT_SUBDOMAIN end
- def escape(str); defined?(Rack::Utils) ? Rack::Utils.escape(str) : CGI.escape(str) end
+ # Helper method to escape string using either <tt>Rack::Utils</tt> if available or else
+ # fallback to <tt>CGI#escape</tt>.
+ def escape(str)
+ str = str.to_s unless str.is_a?(String) # convert to string!
+ defined?(Rack::Utils) ? Rack::Utils.escape(str) : CGI.escape(str)
+ end
end
# Provides core support to build gravatar urls based on supplied e-mail strings.
module Base
@@ -101,13 +109,12 @@
# 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)
-
- build_gravatar_host(email_hash, url_options.delete(:secure)) <<
- "/avatar/#{email_hash}.#{url_options.delete(:filetype) || GRAVATAR_DEFAULT_FILETYPE}#{build_gravatar_options(email, url_options)}"
+ extension = url_options[:filetype] == false ? '' : ".#{url_options.delete(:filetype) || GRAVATAR_DEFAULT_FILETYPE}"
+ 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.
# Ensures that for the same +str_hash+ always the same subdomain is used.
@@ -126,10 +133,10 @@
def build_gravatar_options(source, url_options = {})
params = []
url_options.each_pair do |key, value|
key = GRAVATAR_ABBREV_OPTIONS[key] if GRAVATAR_ABBREV_OPTIONS.include?(key) # shorten key!
value = value.call(url_options, source.is_a?(String) ? self : source) if key.to_s == 'd' and value.respond_to?(:call)
- params << "#{Gravatarify.escape(key.to_s)}=#{Gravatarify.escape(value.to_s)}" if value
+ params << "#{Gravatarify.escape(key)}=#{Gravatarify.escape(value)}" if value
end
"?#{params.sort * '&'}" unless params.empty?
end
def self.get_smart_email_from(obj)
\ No newline at end of file