0
+require 'avatar/source/abstract_source'
0
+require 'avatar/source/static_url_source'
0
+require 'avatar/source/nil_source'
0
+module Avatar # :nodoc:
0
+ module Source # :nodoc:
0
+ # NOTE: since Gravatar always returns a URL (never a 404), instances of this
0
+ # class should only be placed at the end of a SourceChain.
0
+ # (see link:classes/Avatar/Source/SourceChain.html)
0
+ # Alternatively, use <code>default_source = ...</code> to generate a site-wide
0
+ # default to be passed to Gravatar. (In fact, since <code>default_source</code>
0
+ # is an instance of Avatar::Source::AbstractSource, it can generate a different
0
+ # default for each person.)
0
+ include AbstractSource
0
+ attr_accessor :default_field
0
+ attr_reader :default_source
0
+ # 'http://www.gravatar.com/avatar/'
0
+ 'http://www.gravatar.com/avatar/'
0
+ def initialize(default_source = nil, default_field = :email)
0
+ self.default_source = default_source #not @default_source = ... b/c want to use the setter function below
0
+ @default_field = default_field
0
+ raise "There's a bug in the code" if @default_source.nil?
0
+ # Generates a Gravatar URL. Returns nil if person is nil.
0
+ # * <code>:field (Symbol)</code> - the field to call from person. By default, <code>:email</code>.
0
+ # * <code>:default (String)</code> - override the default generated by <code>default_source</code>.
0
+ # * <code>:size or :s</code> - the size in pixels of the avatar to render.
0
+ # * <code>:rating or :r</code> - the maximum rating; one of ['G', 'PG', 'R', 'X']
0
+ def avatar_url_for(person, options = {})
0
+ return nil if person.nil?
0
+ options[:default] ||= default_avatar_url_for(person, options)
0
+ field = options[:field] || default_field
0
+ raise ArgumentError.new('No field specified; either specify a default field or pass in a value for :field (probably :email)') unless field
0
+ url = self.class.base_url
0
+ url << Digest::MD5::hexdigest(person.send(field)).strip
0
+ [:size, :s, :rating, :r, :default].each do |x|
0
+ next unless options[x]
0
+ url << (url.include?('?') ? '&' : '?')
0
+ url << "#{x}=#{options[x]}"
0
+ # Set the default source for all people.
0
+ # If +default+ is a String, it will be converted to an instance of Avatar::Source::StaticUrlSource.
0
+ # If +default+ is nil, sets the default to a NilSource.
0
+ def default_source=(default)
0
+ @default_source = StaticUrlSource.new(default)
0
+ @default_source = default
0
+ @default_source = NilSource.new
0
+ raise ArgumentError.new("#{default} must be either a String or an instance of #{AbstractSource}")
0
+ def default_avatar_url_for(person, options)
0
+ @default_source.avatar_url_for(person, options)