Skip to content
Browse files

Initial import

  • Loading branch information...
0 parents commit 3201731e418ae68beb528c3074b76da6d69e519c @jfarmer committed
Showing with 177 additions and 0 deletions.
  1. 0 README
  2. +24 −0 facebook-graphclient.gemspec
  3. +94 −0 lib/facebook-graphclient.rb
  4. +59 −0 lib/facebook-graphclient/rails.rb
0 README
No changes.
24 facebook-graphclient.gemspec
@@ -0,0 +1,24 @@
+spec = Gem::Specification.new do |s|
+ s.name = 'facebook-graphclient'
+ s.version = '0.1'
+ s.date = '2010-04-24'
+ s.summary = "A simple library for Facebook's new Graph API"
+ s.description = "A bare-bones implementation of Facebook's new Graph API"
+
+ s.homepage = "http://github.com/jessefarmer/facebook-graphclient"
+
+ s.authors = ["Jesse Farmer"]
+ s.email = "jesse@20bits.com"
+
+ s.add_dependency('yajl-ruby')
+ s.add_dependency('patron')
+
+ # TODO Remove this dependency
+ s.add_dependency('rack')
+ s.has_rdoc = false
+
+ s.files = ["README",
+ "facebook-graphclient.gemspec",
+ "lib/facebook-graphclient.rb",
+ "lib/facebook-graphclient/rails.rb"]
+end
94 lib/facebook-graphclient.rb
@@ -0,0 +1,94 @@
+class FacebookError < StandardError
+ attr_accessor :data
+end
+
+require 'digest/md5'
+require 'yajl'
+require 'patron'
+
+module Facebook
+ GRAPH_URL = 'http://graph.facebook.com'
+
+ class GraphClient
+
+ attr_reader :secret, :app_id
+ attr_accessor :access_token
+
+ def initialize facebook_settings = {}
+ @app_id = facebook_settings[:app_id]
+ @secret = facebook_settings[:secret]
+
+ @cookie = get_user_cookie facebook_settings[:cookies]
+
+ @access_token = facebook_settings[:access_token] || @cookie['access_token']
+
+ @session = Patron::Session.new
+ end
+
+ def get_user_cookie cookies
+ if cookies and cookie = cookies["fbs_#{@app_id}"]
+ Rack::Utils.parse_nested_query(cookie)
+ else
+ {}
+ end
+ end
+
+ %w[get post delete].each do |action|
+ class_eval %[
+ def #{action} method, params = {}
+ self.api '#{action}', method, params
+ end
+ ]
+ end
+
+ def api action, method, query_params = nil
+ query_params[:access_token] ||= @access_token
+
+ query_string = '?' + query_params.map { |k,v| "#{k}=#{v}" }.join("&") unless query_params.empty?
+
+ raw_response = @session.send(action, GRAPH_URL + method + query_string)
+
+ # TODO: Handle photo requests, which return photo data and not JSON
+
+ if raw_response.headers['Content-Type'] =~ /text\/javascript/
+ # We have JSON
+ response = Yajl::Parser.parse(raw_response.body)
+
+ if e = response['error']
+ error = FacebookError.new(e['message'])
+ error.data = e
+ raise error
+ else
+ response
+ end
+ end
+ end
+
+ def valid?
+ return false if @cookie.nil?
+
+ unless @is_valid
+ vars = @cookie.dup
+
+ good_sig = vars.delete 'sig'
+ sig = Digest::MD5.hexdigest(vars.sort.map { |k,v| "#{k}=#{v}" }.compact.join + @secret)
+
+ if @is_valid = (sig == good_sig)
+ @params = vars
+ else
+ @params = {}
+ end
+ end
+
+ @params
+ end
+
+ def [] k
+ params[k]
+ end
+
+ def params
+ valid? ? @params : {}
+ end
+ end
+end
59 lib/facebook-graphclient/rails.rb
@@ -0,0 +1,59 @@
+begin
+ require 'action_controller'
+rescue LoadError
+ retry if require 'rubygems'
+ raise
+end
+
+require 'facebook-graphclient'
+
+module Facebook
+ module RailsFacebookSettings
+ def self.extended(klass)
+ klass.cattr_accessor :facebook_settings
+ klass.facebook_settings = {}
+ end
+ def facebook(&blk)
+ instance_eval(&blk)
+ include Facebook::Rails
+ end
+ %w[ secret app_id access_token ].each do |param|
+ class_eval %[
+ def #{param} val, &blk
+ facebook_settings[:#{param}] = val
+ end
+ ]
+ end
+ end
+
+ class RailsFacebookResponse
+ def initialize(app)
+ @app = app
+ end
+ def redirect(url)
+ @app.redirect_to(url)
+ end
+ def body(msg)
+ @app.render :text => msg
+ end
+ end
+
+ module Rails
+ def self.included(controller)
+ if controller.respond_to?(:helper_method)
+ controller.helper_method :fb, :facebook
+ end
+ end
+ def facebook
+ unless request.env['facebook.helper']
+ fb = Facebook::GraphClient.new(self.class.facebook_settings.merge(:cookies => request.cookies))
+ env['facebook.helper'] = fb
+ end
+
+ env['facebook.helper']
+ end
+ alias fb facebook
+ end
+end
+
+ActionController::Base.extend Facebook::RailsFacebookSettings

0 comments on commit 3201731

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