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
-
#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
- #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 |
# 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 end |
Instance Attribute Details
#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
#gravatar? ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/components/satis/avatar/component.rb', line 33 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
49 50 51 |
# File 'app/components/satis/avatar/component.rb', line 49 def gravatar_url "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email).downcase}?d=404" if gravatar? end |
#initials ⇒ Object
18 19 20 21 22 23 24 |
# File 'app/components/satis/avatar/component.rb', line 18 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_url ⇒ Object
26 27 28 29 30 31 |
# File 'app/components/satis/avatar/component.rb', line 26 def photo_url return unless photo&.attached? #helpers.main_app.url_for(photo) Rails.application.routes.url_helpers.rails_blob_path(photo, only_path: true) end |