by Cardinal Blue ( cardinalblue.com )
A super simple Facebook Open Graph API client
-
Simple Graph API call
-
Simple FQL call
-
Utility to extract access_token and check sig in cookies
require 'rest-graph' # Every option is optional. rg = RestGraph.new(:access_token => 'tok', :graph_server => 'https://graph.facebook.com/', :old_server => 'https://api.facebook.com/', :accept => 'text/javascript', :lang => 'en-us', # this affect search :auto_decode => true , # decode by json :app_id => '123' , :secret => '1829' , # This handler callback is only called if auto_decode is set to true, # otherwise, it's ignored. :error_handler => lambda{ |hash| raise ::RestGraph::Error.new(hash) }, # You might want to do this in Rails to do debug logging: :log_handler => lambda{ |duration, url| Rails.logger.debug("RestGraph " \ "spent #{duration} " \ "requesting #{url}") }) # API call: rg.get('me') # GET https://graph.facebook.com/me?access_token=tok rg.get('4/likes') # GET https://graph.facebook.com/4/likes?access_token=tok # GET https://graph.facebook.com/search?q=taiwan&access_token=tok rg.get('search', :q => 'taiwan') # GET https://graph.facebook.com/me?metadata=1&access_token=tok rg.get('me', :metadata => '1') # POST https://graph.facebook.com/me/feed?message=bread%21&access_token=tok rg.post('me/feed', :message => 'bread!') # For fully blown cookies hash rg.parse_cookies!(cookies) # auto save access_token if sig checked rg.data['uid'] # => facebook uid # OAuth utilites: # https://graph.facebook.com/oauth/authorize?client_id=123& # redirect_uri=http%3A%2F%2Fw3.org%2F rg.authorize_url(:redirect_uri => 'http://w3.org/') # Get access token by: # https://graph.facebook.com/oauth/access_token?code=edoc& # client_id=123&redirect_uri=http%3A%2F%2Fw3.org%2F& # client_secret=1829 rg.authorize!(:redirect_uri => 'http://w3.org/', :code => 'edoc') rg.access_token # your access_token is now available rg.data['expires'] # other values as well # Exchange old session key for access token: # https://graph.facebook.com/oauth/exchange_sessions?sessions=sess params[:fb_sig_session_key] # => sess rg.exchange_sessions(:sessions => params[:fb_sig_session_key]) # FQL query, same as: # GET https://api.facebook.com/method/fql.query?query= # SELECT+name+FROM+page+WHERE+page_id%3D%22123%22& # format=json&access_token=tok rg.fql('SELECT name FROM page WHERE page_id="123"') # FQL multiquery, same as: # GET https://api.facebook.com/method/fql.multiquery?query= # %7BSELECT+name+FROM+page+WHERE+page_id%3D%22123%22&%2C # SELECT+name+FROM+page+WHERE+page_id%3D%22456%22&%7D # format=json&access_token=tok rg.fql_multi(:q1 => 'SELECT name FROM page WHERE page_id="123"', :q2 => 'SELECT name FROM page WHERE page_id="456"') # Call Facebook's old REST API: rg.old_rest( 'stream.publish', { :message => 'Greetings', :attachment => {:name => 'Wikipedia', :href => 'http://wikipedia.org/', :caption => 'Wikipedia says hi.', :media => [{:type => 'image', :src => 'http://wikipedia.org/favicon.ico', :href => 'http://wikipedia.org/'}] }.to_json, :action_links => [{:text => 'Go to Wikipedia', :href => 'http://wikipedia.org/'} ].to_json }, :suppress_decode => true) # Setup default settings: module MyDefaults def default_app_id '456' end def default_secret 'category theory' end end RestGraph.send(:extend, MyDefaults) # Or load defaults from a config file: require 'rest-graph/load_config' RestGraph::LoadConfig.load_config!('path/to/rest-graph.yaml', 'development') RestGraph.new # app_id would be 456 RestGraph.new(:app_id => '123') # default could be overridden # Or load config automatically require 'rest-graph/auto_load' # under Rails, load config/rest-graph.yaml # Please read:
# for an example of config file. # You might want to do redirect instead of raising an exception # when encountering an authorization problem, that is automatically # redirect the user to authorization page if the access token is # unavailable. This way, you don't have to check if the token is # expired or not. If the token is expired, it will automatically # do authorization again. For that purpose, you might want to # include RestGraph::RailsUtil in your Rails' controller. For example: class UserController < ApplicationController include RestGraph::RailsUtil before_filter :filter_rest_graph_setup private def filter_rest_graph_setup rest_graph_setup(:auto_authorize => true) end end # Please read:
# for more detail, and other frameworks utils wanted!
-
Tested with MRI 1.8.7 and 1.9.1
-
gem install rest-client
-
gem install json (optional)
-
gem install json_pure (optional)
-
gem install rack (optional, to parse access_token in HTTP_COOKIE)
> gem install rest-graph # or if you want rails plugin and bleeding edge > script/plugin install git://github.com/cardinalblue/rest-graph.git
Apache License 2.0 Copyright (c) 2010, Cardinal Blue Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.