How can I check if gravatar exists?

using this stone https://github.com/sinisterchipmunk/gravatar how can I check if gravatar exists for the specified email or not? I think that I do not have the option of forced default? Because it

url = Gravatar.new(" generic@example.com ").image_url 

always return image

+7
source share
3 answers

Looking at this gemstone documentation, it seems like you need an API key before you can run the exist method:

Ok, but what about the rest of the API, as advertised on en.gravatar.com/site/implement/xmlrpc? To do this, you need either the Gravatar user password or its API key:

api = Gravatar.new (" generic@example.com ",: api_key => "AbCdEfG1234")

api.exists? (" another@example.com ") # => true or false, depending on whether the specified message exists.

If the user does not have a gravitator, an image will be created for them based on email (at least that was my experience). I used gem gravatar_image_tag - http://rubygems.org/gems/gravatar_image_tag , which allows you to change the default gravatar image.

+8
source

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

The trick is to get the gravity image with the default false image, and then check the response of the header. This was achieved using the Ruby Net :: HTTP 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 
+38
source
 if (response.code.to_i == 404) return false else return true end 

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

 response.code.to_i != 404 
+1
source

All Articles