Ruby Gravatar Link generator

Just a simple example of Ruby code to generate a gravatar link, substitute the xxxx@gmail.com with the desired address and you will get the something like:

http://www.gravatar.com/avatar/d02770a3574d9ed0ee364c49d5fb14d3?

In the second use case you can specify the size of the gravatar, the default image in case no gravatar is found and the rating:

http://www.gravatar.com/avatar/d02770a3574d9ed0ee364c49d5fb14d3?size=200&default=https%3A%2F%2Fimage-not-found-link.jpg&rating=pg

 

#!/usr/bin/env ruby
require 'digest/md5'
require 'uri'
 
# this is based on gravatar image API
# https://en.gravatar.com/site/implement/images/
# options :
# size : <integer> size of image
# default: <string> url of image if email not found or:
# * 404
# * mm
# * identicon
# * monsterid
# * wavatar
# * retro
# * blank
# forcedefault: "y" force default image to load
# rating: <string> one of the values : g, pg, r, x
def gravatar email, options={}
  email_md5 = Digest::MD5.hexdigest email
  params_query = URI.encode_www_form(options) unless options.empty?
  "http://www.gravatar.com/avatar/#{email_md5}?#{params_query}"
end
 
 
puts gravatar('xxxx@gmail.com')
puts gravatar('xxxx@gmail.com',
              size: 200,
              default: '404',
              rating: 'pg'
     )
Tagged , ,