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