Class: Satis::Avatar::Component
- Inherits:
-
Satis::ApplicationComponent
- Object
- ViewComponent::Base
- Satis::ApplicationComponent
- Satis::Avatar::Component
- Defined in:
- app/components/satis/avatar/component.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#photo ⇒ Object
readonly
Returns the value of attribute photo.
Attributes inherited from Satis::ApplicationComponent
Instance Method Summary collapse
-
#background_color ⇒ Object
Returns the hex-background color for the avatar based on the name or email.
- #gravatar? ⇒ Boolean
- #gravatar_url ⇒ Object
-
#initialize(name: nil, email: nil, photo: nil, **options) ⇒ Component
constructor
A new instance of Component.
- #initials ⇒ Object
- #photo_url ⇒ Object
Methods inherited from Satis::ApplicationComponent
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, **) super @name = name @photo = photo @options = @options[:class] ||= 'w-8 h-8' @email = email @background_color = "#6b7280" @color = .fetch(:color, true) end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
7 8 9 |
# File 'app/components/satis/avatar/component.rb', line 7 def color @color end |
#email ⇒ Object (readonly)
Returns the value of attribute email.
7 8 9 |
# File 'app/components/satis/avatar/component.rb', line 7 def email @email end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'app/components/satis/avatar/component.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'app/components/satis/avatar/component.rb', line 7 def @options end |
#photo ⇒ Object (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_color ⇒ Object
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
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_url ⇒ Object
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 |
#initials ⇒ Object
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_url ⇒ Object
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 |