Module: Fluxbit::Gravatar

Defined in:
lib/fluxbit/gravatar.rb

Overview

Standalone Gravatar URL generator.

Can be used independently of the component:

Fluxbit::Gravatar.url(email: "user@example.com")
Fluxbit::Gravatar.url(email: "user@example.com", size: :lg, rating: :g)

Or via the helper in views:

fx_gravatar_url(email: "user@example.com")

From: github.com/chrislloyd/gravtastic/blob/master/lib/gravtastic.rb chrislloyd.github.io/gravtastic/

Constant Summary collapse

ABBREVIATIONS =
{
  size: "s",
  default: "d",
  rating: "r",
  forcedefault: "f"
}.freeze

Class Method Summary collapse

Class Method Details

.gravatar_id(email) ⇒ String

Returns the MD5 hash of the email, which Gravatar uses as identifier.

Parameters:

  • email (String)

Returns:

  • (String)


59
60
61
# File 'lib/fluxbit/gravatar.rb', line 59

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

.url(email:, rating: nil, secure: true, filetype: nil, default: nil, size: nil, name: nil, initials: nil) ⇒ String

Generates a Gravatar URL for the given email.

Parameters:

  • email (String)

    The email address.

  • rating (Symbol) (defaults to: nil)

    Gravatar rating (:g, :pg, :r, :x).

  • secure (Boolean) (defaults to: true)

    Use HTTPS (default: true).

  • filetype (Symbol) (defaults to: nil)

    Image format (:png, :jpg, :gif, etc.).

  • default (Symbol) (defaults to: nil)

    Default image style (:identicon, :robohash, :mp, etc.).

  • size (Symbol, Integer) (defaults to: nil)

    Size as a symbol (:xs, :sm, :md, :lg, :xl) or pixel integer.

  • name (String) (defaults to: nil)

    Optional name param appended to URL.

  • initials (String) (defaults to: nil)

    Optional initials param appended to URL.

Returns:

  • (String)

    The full Gravatar URL.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fluxbit/gravatar.rb', line 40

def url(email:, rating: nil, secure: true, filetype: nil, default: nil, size: nil, name: nil, initials: nil)
  styles = config.gravatar_styles

  rating   = resolve_option(rating,   collection: styles[:rating],   fallback: config.rating)
  filetype = resolve_option(filetype, collection: styles[:filetype], fallback: config.filetype)
  default  = resolve_option(default,  collection: styles[:default],  fallback: config.default)
  size_px  = resolve_size(size, styles[:size])

  params = { rating: rating, default: default, size: size_px }
  params[:name] = name if name.present?
  params[:initials] = initials if initials.present?

  hostname(secure) + filename(email, filetype) + "?" + to_query(process_options(params))
end