rest-graph 1.4.5 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES CHANGED
@@ -1,5 +1,36 @@
1
1
  = rest-graph changes history
2
2
 
3
+ == rest-graph 1.4.6 -- 2010-09-01
4
+
5
+ * [RestGraph] Now it will try to pick yajl-ruby or json gem from memory first,
6
+ if it's not there, then try to load one and try to pick one
7
+ again. This way, it won't force you to load two gems at the
8
+ same time if you've installed them both. In addition, there's
9
+ a bug in yajl/json_gem pointed out at:
10
+ http://github.com/brianmario/yajl-ruby/issues/31
11
+ So we're using Nicolas' patch to use yajl directly to workaround
12
+ this issue when we've chosen yajl-ruby json backend.
13
+
14
+ * [RestGraph] Only cache GET request, don't cache POST/PUT/DELETE
15
+
16
+ * [RestGrahp] Add RestGraph#lighten and RestGraph#lighten! to remove any
17
+ handler and cache object to make it serializable.
18
+
19
+ * [RailsUtil] Add ensure_authorized option which enforces the user has
20
+ authorized to the application.
21
+
22
+ * [RailsUtil] Unified rest_graph_storage_key, which used in cookies/session
23
+ storage, and the key would depend on app_id, just like Facebook
24
+ JavaScript SDK which use fbs_[app_id] as the name of cookie.
25
+ This way, you are able to run different applications with
26
+ different permissions in one Rails application.
27
+
28
+ * [RailsUtil] Now rest_graph_authorize defaults to do redirect.
29
+ Previously, you'll need to use:
30
+ `rest_graph_authorize(message, true)`
31
+ Now it's:
32
+ `rest_graph_authorize(message)`
33
+
3
34
  == rest-graph 1.4.5 -- 2010-08-07
4
35
 
5
36
  * [RestGraph] Treat oauth_token as access_token as well. This came from
data/CONTRIBUTORS ADDED
@@ -0,0 +1,7 @@
1
+ Lin Jen-Shin (godfat)
2
+ Jaime Cham (jcham)
3
+ Andrew Liu (eggegg)
4
+ John Fan (johnfan)
5
+ Florent Vaucelle (florent)
6
+ Ethan Czahor (ethanz5)
7
+ Nicolas Fouché (nfo)
data/Gemfile CHANGED
@@ -5,6 +5,9 @@ gem 'rest-client'
5
5
 
6
6
  group :test do
7
7
  gem 'yajl-ruby'
8
+ gem 'json'
9
+ gem 'json_pure'
10
+
8
11
  gem 'rack'
9
12
  gem 'rr'
10
13
  gem 'webmock'
