Class: Avatar::Source::GravatarSource
- Inherits:
-
Object
- Object
- Avatar::Source::GravatarSource
- 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
-
#default_field ⇒ Object
Returns the value of attribute default_field.
-
#default_source ⇒ Object
Returns the value of attribute default_source.
Class Method Summary collapse
-
.allowed_ratings ⇒ Object
[‘G’, ‘PG’, ‘R’, ‘X’].
- .base_url ⇒ Object
Instance Method Summary collapse
-
#avatar_url_for(person, options = {}) ⇒ Object
Generates a Gravatar URL.
-
#initialize(default_source = nil, default_field = :email) ⇒ GravatarSource
constructor
Arguments: *
default_source
: a Source to generate defaults to be passed to Gravatar; optional; default: nil (a NilSource). -
#parse_options(person, options) ⇒ Object
Returns a Hash containing * :field - passed through; defaults to
self.default_field
* :default - passed through; defaults toself.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 ofself.class.allowed_ratings
.
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 eachperson
passed toavatar_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_field ⇒ Object
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_source ⇒ Object
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_ratings ⇒ Object
- ‘G’, ‘PG’, ‘R’, ‘X’
27 28 29 |
# File 'lib/avatar/source/gravatar_source.rb', line 27 def self. ['G', 'PG', 'R', 'X'] end |
.base_url ⇒ Object
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 bydefault_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’]
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, = {}) return nil if person.nil? = (person, ) field = .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 .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 (person, ) returning({}) do |result| result[:gravatar_field] = [:gravatar_field] || default_field result[:default] = [:default] || default_avatar_url_for(person, ) size = [:gravatar_size] || [:size] || [:s] result[:size] = size if size.to_s =~ /^\d*/ = [:gravatar_rating] || [:rating] || [:r] result[:rating] = .upcase if and self.class..include?(.to_s) end end |