Class: Gravatar

Inherits:
Object
  • Object
show all
Defined in:
lib/rb-gravatar.rb

Class Method Summary collapse

Class Method Details

.construct_resource(email_address, size, default) ⇒ Object

Construct the Gravatar URI generically with improved validation and HTTPS support



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rb-gravatar.rb', line 7

def self.construct_resource(email_address, size, default)
  # Validate email format
  unless email_address =~ URI::MailTo::EMAIL_REGEXP
    raise ArgumentError, "Invalid email format"
  end

  # Size validation
  size = (1..2048).cover?(size) ? size : 64

  # Build URL
  gravatar_url = "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email_address.downcase.strip)}?s=#{size}"
  gravatar_url += "&d=#{CGI.escape(default)}" if default
  gravatar_url
end

.prefetch_dnsObject

Output the DNS preload tags



39
40
41
# File 'lib/rb-gravatar.rb', line 39

def self.prefetch_dns
  '<link rel="dns-prefetch" href="//gravatar.com">'
end

.src(email_address, size = 64, default = nil) ⇒ Object

Generate and return only the Gravatar URI



23
24
25
26
27
28
# File 'lib/rb-gravatar.rb', line 23

def self.src(email_address, size = 64, default = nil)
  construct_resource(email_address, size, default)
rescue ArgumentError => e
  puts "Error: #{e.message}"
  nil
end

.tag(email_address, size = 64, default = nil, alt_text = "Gravatar") ⇒ Object

Generate and return the full img tag for the Gravatar URI



31
32
33
34
35
36
# File 'lib/rb-gravatar.rb', line 31

def self.tag(email_address, size = 64, default = nil, alt_text = "Gravatar")
  "<img src='#{construct_resource(email_address, size, default)}' class='gravatar' alt='#{CGI.escapeHTML(alt_text)}' />"
rescue ArgumentError => e
  puts "Error: #{e.message}"
  nil
end