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’, ‘any’].
- .base_url ⇒ Object
- .valid_default_url?(url) ⇒ Boolean
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 - value of :gravatar_field; defaults to
self.default_field
* :default - value of :gravatar_default_url; defaults toself.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 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
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_field ⇒ Object
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_source ⇒ Object
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_ratings ⇒ Object
- ‘G’, ‘PG’, ‘R’, ‘X’, ‘any’
28 29 30 |
# File 'lib/avatar/source/gravatar_source.rb', line 28 def self. ['G', 'PG', 'R', 'X', 'any'] end |
.base_url ⇒ Object
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
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 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’]
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, = {}) 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 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 = [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 (person, ) returning({}) do |result| result[:gravatar_field] = [:gravatar_field] || default_field default = [:gravatar_default_url] || default_avatar_url_for(person, ) raise "default must be a fully-qualified URL with port and host" unless self.class.valid_default_url?(default) result[:default] = default size = ([:gravatar_size] || [:size] || [:s] || '').to_s.to_i result[:size] = size if size > 0 = [:gravatar_rating] || [:rating] || [:r] result[:rating] = if and self.class..include?(.to_s) end end |