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



34
35
36
37
38
# File 'lib/avatar/source/gravatar_source.rb', line 34

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.



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

def default_field
  @default_field
end

#default_sourceObject

Returns the value of attribute default_source.



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

def default_source
  @default_source
end

Class Method Details

.allowed_ratingsObject

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


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

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

.base_urlObject



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

def self.base_url
  'http://www.gravatar.com/avatar/'
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.

  • :default (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)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/avatar/source/gravatar_source.rb', line 46

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
  
  returning(self.class.base_url) do |url|
    url << Digest::MD5::hexdigest(person.send(field)).strip
    options.each do |k, v|
      next if v.nil?
      url << (url.include?('?') ? '&' : '?')
      url << "#{k}=#{v}"
    end
  end
end

#parse_options(person, options) ⇒ Object

Returns a Hash containing

  • :field - passed through; defaults to self.default_field

  • :default - passed through; defaults to self.default_avatar_url_for(person, options)

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

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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/avatar/source/gravatar_source.rb', line 67

def parse_options(person, options)
  returning({}) do |result|
    result[:gravatar_field] = options[:gravatar_field] || default_field
    
    result[:default] = options[:default] || default_avatar_url_for(person, options)

    size = options[:gravatar_size] || options[:size] || options[:s]
    result[:size] = size if size.to_s =~ /^\d*/

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