Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Last active August 29, 2015 14:10
Show Gist options
  • Save KamilLelonek/1cf37b10e9dadf1b7043 to your computer and use it in GitHub Desktop.
Save KamilLelonek/1cf37b10e9dadf1b7043 to your computer and use it in GitHub Desktop.
GravatarHelper with module_function usage
require 'digest/md5'
module GravatarHelper
module_function
def gravatar_for_email(email)
"#{gravatar_endpoint}#{hash_for_email(email)}"
end
def hash_for_email(email)
Digest::MD5.hexdigest(email)
end
def gravatar_endpoint
'http://www.gravatar.com/avatar/'
end
end
[0] (pry) main: 0> user = User.new('[email protected]');
[1] (pry) main: 0> GravatarHelper.gravatar_for_email(user.email)
=> 'http://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af'
[2] (pry) main: 0> user.gravatar
=> 'http://www.gravatar.com/avatar/b58996c504c5638798eb6b511e6f49af'
class User
include GravatarHelper
attr_reader :email
def initialize(email)
@email = email
end
def gravatar
gravatar_for_email @email
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment