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