Class: Vishnu

Inherits:
Object
  • Object
show all
Defined in:
lib/vishnu.rb,
lib/vishnu/version.rb

Direct Known Subclasses

Libravatar

Constant Summary collapse

PROFILES =

All the values which are different between HTTP and HTTPS methods.

[
    {
        scheme: 'http://',
        host:   'cdn.libravatar.org',
        srv:    '_avatars._tcp.',
        port:   80,
    },
    {
        scheme: 'https://',
        host:   'seccdn.libravatar.org',
        srv:    '_avatars-sec._tcp.',
        port:   443,
    }
]
VERSION =
'2.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email: nil, openid: nil, size: nil, default: nil, https: nil) ⇒ Vishnu

The options should contain :email or :openid values. If both are given, email will be used. The value of openid and email will be normalized by the rule described in wiki.libravatar.org/api/

List of option keys:

  • :email

  • :openid

  • :size An integer ranged 1 - 512, default is 80.

  • :https Set to true to serve avatars over SSL

  • :default URL to redirect missing avatars to, or one of these specials: “404”, “mm”, “identicon”, “monsterid”, “wavatar”, “retro”


36
37
38
39
40
41
42
# File 'lib/vishnu.rb', line 36

def initialize(email: nil, openid: nil, size: nil, default: nil, https: nil)
  @email   = email
  @openid  = openid
  @size    = size
  @default = default
  @https   = https
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.


22
23
24
# File 'lib/vishnu.rb', line 22

def default
  @default
end

#emailObject

Returns the value of attribute email.


22
23
24
# File 'lib/vishnu.rb', line 22

def email
  @email
end

#httpsObject

Returns the value of attribute https.


22
23
24
# File 'lib/vishnu.rb', line 22

def https
  @https
end

#openidObject

Returns the value of attribute openid.


22
23
24
# File 'lib/vishnu.rb', line 22

def openid
  @openid
end

#sizeObject

Returns the value of attribute size.


22
23
24
# File 'lib/vishnu.rb', line 22

def size
  @size
end

Instance Method Details

#urlObject Also known as: to_s

Generate the libravatar URL


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vishnu.rb', line 61

def url
  if @email
    id = Digest::MD5.hexdigest(normalize_email(@email))
  else
    id = Digest::SHA2.hexdigest(normalize_openid(@openid))
  end

  size    = @size ?     "s=#{@size}" : nil
  default = @default ?  "d=#{@default}" : nil

  query = [ size, default ].reject{ |x| !x }.join('&')
  query = "?#{query}" unless query == ''

  baseurl = get_base_url + '/avatar/'

  baseurl + id + query
end