Permalink
Browse files

Added implementation for Facebook module; Updated README; Updated Rak…

…efile
  • Loading branch information...
1 parent f6a3b66 commit f8da7e068b5f01d1567075a3e93aced85272db38 @jurisgalang committed Nov 28, 2009
Showing with 65 additions and 3 deletions.
  1. +3 −1 README.rdoc
  2. +5 −2 Rakefile
  3. +57 −0 lib/facebook.rb
View
@@ -1,6 +1,8 @@
= facebook
-Description goes here.
+This gem is a minimalist implementation of the Facebook API (http://wiki.developers.facebook.com/index.php/API)
+
+Currently, it only supports API calls under the `users` namespace (eg: `users.getInfo`, `users.getLoggedInUser`, etc.)
== Note on Patches/Pull Requests
View
@@ -5,8 +5,11 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "facebook"
- gem.summary = %Q{Minimalist gem implemeting the Facebook API}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{Minimalist gem implementing the Facebook API}
+ gem.description = %Q{
+ This gem is a minimalist implementation of the Facebook API (http://wiki.developers.facebook.com/index.php/API)
+ Currently, it only supports API calls under the `users` namespace (eg: `users.getInfo`, `users.getLoggedInUser`, etc.)
+ }
gem.email = "jurisgalang@gmail.com"
gem.homepage = "http://github.com/jurisgalang/facebook"
gem.authors = ["Juris Galang"]
View
@@ -0,0 +1,57 @@
+require 'digest/md5'
+require 'json'
+require "uri"
+require "net/http"
+
+# Quick and dirty facebook api wrapper
+module Facebook
+ class API
+ def initialize(apikey, secretkey)
+ @apikey = apikey
+ @secretkey = secretkey
+ end
+
+ protected
+ SUPPORTED_METHODS = %w{ users }
+
+ def method_missing(sym, *args)
+ if @pseudo.nil? and SUPPORTED_METHODS.include? sym.to_s
+ @pseudo = "#{sym}."
+ return self
+ elsif args.first.instance_of?(Hash) and @pseudo =~ /\.$/
+ action = "#{@pseudo}#{sym}"
+ params = args.first
+ @pseudo = nil
+ return post(action, params)
+ end
+ super(sym, *args)
+ end
+
+ private
+ def post(action, params)
+ params.merge!('method' => action,
+ 'api_key' => @apikey,
+ 'call_id' => Time.now.to_i,
+ 'v' => '1.0',
+ 'format' => 'JSON')
+ params['sig'] = API::sig(@secretkey, params)
+ result = JSON::parse("{\"data\": #{API::post(params)}}")
+ data = result['data']
+ data = data.first if data.instance_of?(Array)
+ data = data.inject({}){ |data, (key, value)| data["#{key}".to_sym] = value; data } if data.instance_of?(Hash)
+ data
+ end
+
+ def self.sig(key, params)
+ sig = ''
+ params.keys.sort.each { |k| sig << "#{k}=#{params[k]}" }
+ Digest::MD5.hexdigest(sig + key)
+ end
+
+ def self.post(params)
+ uri = URI.parse('http://api.facebook.com/restserver.php')
+ response = Net::HTTP.post_form(uri, params)
+ response.body
+ end
+ end
+end

0 comments on commit f8da7e0

Please sign in to comment.