data/Gemfile.lock CHANGED
@@ -1,49 +1,30 @@
1
- ---
2
- hash: 1abfe240bd774ad21e7a187351da9cabacb74d58
3
- sources:
4
- - Rubygems:
5
- uri: http://rubygems.org
6
- specs:
7
- - addressable:
8
- version: 2.1.2
9
- - bacon:
10
- version: 1.1.0
11
- - crack:
12
- version: 0.1.8
13
- - mime-types:
14
- version: "1.16"
15
- - rack:
16
- version: 1.2.1
17
- - rest-client:
18
- version: 1.6.0
19
- - rr:
20
- version: 0.10.11
21
- - webmock:
22
- version: 1.3.2
23
- - yajl-ruby:
24
- version: 0.7.7
25
- dependencies:
26
- rest-client:
27
- version: ">= 0"
28
- group:
29
- - :default
30
- yajl-ruby:
31
- version: ">= 0"
32
- group:
33
- - :test
34
- rack:
35
- version: ">= 0"
36
- group:
37
- - :test
38
- rr:
39
- version: ">= 0"
40
- group:
41
- - :test
42
- webmock:
43
- version: ">= 0"
44
- group:
45
- - :test
46
- bacon:
47
- version: ">= 0"
48
- group:
49
- - :test
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.0)
5
+ bacon (1.1.0)
6
+ crack (0.1.8)
7
+ json (1.4.6)
8
+ json_pure (1.4.6)
9
+ mime-types (1.16)
10
+ rack (1.2.1)
11
+ rest-client (1.6.1)
12
+ mime-types (>= 1.16)
13
+ rr (1.0.0)
14
+ webmock (1.3.4)
15
+ addressable (>= 2.1.1)
16
+ crack (>= 0.1.7)
17
+ yajl-ruby (0.7.7)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ bacon
24
+ json
25
+ json_pure
26
+ rack
27
+ rest-client
28
+ rr
29
+ webmock
30
+ yajl-ruby
data/README CHANGED
@@ -1,4 +1,4 @@
1
- = rest-graph 1.4.5
1
+ = rest-graph 1.4.6
2
2
  by Cardinal Blue ( http://cardinalblue.com )
3
3
 
4
4
  == LINKS:
@@ -10,7 +10,7 @@ by Cardinal Blue ( http://cardinalblue.com )
10
10
 
11
11
  == DESCRIPTION:
12
12
 
13
- A super simple Facebook Open Graph API client
13
+ A super simple Facebook Open Graph API client
14
14
 
15
15
  == FEATURES:
16
16
 
@@ -18,162 +18,209 @@ by Cardinal Blue ( http://cardinalblue.com )
18
18
  * Simple FQL call
19
19
  * Utility to extract access_token and check sig in cookies
20
20
 
21
- == QUICK START:
21
+ == REQUIREMENTS:
22
22
 
23
- # In typical use, here's how you use RestGraph. Note that the syntax follows
24
- # closely to the Graph API URL syntax, making it easy to use. First, suppose
25
- # that you already have an access_token, represented by TOKEN:
23
+ * Tested with MRI 1.8.7 and 1.9.2 and Rubinius HEAD
24
+ * gem install rest-client
25
+ * gem install json (optional)
26
+ * gem install json_pure (optional)
27
+ * gem install rack (optional, to parse access_token in HTTP_COOKIE)
26
28
 
27
- require 'rest-graph'
28
- rg = RestGraph.new(:access_token => TOKEN)
29
+ == INSTALL:
29
30
 
30
- # GET https://graph.facebook.com/me?access_token=TOKEN
31
- rg.get('me')
31
+ gem install rest-graph
32
32
 
33
- # GET https://graph.facebook.com/me/likes?access_token=TOKEN
34
- rg.get('me/likes')
33
+ or if you want rails plugin and bleeding edge
34
+
35
+ script/plugin install git://github.com/cardinalblue/rest-graph.git
35
36
 
36
- # GET https://graph.facebook.com/search?q=taiwan&access_token=TOKEN
37
+ == QUICK START:
38
+
39
+ require 'rest-graph'
40
+ rg = RestGraph.new(:access_token => 'myaccesstokenfromfb')
41
+ rg.get('me')
42
+ rg.get('me/likes')
37
43
  rg.get('search', :q => 'taiwan')
38
44
 
45
+ === Obtaining an access token
39
46
 
40
- # Next, we explain how to use RestGraph to obtain the access token
47
+ If you are using Rails, we recommend that you include a module called
48
+ RestGraph::RailsUtil into your Controllers. (Your code contributions
49
+ for other Ruby frameworks would be appreciated!). RestGraph::RailsUtil
50
+ adds the following two methods to your Controllers:
41
51
 
42
- # If you are using Rails, we recommend that you include a module
43
- # called RailsUtil into your controllers, which will configure RestGraph.
44
- # (Your code contributions for other Ruby frameworks would be appreciated!)
45
- # There is an option in RailsUtil called "auto_authorize" which will cause
46
- # RestGraph to automatically redirect the user to the authorization page if
47
- # the access token is unavailable or has expired. (This way, you don't have
48
- # to check if the token is expired or not.)
52
+ rest_graph_setup: Attempts to find an access_token from the environment
53
+ and initializes a RestGraph object with it.
54
+ Most commonly used inside a filter.
49
55
 
50
- # Here is an example:
56
+ rest_graph: Accesses the RestGraph object by rest_graph_setup.
51
57
 
52
- class UserController < ApplicationController
53
- include RestGraph::RailsUtil
54
- before_filter :filter_rest_graph_setup
58
+ === Example usage:
55
59
 
56
- def index
57
- # rest_graph_setup provides rest_graph as a RestGraph instance
58
- @profile = rest_graph.get('me')
60
+ class MyController
61
+ include RestGraph::RailsUtil
62
+ before_filter do
63
+ rest_graph_setup(:app_id => '123',
64
+ :canvas => 'mycanvas',
65
+ :auto_authorize_scope => 'email')
66
+ # See below for more options
59
67
  end
60
68
 
61
- # your code
62
-
63
- private
64
- def filter_rest_graph_setup
65
- # Please see RestGraph::RailsUtil#rest_graph_options for all options.
66
- rest_graph_setup(:auto_authorize_scope => 'publish_stream,email',
67
- :app_id => '123',
68
- :canvas => RestGraph.default_canvas)
69
+ def myaction
70
+ @medata = rest_graph.get('me')
69
71
  end
70
72
  end
71
73
 
74
+ === Default setup
72
75
 
73
- # You might wonder how do we setup app_id, secret, and other stuffs?
74
- # You could pass them in rest_graph_setup(:app_id => 1234), or setup
75
- # in a config YAML file. Here's a config example:
76
- {rest-graph.yaml}[http://github.com/cardinalblue/rest-graph/blob/master/test/config/rest-graph.yaml]
77
- # For a QUICK START, we recommend that put this config file under
78
- # config/rest-graph.yaml and require 'rest-graph/auto_load' to automatically
79
- # load the config to setup default values for RestGraph in your application.
80
- # in Rails 2.x, you might want to add this line into config/environment.rb:
81
-
82
- config.gem 'rest-graph', :lib => 'rest-graph/auto_load'
83
-
84
- # While for bundler, you might want to add this line into Gemfile:
85
-
86
- gem 'rest-graph', :require => 'rest-graph/auto_load'
76
+ New RestGraph objects can read their default setup configuration from a
77
+ YAML configuration file.
87
78
 
88
- == SYNOPSIS:
79
+ * {Sample}[http://github.com/cardinalblue/rest-graph/blob/master/test/config/rest-graph.yaml]
89
80
 
90
- # Here are ALL the available options for new instance of RestGraph.
91
- # All options are optional:
81
+ To enable, just require anywhere:
92
82
 
93
- rg = RestGraph.new(:access_token => TOKEN,
94
- :graph_server => 'https://graph.facebook.com/',
95
- :old_server => 'https://api.facebook.com/',
96
- :accept => 'text/javascript',
97
- :lang => 'en-us', # this affect search
98
- :auto_decode => true , # decode by json
99
- :app_id => '123' ,
100
- :secret => '1829' ,
101
- :cache => {} , # a cache for the same API call
83
+ require 'rest-graph/auto_load'
102
84
 
103
- # This handler callback is only called if auto_decode is set to true,
104
- # otherwise, it's ignored.
105
- :error_handler =>
106
- lambda{ |hash| raise ::RestGraph::Error.new(hash) },
85
+ If you are using Rails and rest-graph as a gem, you can include this
86
+ when you specify the gem in your environment file by using:
107
87
 
108
- # You might want to do this in Rails to do debug logging:
109
- :log_handler =>
110
- lambda{ |duration, url|
111
- Rails.logger.debug("RestGraph " \
112
- "spent #{duration} " \
113
- "requesting #{url}")
114
- })
88
+ config.gem 'rest-graph', :lib => 'rest-graph/auto_load'
115
89
 
90
+ Or if using bundler, by adding this line into your Gemfile:
116
91
 
117
- # API calls:
92
+ gem 'rest-graph', :require => 'rest-graph/auto_load'
118
93
 
94
+ === Setup options:
95
+
96
+ Here are ALL the available options for new instance of RestGraph.
97
+
98
+ rg = RestGraph.new(
99
+ :access_token => TOKEN , # default nil
100
+ :graph_server => 'https://graph.facebook.com/', # this is the default
101
+ :old_server => 'https://api.facebook.com/' , # this is the default
102
+ :accept => 'text/javascript' , # this is the default
103
+ :lang => 'en-us' , # this affect search
104
+ :auto_decode => true , # decode by json
105
+ # default true
106
+ :app_id => '123' , # default nil
107
+ :secret => '1829' , # default nil
108
+
109
+ :cache => {} ,
110
+ # A cache for the same API call. Any object quacks like a hash should
111
+ # work, and Rails.cache works, too. (because of a patch in RailsUtil)
112
+
113
+ :error_handler => lambda{ |hash| raise ::RestGraph::Error.new(hash) },
114
+ # This handler callback is only called if auto_decode is
115
+ # set to true, otherwise, it's ignored. And raising exception
116
+ # is the default unless you're using RailsUtil and enabled
117
+ # auto_authorize. That way, RailsUtil would do redirect instead
118
+ # of raising an exception.
119
+
120
+ :log_handler => lambda{ |event|
121
+ Rails.logger.
122
+ debug("Spent #{event.duration} requesting #{event.url}")})
123
+ # You might not want to touch this if you're using RailsUtil.
124
+ # Otherwise, the default behavior is do nothing. (i.e. no logging)
125
+
126
+ And here are ALL the available options for rest_graph_setup. Note that all
127
+ options for RestGraph instance are also valid options for rest_graph_setup.
128
+
129
+ rest_graph_setup(#
130
+ # == All the above RestGraph options, plus
131
+ #
132
+ :canvas => 'mycanvas', # default ''
133
+ :iframe => true , # default false
134
+ :auto_authorize => true , # default false
135
+ :auto_authorize_scope => 'email' , # default ''
136
+ :auto_authorize_options => {} , # default {}
137
+ # auto_authorize means it will do redirect to oauth
138
+ # API automatically if the access_token is invalid or
139
+ # missing. So you would like to setup scope if you're
140
+ # using it. Note that: setting scope implies setting
141
+ # auto_authorize to true, even it's false.
142
+
143
+ :ensure_authorized => false , # default false
144
+ # This means if the access_token is not there,
145
+ # then do auto_authorize.
146
+
147
+ :write_session => false , # default false
148
+ :write_cookies => false , # default false
149
+ :write_handler =>
150
+ lambda{ |fbs| @cache[uid] = fbs } , # default nil
151
+ :check_handler =>
152
+ lambda{ @cache[uid] }) # default nil
153
+ # If we're not using Facebook JavaScript SDK,
154
+ # then we'll need to find a way to store the fbs,
155
+ # which contains access_token and/or user id.
156
+ # In a FBML canvas application, it seems session
157
+ # doesn't work right, so you'll need cookies or
158
+ # your custom handler to store it. In a standalone
159
+ # site or iframe canvas application, you might want
160
+ # to just use the Rails (or other framework) session.
161
+
162
+ === Setup procedures:
163
+
164
+ 1. Set upon RestGraph object creation:
165
+
166
+ rg = RestGraph.new :app_id => 1234
167
+
168
+ 2. Set via the rest_graph_setup call in a Controller:
169
+
170
+ rest_graph_setup :app_id => 1234
171
+
172
+ 3. Load from a YAML file
173
+
174
+ require 'rest-graph/load_config'
175
+ RestGraph::LoadConfig.load_config!('path/to/rest-graph.yaml', 'production')
176
+ rg = RestGraph.new
177
+
178
+ 4. Load config automatically
179
+
180
+ require 'rest-graph/auto_load' # under Rails, load config/rest-graph.yaml
181
+ rg = RestGraph.new
182
+
183
+ 5. Override directly
184
+
185
+ module MyDefaults
186
+ def default_app_id
187
+ '456'
188
+ end
189
+
190
+ def default_secret
191
+ 'category theory'
192
+ end
193
+ end
194
+ RestGraph.send(:extend, MyDefaults)
195
+ rg = RestGraph.new
196
+
197
+ == API REFERENCE:
198
+
199
+ === Facebook Graph API:
200
+
201
+ ==== get
119
202
  # GET https://graph.facebook.com/me?access_token=TOKEN
120
203
  rg.get('me')
121
204
 
122
205
  # GET https://graph.facebook.com/me?metadata=1&access_token=TOKEN
123
206
  rg.get('me', :metadata => '1')
124
207
 
125
- # POST https://graph.facebook.com/me/feed?message=bread%21&access_token=tok
208
+ ==== post
126
209
  rg.post('me/feed', :message => 'bread!')
127
210
 
128
- == UTILITY FUNCTIONS:
129
-
130
- # If you have the session in the cookies,
131
- # then RestGraph can parse the cookies:
132
- rg.parse_cookies!(cookies) # auto save access_token if sig is correct
133
- rg.data['uid'] # => facebook uid
211
+ ==== fql
212
+ Make an arbitrary
213
+ {FQL}[http://developers.facebook.com/docs/reference/fql/] query
134
214
 
135
- # If you're writing a Rack application, you might want to parse
136
- # the session directly from Rack env:
137
- rg.parse_rack_env!(env) # auto save access_token if sig is correct
138
- rg.data['uid'] # => facebook uid
139
-
140
- # The following method yields the redirect URL for authorizing
141
- # https://graph.facebook.com/oauth/authorize?client_id=123&
142
- # redirect_uri=http%3A%2F%2Fw3.org%2F
143
- rg.authorize_url(:redirect_uri => 'http://w3.org/', :scope => 'email')
144
-
145
- # The following method makes a call to Facebook to convert
146
- # the authorization "code" into an access token:
147
- # https://graph.facebook.com/oauth/access_token?code=CODE&
148
- # client_id=123&redirect_uri=http%3A%2F%2Fw3.org%2F&
149
- # client_secret=1829
150
- rg.authorize!(:redirect_uri => 'http://w3.org/', :code => 'CODE')
151
- rg.access_token # your access_token is now available
152
- rg.data['expires'] # other values are available in data
153
-
154
- # The following method takes a session key from the old REST API
155
- # (non-Graph API) and converts to an access token:
156
- # https://graph.facebook.com/oauth/exchange_sessions?sessions=SESSION
157
- params[:fb_sig_session_key] # => SESSION
158
- rg.exchange_sessions(:sessions => params[:fb_sig_session_key])
159
-
160
- # The following method allows for an arbitrary FQL query to made
161
- # GET https://api.facebook.com/method/fql.query?query=
162
- # SELECT+name+FROM+page+WHERE+page_id%3D%22123%22&
163
- # format=json&access_token=tok
164
215
  rg.fql('SELECT name FROM page WHERE page_id="123"')
165
216
 
166
- # The following method allows for multiple FQL query to made
167
- # http://developers.facebook.com/docs/reference/rest/fql.multiquery
168
- # GET https://api.facebook.com/method/fql.multiquery?query=
169
- # %7BSELECT+name+FROM+page+WHERE+page_id%3D%22123%22&%2C
170
- # SELECT+name+FROM+page+WHERE+page_id%3D%22456%22&%7D
171
- # format=json&access_token=tok
217
+ ==== fql_multi
172
218
  rg.fql_multi(:q1 => 'SELECT name FROM page WHERE page_id="123"',
173
219
  :q2 => 'SELECT name FROM page WHERE page_id="456"')
174
220
 
175
- # The following method makes it possible to call functionality
176
- # from Facebook's old REST API:
221
+ ==== old_rest
222
+ Call functionality from Facebook's old REST API:
223
+
177
224
  rg.old_rest(
178
225
  'stream.publish',
179
226
  { :message => 'Greetings',
@@ -192,47 +239,66 @@ by Cardinal Blue ( http://cardinalblue.com )
192
239
  # if Facebook is not returning a proper JSON
193
240
  # response. Otherwise, this could be omitted.
194
241
 
195
- # Here are 3 possible ways to set up the default settings:
242
+ === Utility Methods:
196
243
 
197
- # (1) set it directly
198
- module MyDefaults
199
- def default_app_id
200
- '456'
201
- end
244
+ ==== parse_xxxx
202
245
 
203
- def default_secret
204
- 'category theory'
205
- end
206
- end
207
- RestGraph.send(:extend, MyDefaults)
246
+ All the methods that obtain an access_token will automatically save it.
208
247
 
209
- # or (2) Load defaults from a YAML config file:
210
- require 'rest-graph/load_config'
211
- RestGraph::LoadConfig.load_config!('path/to/rest-graph.yaml', 'development')
248
+ If you have the session in the cookies,
249
+ then RestGraph can parse the cookies:
212
250
 
213
- RestGraph.new # app_id would be 456
214
- RestGraph.new(:app_id => '123') # defaults could be overridden
251
+ rg.parse_cookies!(cookies)
215
252
 
216
- # or (3) Load config automatically
217
- require 'rest-graph/auto_load' # under Rails, load config/rest-graph.yaml
253
+ If you're writing a Rack application, you might want to parse
254
+ the session directly from Rack env:
218
255
 
219
- # Please read: for an example of config file.
220
- # Note that :auto_authorize_scope and friends is only for RailsUtil.
221
- {rest-graph.yaml}[http://github.com/cardinalblue/rest-graph/blob/master/test/config/rest-graph.yaml]
256
+ rg.parse_rack_env!(env)
222
257
 
223
- == REQUIREMENTS:
258
+ ==== access_token
224
259
 
225
- * Tested with MRI 1.8.7 and 1.9.1 and Rubinius HEAD
226
- * gem install rest-client
227
- * gem install json (optional)
228
- * gem install json_pure (optional)
229
- * gem install rack (optional, to parse access_token in HTTP_COOKIE)
260
+ rg.access_token
230
261
 
231
- == INSTALL:
262
+ Data associated with the access_token (which might or might not
263
+ available, depending on how the access_token was obtained).
264
+
265
+ rg.data
266
+ rg.data['uid']
267
+ rg.data['expires']
268
+
269
+ ==== Default values
232
270
 
233
- > gem install rest-graph
234
- # or if you want rails plugin and bleeding edge
235
- > script/plugin install git://github.com/cardinalblue/rest-graph.git
271
+ Read from the rest-graph.yaml file.
272
+
273
+ RestGraph.default_xxxx
274
+
275
+ === Other ways of getting an access token
276
+
277
+ ==== authorize_url
278
+
279
+ Returns the redirect URL for authorizing
280
+
281
+ # https://graph.facebook.com/oauth/authorize?
282
+ # client_id=123&redirect_uri=http%3A%2F%2Fw3.org%2F
283
+ rg.authorize_url(:redirect_uri => 'http://w3.org/', :scope => 'email')
284
+
285
+ ==== authorize!
286
+
287
+ Makes a call to Facebook to convert
288
+ the authorization "code" into an access token:
289
+
290
+ # https://graph.facebook.com/oauth/access_token?
291
+ # code=CODE&client_id=123&client_secret=1829&
292
+ # redirect_uri=http%3A%2F%2Fw3.org%2F
293
+ rg.authorize!(:redirect_uri => 'http://w3.org/', :code => 'CODE')
294
+
295
+ ==== exchange_sessions
296
+
297
+ Takes a session key from the old REST API
298
+ (non-Graph API) and converts to an access token:
299
+
300
+ # https://graph.facebook.com/oauth/exchange_sessions?sessions=SESSION
301
+ rg.exchange_sessions(:sessions => params[:fb_sig_session_key])
236
302
 
237
303
  == LICENSE:
238
304