# Rest-graph 1.3.0 - A super simple Facebook Open Graph API client

**URL:** <https://www.ruby-forum.com/t/rest-graph-1-3-0-a-super-simple-facebook-open-graph-api-client/190016>\
**Category:** Ruby\
**Created:** [June 11, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/rest-graph-1-3-0-a-super-simple-facebook-open-graph-api-client/190016 "2010-06-11T12:37:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lin\_Jen-Shin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7993a0/32.png) [@Lin\_Jen-Shin](https://www.ruby-forum.com/u/Lin_Jen-Shin)\
**Post date:** [June 11, 2010, 12:37pm UTC](https://www.ruby-forum.com/t/rest-graph-1-3-0-a-super-simple-facebook-open-graph-api-client/190016/1 "2010-06-11T12:37:05Z")

</div>

= rest-graph 1.3.0  
by Cardinal Blue ( [http://cardinalblue.com](http://cardinalblue.com) )

== LINKS:

- [GitHub - cardinalblue/rest-graph: A lightweight Facebook Graph API client](http://github.com/cardinalblue/rest-graph)
- [rest-graph | RubyGems.org | your community gem host](http://rubygems.org/gems/rest-graph)
- [http://rdoc.info/projects/cardinalblue/rest-graph](http://rdoc.info/projects/cardinalblue/rest-graph)
- [http://groups.google.com/group/rest-graph/topics](http://groups.google.com/group/rest-graph/topics)

== DESCRIPTION:

A super simple Facebook Open Graph API client

== FEATURES:

- Simple Graph API call
- Simple FQL call
- Utility to extract access\_token and check sig in cookies

== CHANGES in 1.3.0

- Now rest-graph is rescuing all exceptions from rest-client.

- Added RestGraph#exchange\_sessions to exchange old sessions to access  
tokens.

- Added RestGraph#old\_rest, see:  
[http://developers.facebook.com/docs/reference/rest/](http://developers.facebook.com/docs/reference/rest/)

- Now all API request accept an additional options argument,  
you may pass :suppress\_decode =\> true to turn off auto-decode this  
time.  
e.g. rg.get(‘bad/json’, {:query =\> ‘string’}, :suppress\_decode =\>  
true)  
This is for Facebook who didn’t always return JSON in response.

- Renamed fql\_server to old\_server.

- Favor yaji/json\_gem first, then falls back to json, and json\_pure.

- Fixed a bug that cookie format from Facebook varies. No idea why.

for RailsUtil:

- Big and fat refactoring in RailsUtil, see example for detail:  
[http://github.com/cardinalblue/rest-graph/tree/rest-graph-1.3.0/example](http://github.com/cardinalblue/rest-graph/tree/rest-graph-1.3.0/example)
- url\_for and link\_to would auto pass :host option if it’s inside  
canvas.

== SYNOPSIS:

# If you feel SYNOPSIS is so hard to understand, please read

# {examples}[[GitHub - cardinalblue/rest-graph: A lightweight Facebook Graph API client](http://github.com/cardinalblue/rest-graph/tree/master/)

example].

require ‘rest-graph’

# Every option is optional.

rg = RestGraph.new(:access\_token =\> ‘tok’,  
:graph\_server =\> ‘[https://graph.facebook.com/](https://graph.facebook.com/)’,  
:old\_server =\> ‘[https://api.facebook.com/](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}")
                  })

```

# You might want to do redirect instead of raising an exception,

# 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 :rest\_graph\_setup  
end

# Please read:

# {examples}[[GitHub - cardinalblue/rest-graph: A lightweight Facebook Graph API client](http://github.com/cardinalblue/rest-graph/tree/master/)

example].

# for more detail, and other frameworks utils wanted!

# Other simple API call:

rg.get(‘me’) # GET [https://graph.facebook.com/me?access\_token=tok](https://graph.facebook.com/me?access_token=tok)  
rg.get(‘4/likes’) # GET  
[https://graph.facebook.com/4/likes?access\_token=tok](https://graph.facebook.com/4/likes?access_token=tok)

# GET [https://graph.facebook.com/search?q=taiwan&access\_token=tok](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](https://graph.facebook.com/me?metadata=1&access_token=tok)

rg.get(‘me’, :metadata =\> ‘1’)

# POST

[https://graph.facebook.com/me/feed?message=bread!&access\_token=tok](https://graph.facebook.com/me/feed?message=bread%21&access_token=tok)  
rg.post(‘me/feed’, :message =\> ‘bread!’)

# For fully blown cookies hash

rg = RestGraph.new(:app\_id =\> ‘123’, :secret =\> ‘1829’)  
rg.parse\_cookies!(cookies) # auto save access\_token if sig checked  
rg.data[‘uid’] # =\> facebook uid

# FQL query, same as:

# GET [https://api.facebook.com/method/fql.query?query=](https://api.facebook.com/method/fql.query?query=)

# SELECT+name+FROM+page+WHERE+page\_id%3D%22123%22&

# format=json&access\_token=…

rg.fql(‘SELECT name FROM page WHERE page\_id=“123”’)

# FQL multiquery, same as:

# GET [https://api.facebook.com/method/fql.multiquery?query=](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=…

rg.fql\_multi(:q1 =\> ‘SELECT name FROM page WHERE page\_id=“123”’,  
:q2 =\> ‘SELECT name FROM page WHERE page\_id=“456”’)

# Setup default settings:

module MyDefaults  
def default\_app\_id  
‘456’  
end

def default\_secret  
‘category theory’  
end  
end  
RestGraph.send(:extend, MyDefaults)

# Automatically load config:

require ‘rest-graph/auto\_load’ # under Rails, load config/rest-  
graph.yaml  
RestGraph.new # all default options would honor  
config  
RestGraph.new(:app\_id =\> ‘123’) # default could be override as well

# Manually load config:

require ‘rest-graph/load\_config’  
RestGraph::LoadConfig.load\_config!(‘path/to/rest-graph.yaml’, ‘env’)

# See test/config/rest-graph.yaml for an example for config.

# OAuth utilites:

# [https://graph.facebook.com/oauth/authorize?client\_id=123&](https://graph.facebook.com/oauth/authorize?client_id=123&)

RestGraph.new.authorize\_url(:redirect\_uri =\> ‘[http://w3.org/](http://w3.org/)’)

# Get access token by:

# [https://graph.facebook.com/oauth/access\_token?code=edoc&](https://graph.facebook.com/oauth/access_token?code=edoc&)

rg = RestGraph.new  
rg.authorize!(:redirect\_uri =\> ‘[http://w3.org/](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](https://graph.facebook.com/oauth/exchange_sessions?sessions=sess)

rg.exchange\_sessions(:sessions =\> params[:fb\_sig\_session\_key])

# Call Facebook’s old REST API:

rg.old\_rest(  
‘stream.publish’,  
{ :message =\> ‘Greetings’,  
:attachment =\> {:name =\> ‘Wikipedia’,  
:href =\> ‘[http://wikipedia.org/](http://wikipedia.org/)’,  
:caption =\> ‘Wikipedia says hi.’,  
:media =\> [{:type =\> ‘image’,  
:src =\> ‘[http://wikipedia.org/](http://wikipedia.org/)  
favicon.ico’,  
:href =\> ‘[http://wikipedia.org/](http://wikipedia.org/)’}]  
}.to\_json,  
:action\_links =\> [{:text =\> ‘Go to Wikipedia’,  
:href =\> ‘[http://wikipedia.org/](http://wikipedia.org/)’}  
].to\_json  
},  
:suppress\_decode =\> true)

== REQUIREMENTS:

- 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)

== INSTALL:

> gem install rest-graph

# or if you want rails plugin and bleeding edge

> script/plugin install git://github.com/cardinalblue/rest-graph.git

== LICENSE:

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.
