Skip to content

Instantly share code, notes, and snippets.

@Mic92
Created September 27, 2012 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mic92/3792876 to your computer and use it in GitHub Desktop.
Save Mic92/3792876 to your computer and use it in GitHub Desktop.
Check if facebook user has uploaded a profile picture or uses the default in ruby
require 'net/http'
# idea: default profile photos has the Content-Type image/gif
# read this: http://stackoverflow.com/questions/5555199
def default_facebook_photo?(facebook_uid)
resp = fetch("https://graph.facebook.com/#{facebook_uid}/picture")
resp['content-type'] == 'image/gif'
end
# http head request which follows redirects
def fetch(uri, limit = 10)
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
url = URI.parse(uri)
req = Net::HTTP::Head.new(url.path)
response = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response['location'], limit - 1)
else
response.error!
end
end
# usage example, if you use omniauth-facebook (https://github.com/mkdynamic/omniauth-facebook)
default_facebook_photo?(omniauth['uid']) # just pass the uid, you got from the omniauth hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment