Class: Vishnu

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

Constant Summary collapse

PROFILES =

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

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

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”



38
39
40
41
42
43
44
# File 'lib/vishnu.rb', line 38

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.



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

def default
  @default
end

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#httpsObject

Returns the value of attribute https.



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

def https
  @https
end

#openidObject

Returns the value of attribute openid.



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

def openid
  @openid
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

Instance Method Details

#urlObject Also known as: to_s

Generate the libravatar URL



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vishnu.rb', line 63

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

  size    = "s=#{@size}"    if @size
  default = "d=#{@default}" if @default

  # noinspection RubyScope
  # ok for them to be nil
  query = [size, default].reject(&:!).join('&')
  query = "?#{query}" unless query == ''

  base_url = get_base_url + '/avatar/'

  base_url + id + query
end