Module: ImgGravatar

Defined in:
lib/img_gravatar.rb

Overview

ImgGravatar

Adds the gravatar method to ActionViews.

Defined Under Namespace

Modules: Base, ClassMethods, InstanceMethods

Constant Summary collapse

@@gravatar_host =
'www.gravatar.com'
@@gravatar_base_url =
"http://#{@@gravatar_host}/avatar"
@@default_img_url =
'/img/no_gravatar.png'
@@minimum_size =
1
@@maximum_size =
512
@@default_size =
80
@@default_rating =
'g'

Class Method Summary collapse

Class Method Details

.encode_md5(email) ⇒ Object

encode an EMail for Gravatar. This will basically take any string, strip is, and hash the result using MD5.



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/img_gravatar.rb', line 92

def self.encode_md5(email)
  value = email.downcase.strip
  case DIGEST_INTERFACE
    when :ruby18
      return MD5.md5(value)
    when :ruby19
      return Digest::MD5.hexdigest(value)
    else
      raise "unknown Ruby Digest interface."
  end
end

.image_url(email, opts = {}) ⇒ Object

get the default Gravatar image. options:

:default_url  - the default URL for this gravatar
:size         - the requested gravatar size
:rating       - the requested maximum rating


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/img_gravatar.rb', line 68

def self.image_url(email, opts={})
  appender = '?'
  
  # now, load infos from options
  default_img_url = check_default_opt(opts[:default_url])
  size = check_size_opt(opts[:size] )
  rating = check_rating_opt(opts[:rating])
  
  query = nil
  query = "s=%s" % size if size
  query = query ? "%s&r=%s" % [query, rating] : "r=%s" % rating if rating
  query = query ? "%s&d=%s" % [query, default_img_url] : "d=%s" % default_img_url if default_img_url
  
  query = URI.escape(query) if query
  
  #uri = URI::HTTP.new(Gravatar.gravatar_base_url)
  uri = URI::HTTP.build(:host => ImgGravatar.gravatar_host,
    :path => "/avatar/%s" % encode_md5(email),
    :query => query)
end

get the Gravatar image. options:

:alt          - the alternative text for this image (img attribute)
:default_url  - the default URL for this gravatar
:size         - the requested gravatar size (img attribute, gravatar-attribute)
:rating       - the requested maximum rating


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/img_gravatar.rb', line 48

def self.link_gravatar(email, opts={})
  # the defaults
  tag_options = {}
  tag_options[:alt] =opts[:alt]   if opts[:alt]
  tag_options[:size] =opts[:size] if opts[:size] && (opts[:size] >= 1 && opts[:size] <= 512)

  unless tag_options.empty?
    attributes = tag_options.collect {|key, value| "#{key}=\"#{value}\"" }.join(" ")
    "<img src=\"%s\" %s />" % [image_url(email, opts), attributes]
  else
    "<img src=\"%s\" />" % image_url(email, opts)
  end
end