How should i check if gravatar exist?
Asked Answered
M

3

10

using this gem https://github.com/sinisterchipmunk/gravatar how do i check if gravatar for specified email exist or not? I think i am missing some force default option? Because this

url = Gravatar.new("[email protected]").image_url

always return a picture

Midwinter answered 20/7, 2012 at 15:29 Comment(2)
Have you tried with an invalid email address other than [email protected] ?Gyronny
yeah, i tried, same result :-(Midwinter
I
8

Looking at that gem's documentation, it sounds like you need an API key before you can run the exists method:

Fine, but how about the rest of the API as advertised at en.gravatar.com/site/implement/xmlrpc? Well, for that you need either the user’s Gravatar password, or their API key:

api = Gravatar.new("[email protected]", :api_key => "AbCdEfG1234")

api.exists?("[email protected]") #=> true or false, depending on whether the specified email exists.

If the user doesn't have a gravatar, an image will be generated for them based on the email (at least that has been my experience). I've used the gem gravatar_image_tag - http://rubygems.org/gems/gravatar_image_tag which lets you change the default gravatar image.

Inherit answered 20/7, 2012 at 16:47 Comment(2)
This is maybe another approach to try: #2104120Inherit
well, i just did not catch the politics gem vs gravatar. As in written in gravatar documentation u can easy pass d= parameter to an image request: 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response. That i was looking for in the mentioned gemMidwinter
G
46

In case people would like to know how to do it without any gems :

The trick is to get gravatar image with a false default image and then check header response. It's achieved with the Net::HTTP ruby library.

require 'net/http'

def gravatar?(user)
    gravatar_check = "http://gravatar.com/avatar/#{Digest::MD5.hexdigest(user.gravatar_email.downcase)}.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 # from d=404 parameter
end
Gametogenesis answered 22/6, 2013 at 11:45 Comment(6)
But if email doesn't exists, it doesn't reply with 404, but with the default gravatar, so how could you return false?Chaco
Yes, that's exactly what allows you to get an error if there is no gravatar, because you provide a false default picture.Knap
@ Noémien Kocher This works great for me on localhost, but when I deploy to Heroku, I get the error 'uninitialized constant Net::HTTP' Does anyone know why?Blaubok
@TomD did you try to add "require 'net/http'" at the top of your file ?Knap
@ Noémien Kocher. No, I hadn't done that, and adding it as you suggest resolved the issue. Many thanks!Blaubok
also start with require 'digest'Aldas
I
8

Looking at that gem's documentation, it sounds like you need an API key before you can run the exists method:

Fine, but how about the rest of the API as advertised at en.gravatar.com/site/implement/xmlrpc? Well, for that you need either the user’s Gravatar password, or their API key:

api = Gravatar.new("[email protected]", :api_key => "AbCdEfG1234")

api.exists?("[email protected]") #=> true or false, depending on whether the specified email exists.

If the user doesn't have a gravatar, an image will be generated for them based on the email (at least that has been my experience). I've used the gem gravatar_image_tag - http://rubygems.org/gems/gravatar_image_tag which lets you change the default gravatar image.

Inherit answered 20/7, 2012 at 16:47 Comment(2)
This is maybe another approach to try: #2104120Inherit
well, i just did not catch the politics gem vs gravatar. As in written in gravatar documentation u can easy pass d= parameter to an image request: 404: do not load any image if none is associated with the email hash, instead return an HTTP 404 (File Not Found) response. That i was looking for in the mentioned gemMidwinter
L
1
if (response.code.to_i == 404)
  return false
else
  return true
end

Instead of whole block, use only this(as a last line):

response.code.to_i != 404
Lipfert answered 8/1, 2014 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.