lib/gravatarify/base.rb in gravatarify-2.0.3 vs lib/gravatarify/base.rb in gravatarify-2.0.4
- old
+ new
@@ -1,18 +1,22 @@
require 'digest/md5'
+require 'cgi'
module Gravatarify
- # Hash of 'ultra_long_option_name' => 'abbrevated option'
- # :nodoc:
- GRAVATAR_ABBREV_OPTIONS = { 'default' => 'd', 'rating' => 'r', 'size' => 's' }
+ # Hash of :ultra_long_option_name => :abbr_opt
+ GRAVATAR_ABBREV_OPTIONS = { 'default' => :d, :default => :d, 'rating' => :r, :rating => :r, 'size' => :s, :size => :s }
class << self
- # Globally define options which are then merged on every call to
- # +gravatar_url+, this is useful to e.g. define the default image.
+
+ # Global options which are merged on every call to
+ # +gravatar_url+, this is useful to e.g. define a default image.
#
- # Setting global options should be done (for Rails apps) in an initializer:
+ # When using Rails defining default options is best done in an
+ # initializer +config/initializers/gravatarify.rb+ (or similar).
#
+ # Usage examples:
+ #
# # set the default image using a Proc
# 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
@@ -113,14 +117,13 @@
end
# Builds a query string from all passed in options.
def build_gravatar_options(source, url_options = {})
params = url_options.inject([]) do |params, (key, value)|
- key = key.to_s
- if key != 'html'
- key = GRAVATAR_ABBREV_OPTIONS[key] if GRAVATAR_ABBREV_OPTIONS.include?(key) # shorten key!
- value = value.call(url_options, source) if key == 'd' and value.respond_to?(:call)
- params << "#{Utils.escape(key)}=#{Utils.escape(value)}" if value
+ key = (GRAVATAR_ABBREV_OPTIONS[key] || key).to_sym # shorten key
+ unless key == :html
+ value = value.call(url_options, source) if key == :d and value.respond_to?(:call)
+ params << "#{key}=#{CGI.escape(value.to_s)}" if value
end
params
end
"?#{params.sort * '&'}" unless params.empty?
end
\ No newline at end of file