Created
September 27, 2012 08:25
-
-
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
This file contains 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
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