class Avatar::Source::GravatarSource

NOTE: since Gravatar always returns a URL (never a 404), instances of this class should only be placed at the end of a SourceChain. (see classes/Avatar/Source/SourceChain.html) Alternatively, use default_source = ... to generate a site-wide default to be passed to Gravatar. (In fact, since default_source is an instance of Avatar::Source::AbstractSource, it can generate a different default for each person.)

Attributes

default_field[RW]
default_source[R]

Public Instance Methods

avatar_url_for(person, options = {}) click to toggle source

Generates a Gravatar URL. Returns nil if person is nil. Options:

  • :gravatar_field (Symbol) - the field to call from person. By default, :email.

  • :gravatar_default_url (String) - override the default generated by default_source.

  • :gravatar_size or size or :s - the size in pixels of the avatar to render.

  • :gravatar_rating or rating or :r - the maximum rating; one of [‘G’, ‘PG’, ‘R’, ‘X’]

# File lib/avatar/source/gravatar_source.rb, line 47
def avatar_url_for(person, options = {})
  return nil if person.nil?
  options = parse_options(person, options)
  field = options.delete(:gravatar_field)
  raise ArgumentError.new('No field specified; either specify a default field or pass in a value for :gravatar_field (probably :email)') unless field
  
  email = person.send(field)
  return nil if email.nil? || email.to_s.blank?
  email = email.to_s.downcase
  
  returning(self.class.base_url) do |url|
    url << Digest::MD5::hexdigest(email).strip
    # default must be last or the other options will be parameters to that URL, not the Gravatar one
    [:size, :rating, :default].each do |k|
      v = options[k]
      next if v.nil?
      url << (url.include?('?') ? '&' : '?')
      url << "#{k}=#{v}"
    end
  end
end
default_source=(default) click to toggle source

Set the default source for all people. If default is a String, it will be converted to an instance of Avatar::Source::StaticUrlSource. If default is nil, sets the default to a NilSource.

# File lib/avatar/source/gravatar_source.rb, line 93
def default_source=(default)
  case default
  when String
    @default_source = StaticUrlSource.new(default)
  when AbstractSource
    @default_source = default
  when NilClass
    @default_source = NilSource.new
  else
    raise ArgumentError.new("#{default} must be either a String or an instance of #{AbstractSource}")
  end
end
parse_options(person, options) click to toggle source

Returns a Hash containing

  • :field - value of :gravatar_field; defaults to self.default_field

  • :default - value of :gravatar_default_url; defaults to self.default_avatar_url_for(person, options)

  • :size - value of :gravatar_size or :size or :s passed through only if a number

  • :rating - value of :gravatar_rating or :rating or :r passed through only if one of self.class.allowed_ratings

# File lib/avatar/source/gravatar_source.rb, line 74
def parse_options(person, options)
  returning({}) do |result|
    result[:gravatar_field] = options[:gravatar_field] || default_field
    
    default = options[:gravatar_default_url] || default_avatar_url_for(person, options)
    raise "default must be a fully-qualified URL with port and host" unless self.class.valid_default_url?(default)
    result[:default] = default

    size = (options[:gravatar_size] || options[:size] || options[:s] || '').to_s.to_i
    result[:size] = size if size > 0

    rating = options[:gravatar_rating] || options[:rating] || options[:r]
    result[:rating] = rating if rating and self.class.allowed_ratings.include?(rating.to_s)
  end
end

Public Class Methods

allowed_ratings() click to toggle source
‘G’, ‘PG’, ‘R’, ‘X’, ‘any’
# File lib/avatar/source/gravatar_source.rb, line 28
def self.allowed_ratings
  ['G', 'PG', 'R', 'X', 'any']
end
base_url() click to toggle source

www.gravatar.com/avatar/

# File lib/avatar/source/gravatar_source.rb, line 23
def self.base_url
  'http://www.gravatar.com/avatar/'
end
new(default_source = nil, default_field = :email) click to toggle source

Arguments:

  • default_source: a Source to generate defaults to be passed to Gravatar; optional; default: nil (a NilSource).

  • default_field: the field within each person passed to avatar_url_for in which to look for an email address

# File lib/avatar/source/gravatar_source.rb, line 35
def initialize(default_source = nil, default_field = :email)
  self.default_source = default_source #not @default_source = ... b/c want to use the setter function below
  @default_field = default_field
  raise "There's a bug in the code" if @default_source.nil?
end
valid_default_url?(url) click to toggle source
# File lib/avatar/source/gravatar_source.rb, line 106
def self.valid_default_url?(url)
  url.nil? || url =~ /^http[s]?\:/
end