Skip to content

Instantly share code, notes, and snippets.

@jubishop
Created July 8, 2009 00:28
Show Gist options
  • Save jubishop/142485 to your computer and use it in GitHub Desktop.
Save jubishop/142485 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'digest/md5'
require 'json'
require 'net/http'
class Facebook
public
# This is it.
def self.callMethod(methodName, args={})
args ||= {}
args.each_pair {|key, value| args[key] = jsonify(value) }
args['v'] = '1.0'
args['format'] = 'JSON'
args['method'] = methodName
args['api_key'] = @@apiKey
args['session_key'] = @@sessionKey
args['call_id'] = (@@call_id += 1)
args['ss'] = true
hashString = args.keys.sort.map{|key|
"#{key}=#{args[key]}"}.push(@@sessionSecret).join
args['sig'] = Digest::MD5.hexdigest(hashString)
res = Net::HTTP.post_form(
URI.parse('http://api.facebook.com/restserver.php'),
args)
JSON.parse(res.body.to_s)
end
@@apiKey = '39f0aec9479177cddacef90da714b037'
@@sessionKey = '2.xGrvqiHmJ3b_DKX2LmGjgA__.86400.1247101200-688626964'
@@sessionSecret = 'O0iifghAyr_Ylb5D9FCDWg__'
@@call_id = Time.now.tv_sec
private
def self.jsonify(item)
if (item.class == Hash)
item.each_pair {|key, value|
item[key] = jsonify(value)
}
item.to_json
elsif (item.class == Array)
item.map{|entry| jsonify(entry)}.to_json
else
item
end
end
end
# Some examples...
# Get all my friend ids
friend_ids = Facebook::callMethod("friends.get")
# Get some info about my friends, and print it out
friend_data = Facebook::callMethod("fql.query", {
'query' => "select name, status from user " +
"where uid in (#{friend_ids.join(',')})"
})
friend_data.each {|friend|
puts "Name: #{friend['name']}"
# Avoid NIL error when there's no status ever set, ever.
if (friend['status'])
puts "Status: #{friend['status']['message']}"
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment