Skip to content

Instantly share code, notes, and snippets.

@itay-grudev
Last active April 16, 2017 02:43
Show Gist options
  • Save itay-grudev/06015c582dd744496ed4 to your computer and use it in GitHub Desktop.
Save itay-grudev/06015c582dd744496ed4 to your computer and use it in GitHub Desktop.
Simple Gravatar Rails Helper
# Copyright (c) 2016-2017 Itay Grudev
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'digest/md5'
module GravatarHelper
GRAVATAR_API_URL = 'http://gravatar.com/avatar/'
GRAVATAR_SECURE_API_URL = 'https://secure.gravatar.com/avatar/'
def gravatar_url( email, options = { } )
options = {
secure: true,
size: 160,
default: 'mm',
rating: :g,
force: false
}.merge( options )
if options[:secure]
url = GRAVATAR_SECURE_API_URL
else
url = GRAVATAR_API_URL
end
url += Digest::MD5.hexdigest( email )
url += '?'
url += "s=#{options[:size]}&" if options[:size] != 80
url += "d=#{options[:default]}&" if options[:default] != nil
url += "r=#{options[:rating]}&" if options[:rating] != :g
url += 'f=y&' if options[:force]
url[0...-1] # Removes the last charecter (an extra ? or &)
end
end
<img src="<%= gravatar_url(current_user.email, secure: true, default: :mm) %>">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment