Skip to content

Instantly share code, notes, and snippets.

@neves
Last active July 23, 2019 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neves/5287aeefc12431a706fa7d59a5fcffb4 to your computer and use it in GitHub Desktop.
Save neves/5287aeefc12431a706fa7d59a5fcffb4 to your computer and use it in GitHub Desktop.
functional ruby
# frozen_string_literal: true
# original: https://github.com/gitlabhq/gitlabhq/blob/master/app/services/gravatar_service.rb
# Esse módulo pode ser chamado de 3 formas diferentes:
# GravatarService.call('j.doe@gmail.com')
# GravatarService.('j.doe@gmail.com')
# GravatarService['j.doe@gmail.com'] # eu prefiro essa última
module GravatarService
extend self
def call(email, size: nil, scale: 2, username: nil)
return unless Gitlab::CurrentSettings.gravatar_enabled?
identifier = email.presence || username.presence
return unless identifier
hash = Digest::MD5.hexdigest(identifier.strip.downcase)
size = 40 unless size && size > 0
sprintf gravatar_url,
hash: hash,
size: size * scale,
email: ERB::Util.url_encode(email&.strip || ''),
username: ERB::Util.url_encode(username&.strip || '')
end
alias [] call
def gitlab_config
Gitlab.config.gitlab
end
def gravatar_config
Gitlab.config.gravatar
end
def gravatar_url
if gitlab_config.https
gravatar_config.ssl_url
else
gravatar_config.plain_url
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment