Class: Faces::Providers::Gravatar

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

Overview

Gravatar class handles all Gravatar based avatar methods, check the information page for more details: nickpellant.com/open-source/faces/ruby/providers/gravatar

Default required methods for providers are url and/or html, exists? All other methods are at the discretion of the developer Check the provider creation guide for more details: nickpellant.com/open-source/faces/ruby/developing-providers

Instance Method Summary collapse

Instance Method Details

#exists?(email, configuration = {}) ⇒ Boolean

Checks Avatar exists

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
# File 'lib/providers/gravatar.rb', line 34

def exists?(email, configuration = {})
  m_configuration = Faces::Common.merge_configurations([Faces::Configuration::GRAVATAR, configuration, {:default => '404'}])
  url = URI.parse(url(email, m_configuration))
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = (url.scheme == 'https')
  request = Net::HTTP::Get.new(url.path + '?' + url.query)
  response = http.request(request)
  response.code == '200' ? true : false
end

#html(email, configuration = {}) ⇒ Object

Constructs HTML <img /> tag for Gravatar



29
30
31
32
# File 'lib/providers/gravatar.rb', line 29

def html(email, configuration = {})
  m_configuration = Faces::Common.merge_configurations([Faces::Configuration::GRAVATAR, configuration])
  Faces::Public.generate_html(url(email, configuration), m_configuration)
end

#md5_email(email) ⇒ Object

Formats email and converts into MD5 hash for use with Gravatar url



46
47
48
# File 'lib/providers/gravatar.rb', line 46

def md5_email(email)
  Digest::MD5.hexdigest(email.strip.downcase)
end

#ssl?Boolean

Supports SSL

Returns:

  • (Boolean)


44
# File 'lib/providers/gravatar.rb', line 44

def ssl?; true; end

#url(email, configuration = {}) ⇒ Object

Constructs Gravatar url from email/configuration  It can be passed either an MD5 hash as email or a string email



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

def url(email, configuration = {})
  m_configuration = Faces::Common.merge_configurations([Faces::Configuration::GRAVATAR, configuration])
  email = md5_email(email) if email.include? "@"
  url = hostname(m_configuration) + email + build_params_string(m_configuration)
end