Created
April 27, 2019 02:45
-
-
Save rodloboz/ae341a99814406475c69c1e2aaf94dc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module AvatarHelper | |
def avatar_url(user) | |
if gravatar?(user) | |
gravatar = Digest::MD5::hexdigest(user.email).downcase | |
"http://gravatar.com/avatar/#{gravatar}.png?s=200" | |
else | |
'default_avatar.png' | |
end | |
end | |
def gravatar?(user) | |
gravatar = Digest::MD5::hexdigest(user.email).downcase | |
gravatar_check = "http://gravatar.com/avatar/#{gravatar}.png?d=404" | |
uri = URI.parse(gravatar_check) | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Get.new(uri.request_uri) | |
response = http.request(request) | |
response.code.to_i != 404 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment