Class: Fluxbit::GravatarComponent
- Inherits:
-
AvatarComponent
- Object
- ViewComponent::Base
- Component
- AvatarComponent
- Fluxbit::GravatarComponent
- Includes:
- Config::AvatarComponent, Config::GravatarComponent
- Defined in:
- app/components/fluxbit/gravatar_component.rb
Overview
The ‘Fluxbit::GravatarComponent` is a component for rendering Gravatar avatars. It extends `Fluxbit::AvatarComponent` and provides options for configuring the Gravatar’s appearance and behavior. You can control the Gravatar’s rating, size, filetype, and other attributes. The Gravatar URL is constructed based on the provided email address and options.
Constant Summary
Constants inherited from Component
Instance Method Summary collapse
- #gravatar_abbreviations ⇒ Object
-
#gravatar_filename(filetype) ⇒ Object
Munges the ID and the filetype into one.
-
#gravatar_hostname(secure) ⇒ Object
Returns either Gravatar’s secure hostname or not.
-
#gravatar_id ⇒ Object
The raw MD5 hash of the users’ email.
-
#gravatar_url ⇒ Object
Constructs the full Gravatar url.
-
#initialize(**props) ⇒ GravatarComponent
constructor
Initializes the Gravatar component with the given properties.
-
#process_options(options_to) ⇒ Object
Some options need to be processed before becoming URL params.
-
#url_params_from_hash(hash) ⇒ Object
Creates a params hash like “?foo=bar” from a hash like => ‘bar’.
Methods inherited from AvatarComponent
#avatar_itself, #call, #declare_classes, #declare_color, #dot_indicator, #placeholder_icon, #placeholder_size
Methods inherited from Component
#add, #add_popover_or_tooltip, #anyicon, #element_name, #fx_id, #options, #random_id, #remove_class, #render_popover_or_tooltip, #target
Constructor Details
#initialize(**props) ⇒ GravatarComponent
Initializes the Gravatar component with the given properties.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/components/fluxbit/gravatar_component.rb', line 29 def initialize(**props) @props = props @gravatar_options = { rating: ((@props.delete(:rating)|| "").to_sym, collection: gravatar_styles[:rating], default: @@rating), secure: (@props.delete(:secure), default: true), filetype: ((@props.delete(:filetype)|| "").to_sym, collection: gravatar_styles[:filetype], default: @@filetype), default: ((@props.delete(:default)|| "").to_sym, collection: gravatar_styles[:default], default: @@default), size: gravatar_styles[:size][(@props[:size], collection: gravatar_styles[:size], default: @@size)] } add class: gravatar_styles[:base], to: @props @email = @props.delete(:email) src = gravatar_url super(src: src, **@props) end |
Instance Method Details
#gravatar_abbreviations ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'app/components/fluxbit/gravatar_component.rb', line 91 def gravatar_abbreviations { size: "s", default: "d", rating: "r", forcedefault: "f" } end |
#gravatar_filename(filetype) ⇒ Object
Munges the ID and the filetype into one. Like “abc123.png”
73 74 75 |
# File 'app/components/fluxbit/gravatar_component.rb', line 73 def gravatar_filename(filetype) "#{gravatar_id}.#{filetype}" end |
#gravatar_hostname(secure) ⇒ Object
Returns either Gravatar’s secure hostname or not.
68 69 70 |
# File 'app/components/fluxbit/gravatar_component.rb', line 68 def gravatar_hostname(secure) "http#{secure ? 's://secure.' : '://'}gravatar.com/avatar/" end |
#gravatar_id ⇒ Object
The raw MD5 hash of the users’ email. Gravatar is particularly tricky as it downcases all emails. This is really the guts of the module, everything else is just convenience.
47 48 49 |
# File 'app/components/fluxbit/gravatar_component.rb', line 47 def gravatar_id Digest::MD5.hexdigest(@email.to_s.downcase) end |
#gravatar_url ⇒ Object
Constructs the full Gravatar url.
52 53 54 55 56 |
# File 'app/components/fluxbit/gravatar_component.rb', line 52 def gravatar_url gravatar_hostname(@gravatar_options.delete(:secure)) + gravatar_filename(@gravatar_options.delete(:filetype)) + "?#{url_params_from_hash((@gravatar_options))}" end |
#process_options(options_to) ⇒ Object
Some options need to be processed before becoming URL params
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/components/fluxbit/gravatar_component.rb', line 78 def () = {} .each do |key, val| case key when :forcedefault [key] = "y" if val else [key] = val end end end |
#url_params_from_hash(hash) ⇒ Object
Creates a params hash like “?foo=bar” from a hash like => ‘bar’. The values are sorted so it produces deterministic output (and can therefore be tested easily).
61 62 63 64 65 |
# File 'app/components/fluxbit/gravatar_component.rb', line 61 def url_params_from_hash(hash) hash.map do |key, val| [ gravatar_abbreviations[key.to_sym] || key.to_s, val.to_s ].join("=") end.sort.join("&") end |