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 +31 -0
- data/CONTRIBUTORS +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +30 -49
- data/README +215 -149
- data/README.rdoc +215 -149
- data/Rakefile +16 -6
- data/example/rails/test/functional/application_controller_test.rb +2 -2
- data/lib/rest-graph.rb +113 -25
- data/lib/rest-graph/rails_util.rb +36 -26
- data/lib/rest-graph/version.rb +1 -1
- data/rest-graph.gemspec +24 -18
- data/test/common.rb +3 -0
- data/test/test_access_token.rb +26 -0
- data/test/{test_rest-graph.rb → test_api.rb} +2 -66
- data/test/test_cache.rb +41 -0
- data/test/test_handler.rb +2 -4
- data/test/test_misc.rb +51 -0
- data/test/test_old.rb +1 -13
- data/test/test_parse.rb +1 -1
- data/test/test_serialize.rb +26 -0
- metadata +67 -42
data/README.rdoc
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= rest-graph 1.4.
|
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
|
-
|
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
|
-
==
|
21
|
+
== REQUIREMENTS:
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
28
|
-
rg = RestGraph.new(:access_token => TOKEN)
|
29
|
+
== INSTALL:
|
29
30
|
|
30
|
-
|
31
|
-
rg.get('me')
|
31
|
+
gem install rest-graph
|
32
32
|
|
33
|
-
|
34
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
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
|
-
|
56
|
+
rest_graph: Accesses the RestGraph object by rest_graph_setup.
|
51
57
|
|
52
|
-
|
53
|
-
include RestGraph::RailsUtil
|
54
|
-
before_filter :filter_rest_graph_setup
|
58
|
+
=== Example usage:
|
55
59
|
|
56
|
-
|
57
|
-
|
58
|
-
|
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
|
-
|
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
|
-
|
74
|
-
|
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
|
-
|
79
|
+
* {Sample}[http://github.com/cardinalblue/rest-graph/blob/master/test/config/rest-graph.yaml]
|
89
80
|
|
90
|
-
|
91
|
-
# All options are optional:
|
81
|
+
To enable, just require anywhere:
|
92
82
|
|
93
|
-
|
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
|
-
|
104
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
208
|
+
==== post
|
126
209
|
rg.post('me/feed', :message => 'bread!')
|
127
210
|
|
128
|
-
|
129
|
-
|
130
|
-
|
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
|
-
|
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
|
-
|
176
|
-
|
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
|
-
|
242
|
+
=== Utility Methods:
|
196
243
|
|
197
|
-
|
198
|
-
module MyDefaults
|
199
|
-
def default_app_id
|
200
|
-
'456'
|
201
|
-
end
|
244
|
+
==== parse_xxxx
|
202
245
|
|
203
|
-
|
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
|
-
|
210
|
-
|
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
|
-
|
214
|
-
RestGraph.new(:app_id => '123') # defaults could be overridden
|
251
|
+
rg.parse_cookies!(cookies)
|
215
252
|
|
216
|
-
|
217
|
-
|
253
|
+
If you're writing a Rack application, you might want to parse
|
254
|
+
the session directly from Rack env:
|
218
255
|
|
219
|
-
|
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
|
-
|
258
|
+
==== access_token
|
224
259
|
|
225
|
-
|
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
|
-
|
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
|
-
|
234
|
-
|
235
|
-
|
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
|
|
data/Rakefile
CHANGED
@@ -16,12 +16,15 @@ Bones{
|
|
16
16
|
version RestGraph::VERSION
|
17
17
|
|
18
18
|
depend_on 'rest-client'
|
19
|
-
depend_on 'json', :development => true
|
20
|
-
depend_on 'rack', :development => true
|
21
19
|
|
22
|
-
depend_on '
|
23
|
-
depend_on '
|
24
|
-
depend_on '
|
20
|
+
depend_on 'yajl-ruby', :development => true
|
21
|
+
depend_on 'json' , :development => true
|
22
|
+
depend_on 'json_pure', :development => true
|
23
|
+
|
24
|
+
depend_on 'rack' , :development => true
|
25
|
+
depend_on 'rr' , :development => true
|
26
|
+
depend_on 'webmock' , :development => true
|
27
|
+
depend_on 'bacon' , :development => true
|
25
28
|
|
26
29
|
name proj
|
27
30
|
url "http://github.com/cardinalblue/#{proj}"
|
@@ -32,7 +35,7 @@ Bones{
|
|
32
35
|
readme_file 'README.rdoc'
|
33
36
|
ignore_file '.gitignore'
|
34
37
|
rdoc.include ['\w+']
|
35
|
-
rdoc.exclude ['test', 'doc', 'Rakefile']
|
38
|
+
rdoc.exclude ['test', 'doc', 'Rakefile', 'example']
|
36
39
|
}
|
37
40
|
|
38
41
|
CLEAN.include Dir['**/*.rbc']
|
@@ -49,3 +52,10 @@ end
|
|
49
52
|
|
50
53
|
desc 'Run all tests'
|
51
54
|
task 'test:all' => ['test', 'test:example']
|
55
|
+
|
56
|
+
desc 'Run different json test'
|
57
|
+
task 'test:json' do
|
58
|
+
%w[yajl json].each{ |json|
|
59
|
+
sh "#{Gem.ruby} -S rake -r #{json} test"
|
60
|
+
}
|
61
|
+
end
|