Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…efile
  • Loading branch information...
commit f8da7e068b5f01d1567075a3e93aced85272db38 1 parent f6a3b66
Juris Galang authored November 27, 2009
4  README.rdoc
Source Rendered
... ...
@@ -1,6 +1,8 @@
1 1
 = facebook
2 2
 
3  
-Description goes here.
  3
+This gem is a minimalist implementation of the Facebook API (http://wiki.developers.facebook.com/index.php/API)
  4
+
  5
+Currently, it only supports API calls under the `users` namespace (eg: `users.getInfo`, `users.getLoggedInUser`, etc.)
4 6
 
5 7
 == Note on Patches/Pull Requests
6 8
  
7  Rakefile
@@ -5,8 +5,11 @@ begin
5 5
   require 'jeweler'
6 6
   Jeweler::Tasks.new do |gem|
7 7
     gem.name = "facebook"
8  
-    gem.summary = %Q{Minimalist gem implemeting the Facebook API}
9  
-    gem.description = %Q{TODO: longer description of your gem}
  8
+    gem.summary = %Q{Minimalist gem implementing the Facebook API}
  9
+    gem.description = %Q{
  10
+      This gem is a minimalist implementation of the Facebook API (http://wiki.developers.facebook.com/index.php/API)
  11
+      Currently, it only supports API calls under the `users` namespace (eg: `users.getInfo`, `users.getLoggedInUser`, etc.)
  12
+    }
10 13
     gem.email = "jurisgalang@gmail.com"
11 14
     gem.homepage = "http://github.com/jurisgalang/facebook"
12 15
     gem.authors = ["Juris Galang"]
57  lib/facebook.rb
... ...
@@ -0,0 +1,57 @@
  1
+require 'digest/md5'
  2
+require 'json'
  3
+require "uri"
  4
+require "net/http"
  5
+
  6
+# Quick and dirty facebook api wrapper
  7
+module Facebook
  8
+  class API
  9
+    def initialize(apikey, secretkey)
  10
+      @apikey    = apikey
  11
+      @secretkey = secretkey
  12
+    end
  13
+
  14
+    protected
  15
+    SUPPORTED_METHODS = %w{ users }
  16
+    
  17
+    def method_missing(sym, *args)
  18
+      if @pseudo.nil? and SUPPORTED_METHODS.include? sym.to_s
  19
+        @pseudo = "#{sym}."
  20
+        return self
  21
+      elsif args.first.instance_of?(Hash) and @pseudo =~ /\.$/
  22
+        action  = "#{@pseudo}#{sym}"
  23
+        params  = args.first
  24
+        @pseudo = nil
  25
+        return post(action, params)
  26
+      end
  27
+      super(sym, *args)
  28
+    end
  29
+    
  30
+    private
  31
+    def post(action, params)
  32
+      params.merge!('method'  => action,
  33
+                    'api_key' => @apikey,
  34
+                    'call_id' => Time.now.to_i,
  35
+                    'v'       => '1.0',
  36
+                    'format'  => 'JSON')
  37
+      params['sig'] = API::sig(@secretkey, params)
  38
+      result        = JSON::parse("{\"data\": #{API::post(params)}}")
  39
+      data          = result['data']
  40
+      data          = data.first if data.instance_of?(Array) 
  41
+      data          = data.inject({}){ |data, (key, value)| data["#{key}".to_sym] = value; data } if data.instance_of?(Hash)
  42
+      data
  43
+    end
  44
+
  45
+    def self.sig(key, params)
  46
+      sig = ''
  47
+      params.keys.sort.each { |k| sig << "#{k}=#{params[k]}" }
  48
+      Digest::MD5.hexdigest(sig + key)
  49
+    end
  50
+    
  51
+    def self.post(params)
  52
+      uri      = URI.parse('http://api.facebook.com/restserver.php')
  53
+      response = Net::HTTP.post_form(uri, params)
  54
+      response.body
  55
+    end
  56
+  end
  57
+end

0 notes on commit f8da7e0

Please sign in to comment.
Something went wrong with that request. Please try again.