Class: Satis::Avatar::Component

Inherits:
Satis::ApplicationComponent show all
Defined in:
app/components/satis/avatar/component.rb

Instance Attribute Summary collapse

Attributes inherited from Satis::ApplicationComponent

#original_view_context

Instance Method Summary collapse

Methods inherited from Satis::ApplicationComponent

add_helper, #component_name, #ct, #i18n_scope, #original_i18n_scope, #original_virtual_path

Constructor Details

#initialize(name: nil, email: nil, photo: nil, **options) ⇒ Component

Returns a new instance of Component.



8
9
10
11
12
13
14
15
# File 'app/components/satis/avatar/component.rb', line 8

def initialize(name: nil, email: nil, photo: nil, **options)
  super
  @name = name
  @photo = photo
  @options = options
  @options[:class] ||= 'w-8 h-8'
  @email = email
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



6
7
8
# File 'app/components/satis/avatar/component.rb', line 6

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'app/components/satis/avatar/component.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'app/components/satis/avatar/component.rb', line 6

def options
  @options
end

#photoObject (readonly)

Returns the value of attribute photo.



6
7
8
# File 'app/components/satis/avatar/component.rb', line 6

def photo
  @photo
end

Instance Method Details

#gravatar?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/satis/avatar/component.rb', line 31

def gravatar?
  return false if email.blank?

  url = "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email).downcase}?d=404"

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'

  request = Net::HTTP::Get.new(uri.request_uri)
  request.add_field('User-Agent', controller.request.user_agent)
  response = http.request(request)

  response.code.to_i != 404
end

#gravatar_urlObject



47
48
49
# File 'app/components/satis/avatar/component.rb', line 47

def gravatar_url
  "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email).downcase}?d=404" if gravatar?
end

#initialsObject



17
18
19
20
21
22
23
# File 'app/components/satis/avatar/component.rb', line 17

def initials
  if name.present? && !name.index('@')
    name.scan(/[A-Z]/)[0..1].join
  else
    (name || email).split('@').map(&:capitalize).join('@').scan(/[A-Z]/)[0..1].join
  end
end

#photo_urlObject



25
26
27
28
29
# File 'app/components/satis/avatar/component.rb', line 25

def photo_url
  return unless photo&.attached?

  helpers.main_app.url_for(photo)
end