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