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.
Instance Method Summary collapse
- #call ⇒ Object
- #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, #declare_classes, #declare_color, #dot_indicator, #placeholder_icon, #placeholder_size
Methods inherited from Component
#add, #add_popover_or_tooltip, #anyicon, #element_name, #fx_id, #icon, #options, #popover?, #random_id, #remove_class, #remove_class_from_props, #render_popover_or_tooltip, #target, #tooltip?
Methods included from IconHelpers
#chevron_double_left, #chevron_double_right, #chevron_down, #chevron_left, #chevron_right, #chevron_up, #close_icon, #ellipsis_horizontal, #eye_icon, #eye_slash_icon, #plus_icon
Constructor Details
#initialize(**props) ⇒ GravatarComponent
Initializes the Gravatar component with the given properties.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/components/fluxbit/gravatar_component.rb', line 32 def initialize(**props) @props = props = { rating: ((@props.delete(:rating)|| "").to_sym, collection: gravatar_styles[:rating], default: ), 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)], name: @props.delete(:name), initials: @props.delete(:initials) } add class: gravatar_styles[:base], to: @props @email = @props.delete(:email) @url_only = @props.delete(:url_only) src = gravatar_url super(src: src, **@props) end |
Instance Method Details
#call ⇒ Object
50 51 52 53 |
# File 'app/components/fluxbit/gravatar_component.rb', line 50 def call return gravatar_url.html_safe if @url_only super end |
#gravatar_abbreviations ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'app/components/fluxbit/gravatar_component.rb', line 104 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”
84 85 86 |
# File 'app/components/fluxbit/gravatar_component.rb', line 84 def gravatar_filename(filetype) "#{gravatar_id}.#{filetype}" end |
#gravatar_hostname(secure) ⇒ Object
Returns either Gravatar’s secure hostname or not.
79 80 81 |
# File 'app/components/fluxbit/gravatar_component.rb', line 79 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.
58 59 60 |
# File 'app/components/fluxbit/gravatar_component.rb', line 58 def gravatar_id Digest::MD5.hexdigest(@email.to_s.downcase) end |
#gravatar_url ⇒ Object
Constructs the full Gravatar url.
63 64 65 66 67 |
# File 'app/components/fluxbit/gravatar_component.rb', line 63 def gravatar_url gravatar_hostname(.delete(:secure)) + gravatar_filename(.delete(:filetype)) + "?#{url_params_from_hash(process_options(@gravatar_options))}" end |
#process_options(options_to) ⇒ Object
Some options need to be processed before becoming URL params
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/components/fluxbit/gravatar_component.rb', line 89 def () = {} .each do |key, val| case key when :forcedefault [key] = "y" if val when :name, :initials [key] = val if val.present? 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).
72 73 74 75 76 |
# File 'app/components/fluxbit/gravatar_component.rb', line 72 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 |