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

Constructor Details

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

Returns a new instance of Component.



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

def initialize(name: nil, email: nil, photo: nil, **options)
  super
  @name = name
  @photo = photo
  @options = options
  @options[:class] ||= 'w-8 h-8'
  @email = email
  @background_color = "#6b7280"
  @color = options.fetch(:color, true)
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



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

def color
  @color
end

#emailObject (readonly)

Returns the value of attribute email.



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

def email
  @email
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#photoObject (readonly)

Returns the value of attribute photo.



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

def photo
  @photo
end

Instance Method Details

#background_colorObject

Returns the hex-background color for the avatar based on the name or email



29
30
31
32
33
# File 'app/components/satis/avatar/component.rb', line 29

def background_color
  return unless name || email || color

  @background_color = "##{Digest::MD5.hexdigest(name || email).first(6)}"
end

#gravatar?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/components/satis/avatar/component.rb', line 41

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



57
58
59
# File 'app/components/satis/avatar/component.rb', line 57

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

#initialsObject



20
21
22
23
24
25
26
# File 'app/components/satis/avatar/component.rb', line 20

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

#photo_urlObject



35
36
37
38
39
# File 'app/components/satis/avatar/component.rb', line 35

def photo_url
  return unless photo&.attached?

  Rails.application.routes.url_helpers.rails_blob_path(photo, only_path: true)
end