Class: Fluxbit::GravatarComponent

Inherits:
AvatarComponent show all
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

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.

Parameters:

  • props (Hash)

    The properties to customize the Gravatar.

Options Hash (**props):

  • :email (String)

    The email address associated with the Gravatar.

  • :name (String)

    The display name for the Gravatar (used with :initials and :color defaults).

  • :initials (String)

    Custom initials to display (used with :initials default).

  • :rating (Symbol) — default: :g

    The rating of the Gravatar (:g, :pg, :r, :x).

  • :secure (Boolean) — default: true

    Whether to use HTTPS for the Gravatar URL.

  • :filetype (Symbol) — default: :png

    The filetype of the Gravatar (:png, :jpg, :gif).

  • :default (Symbol) — default: :identicon

    The default image to use if no Gravatar is found.

  • :size (Integer) — default: :md

    The size of the Gravatar base on the size provided by AvatarComponent.

  • :url_only (Boolean) — default: false

    If true, returns only the Gravatar URL instead of rendering the avatar component.

  • :remove_class (String) — default: ''

    Classes to be removed from the default Gravatar class list.

  • **props (Hash)

    Remaining options declared as HTML attributes, applied to the Gravatar container.



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
  @gravatar_options = {
    rating: options((@props.delete(:rating)|| "").to_sym, collection: gravatar_styles[:rating], default: @@rating),
    secure: options(@props.delete(:secure), default: true),
    filetype: options((@props.delete(:filetype)|| "").to_sym, collection: gravatar_styles[:filetype], default: @@filetype),
    default: options((@props.delete(:default)|| "").to_sym, collection: gravatar_styles[:default], default: @@default),
    size: gravatar_styles[:size][options(@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

#callObject



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_abbreviationsObject



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_idObject

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_urlObject

Constructs the full Gravatar url.



63
64
65
66
67
# File 'app/components/fluxbit/gravatar_component.rb', line 63

def gravatar_url
  gravatar_hostname(@gravatar_options.delete(:secure)) +
    gravatar_filename(@gravatar_options.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 process_options(options_to)
  processed_options = {}
  options_to.each do |key, val|
    case key
    when :forcedefault
      processed_options[key] = "y" if val
    when :name, :initials
      processed_options[key] = val if val.present?
    else
      processed_options[key] = val
    end
  end
  processed_options
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