Class: Avatar::Source::GravatarSource

Inherits:
Object
  • Object
show all
Includes:
AbstractSource
Defined in:
lib/avatar/source/gravatar_source.rb

Overview

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.)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_source = nil, default_field = :email) ⇒ GravatarSource

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



35
36
37
38
39
# 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

Instance Attribute Details

#default_fieldObject

Returns the value of attribute default_field.



19
20
21
# File 'lib/avatar/source/gravatar_source.rb', line 19

def default_field
  @default_field
end

#default_sourceObject

Returns the value of attribute default_source.



20
21
22
# File 'lib/avatar/source/gravatar_source.rb', line 20

def default_source
  @default_source
end

Class Method Details

.allowed_ratingsObject

‘G’, ‘PG’, ‘R’, ‘X’, ‘any’


28
29
30
# File 'lib/avatar/source/gravatar_source.rb', line 28

def self.allowed_ratings
  ['G', 'PG', 'R', 'X', 'any']
end

.base_urlObject



23
24
25
# File 'lib/avatar/source/gravatar_source.rb', line 23

def self.base_url
  'http://www.gravatar.com/avatar/'
end

.valid_default_url?(url) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/avatar/source/gravatar_source.rb', line 106

def self.valid_default_url?(url)
  url.nil? || url =~ /^http[s]?\:/
end

Instance Method Details

#avatar_url_for(person, options = {}) ⇒ Object

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’]

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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

#parse_options(person, options) ⇒ Object

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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