Chris@909: require 'digest/md5' Chris@909: require 'cgi' Chris@909: Chris@909: module GravatarHelper Chris@909: Chris@909: # These are the options that control the default behavior of the public Chris@909: # methods. They can be overridden during the actual call to the helper, Chris@909: # or you can set them in your environment.rb as such: Chris@909: # Chris@909: # # Allow racier gravatars Chris@909: # GravatarHelper::DEFAULT_OPTIONS[:rating] = 'R' Chris@909: # Chris@909: DEFAULT_OPTIONS = { Chris@909: # The URL of a default image to display if the given email address does Chris@909: # not have a gravatar. Chris@909: :default => nil, Chris@909: Chris@909: # The default size in pixels for the gravatar image (they're square). Chris@909: :size => 50, Chris@909: Chris@909: # The maximum allowed MPAA rating for gravatars. This allows you to Chris@909: # exclude gravatars that may be out of character for your site. Chris@909: :rating => 'PG', Chris@909: Chris@909: # The alt text to use in the img tag for the gravatar. Since it's a Chris@909: # decorational picture, the alt text should be empty according to the Chris@909: # XHTML specs. Chris@909: :alt => '', Chris@909: Chris@909: # The title text to use for the img tag for the gravatar. Chris@909: :title => '', Chris@909: Chris@909: # The class to assign to the img tag for the gravatar. Chris@909: :class => 'gravatar', Chris@909: Chris@909: # Whether or not to display the gravatars using HTTPS instead of HTTP Chris@909: :ssl => false, Chris@909: } Chris@909: Chris@909: # The methods that will be made available to your views. Chris@909: module PublicMethods Chris@909: Chris@909: # Return the HTML img tag for the given user's gravatar. Presumes that Chris@909: # the given user object will respond_to "email", and return the user's Chris@909: # email address. Chris@909: def gravatar_for(user, options={}) Chris@909: gravatar(user.email, options) Chris@909: end Chris@909: Chris@909: # Return the HTML img tag for the given email address's gravatar. Chris@909: def gravatar(email, options={}) Chris@909: src = h(gravatar_url(email, options)) Chris@909: options = DEFAULT_OPTIONS.merge(options) Chris@909: [:class, :alt, :size, :title].each { |opt| options[opt] = h(options[opt]) } Chris@909: "\"#{options[:alt]}\"" Chris@909: end Chris@909: Chris@909: # Returns the base Gravatar URL for the given email hash. If ssl evaluates to true, Chris@909: # a secure URL will be used instead. This is required when the gravatar is to be Chris@909: # displayed on a HTTPS site. Chris@909: def gravatar_api_url(hash, ssl=false) Chris@909: if ssl Chris@909: "https://secure.gravatar.com/avatar/#{hash}" Chris@909: else Chris@909: "http://www.gravatar.com/avatar/#{hash}" Chris@909: end Chris@909: end Chris@909: Chris@909: # Return the gravatar URL for the given email address. Chris@909: def gravatar_url(email, options={}) Chris@909: email_hash = Digest::MD5.hexdigest(email) Chris@909: options = DEFAULT_OPTIONS.merge(options) Chris@909: options[:default] = CGI::escape(options[:default]) unless options[:default].nil? Chris@909: gravatar_api_url(email_hash, options.delete(:ssl)).tap do |url| Chris@909: opts = [] Chris@909: [:rating, :size, :default].each do |opt| Chris@909: unless options[opt].nil? Chris@909: value = h(options[opt]) Chris@909: opts << [opt, value].join('=') Chris@909: end Chris@909: end Chris@909: url << "?#{opts.join('&')}" unless opts.empty? Chris@909: end Chris@909: end Chris@909: Chris@909: end Chris@909: Chris@909: end