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.

Constant Summary

Constants inherited from Component

Component::ComponentObj

Instance Method Summary collapse

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.

Parameters:

  • props (Hash)

    The properties to customize the Gravatar.

Options Hash (**props):

  • :email (String)

    The email address associated with the Gravatar.

  • :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.

  • :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.



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: 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)]
  }
  add class: gravatar_styles[:base], to: @props
  @email = @props.delete(:email)
  src = gravatar_url
  super(src: src, **@props)
end

Instance Method Details

#gravatar_abbreviationsObject



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_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.



47
48
49
# File 'app/components/fluxbit/gravatar_component.rb', line 47

def gravatar_id
  Digest::MD5.hexdigest(@email.to_s.downcase)
end

#gravatar_urlObject

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(process_options(@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 process_options(options_to)
  processed_options = {}
  options_to.each do |key, val|
    case key
    when :forcedefault
      processed_options[key] = "y" if val
    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).



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