rest-graph 1.7.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/CHANGES +44 -0
- data/CONTRIBUTORS +1 -0
- data/README +221 -191
- data/README.md +367 -0
- data/Rakefile +28 -43
- data/TODO +1 -0
- data/doc/ToC.md +10 -0
- data/doc/dependency.md +78 -0
- data/doc/design.md +206 -0
- data/doc/rails.md +12 -0
- data/doc/test.md +46 -0
- data/doc/tutorial.md +142 -0
- data/example/rails2/Gemfile +13 -0
- data/example/rails2/app/controllers/application_controller.rb +10 -8
- data/example/rails2/app/views/application/helper.html.erb +1 -0
- data/example/rails2/config/boot.rb +16 -0
- data/example/rails2/config/environment.rb +3 -30
- data/example/rails2/config/preinitializer.rb +23 -0
- data/example/rails2/test/functional/application_controller_test.rb +72 -32
- data/example/rails2/test/test_helper.rb +10 -6
- data/example/rails3/Gemfile +13 -0
- data/example/rails3/Rakefile +7 -0
- data/example/rails3/app/controllers/application_controller.rb +118 -0
- data/example/rails3/app/views/application/helper.html.erb +1 -0
- data/example/rails3/config.ru +4 -0
- data/example/rails3/config/application.rb +23 -0
- data/example/rails3/config/environment.rb +5 -0
- data/example/rails3/config/environments/development.rb +26 -0
- data/example/rails3/config/environments/production.rb +49 -0
- data/example/rails3/config/environments/test.rb +30 -0
- data/example/rails3/config/initializers/secret_token.rb +7 -0
- data/example/rails3/config/initializers/session_store.rb +8 -0
- data/example/rails3/config/rest-graph.yaml +11 -0
- data/example/rails3/config/routes.rb +5 -0
- data/example/rails3/test/functional/application_controller_test.rb +183 -0
- data/example/rails3/test/test_helper.rb +18 -0
- data/example/rails3/test/unit/rails_util_test.rb +44 -0
- data/init.rb +1 -1
- data/lib/rest-graph.rb +5 -571
- data/lib/rest-graph/auto_load.rb +3 -3
- data/lib/rest-graph/autoload.rb +3 -3
- data/lib/rest-graph/config_util.rb +43 -0
- data/lib/rest-graph/core.rb +608 -0
- data/lib/rest-graph/facebook_util.rb +74 -0
- data/lib/rest-graph/rails_util.rb +85 -37
- data/lib/rest-graph/test_util.rb +18 -2
- data/lib/rest-graph/version.rb +2 -2
- data/rest-graph.gemspec +42 -47
- data/task/gemgem.rb +155 -0
- data/test/test_api.rb +16 -0
- data/test/test_cache.rb +28 -8
- data/test/test_error.rb +9 -0
- data/test/test_facebook.rb +36 -0
- data/test/test_load_config.rb +16 -14
- data/test/test_misc.rb +4 -4
- data/test/test_parse.rb +10 -4
- metadata +146 -186
- data/Gemfile.lock +0 -45
- data/README.rdoc +0 -337
- data/example/rails2/script/console +0 -3
- data/example/rails2/script/server +0 -3
- data/lib/rest-graph/load_config.rb +0 -41
data/Gemfile.lock
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
addressable (2.2.2)
|
5
|
-
bacon (1.1.0)
|
6
|
-
bones (3.5.4)
|
7
|
-
little-plugger (>= 1.1.2)
|
8
|
-
loquacious (>= 1.7.0)
|
9
|
-
rake (>= 0.8.7)
|
10
|
-
crack (0.1.8)
|
11
|
-
em-http-request (0.2.15)
|
12
|
-
addressable (>= 2.0.0)
|
13
|
-
eventmachine (>= 0.12.9)
|
14
|
-
eventmachine (0.12.10)
|
15
|
-
json (1.4.6)
|
16
|
-
json_pure (1.4.6)
|
17
|
-
little-plugger (1.1.2)
|
18
|
-
loquacious (1.7.0)
|
19
|
-
mime-types (1.16)
|
20
|
-
rack (1.2.1)
|
21
|
-
rake (0.8.7)
|
22
|
-
rest-client (1.6.1)
|
23
|
-
mime-types (>= 1.16)
|
24
|
-
rr (1.0.2)
|
25
|
-
ruby-hmac (0.4.0)
|
26
|
-
webmock (1.6.1)
|
27
|
-
addressable (>= 2.2.2)
|
28
|
-
crack (>= 0.1.7)
|
29
|
-
yajl-ruby (0.7.8)
|
30
|
-
|
31
|
-
PLATFORMS
|
32
|
-
ruby
|
33
|
-
|
34
|
-
DEPENDENCIES
|
35
|
-
bacon
|
36
|
-
bones
|
37
|
-
em-http-request
|
38
|
-
json
|
39
|
-
json_pure
|
40
|
-
rack
|
41
|
-
rest-client (>= 1.6)
|
42
|
-
rr
|
43
|
-
ruby-hmac
|
44
|
-
webmock
|
45
|
-
yajl-ruby
|
data/README.rdoc
DELETED
@@ -1,337 +0,0 @@
|
|
1
|
-
= rest-graph 1.7.0
|
2
|
-
by Cardinal Blue ( http://cardinalblue.com )
|
3
|
-
|
4
|
-
== LINKS:
|
5
|
-
|
6
|
-
* {github}[http://github.com/cardinalblue/rest-graph]
|
7
|
-
* {rubygems}[http://rubygems.org/gems/rest-graph]
|
8
|
-
* {rdoc}[http://rdoc.info/projects/cardinalblue/rest-graph]
|
9
|
-
* {mailing list}[http://groups.google.com/group/rest-graph/topics]
|
10
|
-
|
11
|
-
== DESCRIPTION:
|
12
|
-
|
13
|
-
A super simple Facebook Open Graph API client
|
14
|
-
|
15
|
-
== FEATURES:
|
16
|
-
|
17
|
-
* Simple Graph API call
|
18
|
-
* Simple FQL call
|
19
|
-
* Utility to extract access_token and check sig in cookies
|
20
|
-
|
21
|
-
== REQUIREMENTS:
|
22
|
-
|
23
|
-
* Tested with MRI 1.8.7 and 1.9.2 and Rubinius 1.1.1
|
24
|
-
|
25
|
-
* (must) pick one HTTP client:
|
26
|
-
- gem install rest-client
|
27
|
-
- gem install em-http-request
|
28
|
-
|
29
|
-
* (optional) pick one JSON parser/generator:
|
30
|
-
- gem install yajl-ruby
|
31
|
-
- gem install json
|
32
|
-
- gem install json_pure
|
33
|
-
|
34
|
-
* (optional) parse access_token in HTTP_COOKIE
|
35
|
-
- gem install rack
|
36
|
-
|
37
|
-
== INSTALL:
|
38
|
-
|
39
|
-
gem install rest-graph
|
40
|
-
|
41
|
-
or if you want rails plugin and bleeding edge
|
42
|
-
|
43
|
-
script/plugin install git://github.com/cardinalblue/rest-graph.git
|
44
|
-
|
45
|
-
== QUICK START:
|
46
|
-
|
47
|
-
require 'rest-graph'
|
48
|
-
rg = RestGraph.new(:access_token => 'myaccesstokenfromfb')
|
49
|
-
rg.get('me')
|
50
|
-
rg.get('me/likes')
|
51
|
-
rg.get('search', :q => 'taiwan')
|
52
|
-
|
53
|
-
=== Obtaining an access token
|
54
|
-
|
55
|
-
If you are using Rails, we recommend that you include a module called
|
56
|
-
RestGraph::RailsUtil into your Controllers. (Your code contributions
|
57
|
-
for other Ruby frameworks would be appreciated!). RestGraph::RailsUtil
|
58
|
-
adds the following two methods to your Controllers:
|
59
|
-
|
60
|
-
rest_graph_setup: Attempts to find an access_token from the environment
|
61
|
-
and initializes a RestGraph object with it.
|
62
|
-
Most commonly used inside a filter.
|
63
|
-
|
64
|
-
rest_graph: Accesses the RestGraph object by rest_graph_setup.
|
65
|
-
|
66
|
-
=== Example usage:
|
67
|
-
|
68
|
-
class MyController
|
69
|
-
include RestGraph::RailsUtil
|
70
|
-
before_filter do
|
71
|
-
rest_graph_setup(:app_id => '123',
|
72
|
-
:canvas => 'mycanvas',
|
73
|
-
:auto_authorize_scope => 'email')
|
74
|
-
# See below for more options
|
75
|
-
end
|
76
|
-
|
77
|
-
def myaction
|
78
|
-
@medata = rest_graph.get('me')
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
=== Default setup
|
83
|
-
|
84
|
-
New RestGraph objects can read their default setup configuration from a
|
85
|
-
YAML configuration file.
|
86
|
-
|
87
|
-
* {Example}[http://github.com/cardinalblue/rest-graph/blob/master/test/config/rest-graph.yaml]
|
88
|
-
|
89
|
-
To enable, just require anywhere:
|
90
|
-
|
91
|
-
require 'rest-graph/autoload'
|
92
|
-
|
93
|
-
If you are using Rails and rest-graph as a gem, you can include this
|
94
|
-
when you specify the gem in your environment file by using:
|
95
|
-
|
96
|
-
config.gem 'rest-graph', :lib => 'rest-graph/autoload'
|
97
|
-
|
98
|
-
Or if using bundler, by adding this line into your Gemfile:
|
99
|
-
|
100
|
-
gem 'rest-graph', :require => 'rest-graph/autoload'
|
101
|
-
|
102
|
-
=== Setup options:
|
103
|
-
|
104
|
-
Here are ALL the available options for new instance of RestGraph.
|
105
|
-
|
106
|
-
rg = RestGraph.new(
|
107
|
-
:access_token => TOKEN , # default nil
|
108
|
-
:graph_server => 'https://graph.facebook.com/', # this is the default
|
109
|
-
:old_server => 'https://api.facebook.com/' , # this is the default
|
110
|
-
:accept => 'text/javascript' , # this is the default
|
111
|
-
:lang => 'en-us' , # this affect search
|
112
|
-
:auto_decode => true , # decode by json
|
113
|
-
# default true
|
114
|
-
:app_id => '123' , # default nil
|
115
|
-
:secret => '1829' , # default nil
|
116
|
-
|
117
|
-
:cache => {} ,
|
118
|
-
# A cache for the same API call. Any object quacks like a hash should
|
119
|
-
# work, and Rails.cache works, too. (because of a patch in RailsUtil)
|
120
|
-
|
121
|
-
:error_handler => lambda{ |hash| raise ::RestGraph::Error.new(hash) },
|
122
|
-
# This handler callback is only called if auto_decode is
|
123
|
-
# set to true, otherwise, it's ignored. And raising exception
|
124
|
-
# is the default unless you're using RailsUtil and enabled
|
125
|
-
# auto_authorize. That way, RailsUtil would do redirect instead
|
126
|
-
# of raising an exception.
|
127
|
-
|
128
|
-
:log_method => method(:puts),
|
129
|
-
# This way, any log message would be output by puts. If you want to
|
130
|
-
# change the log format, use log_handler instead. See below:
|
131
|
-
|
132
|
-
:log_handler => lambda{ |event|
|
133
|
-
Rails.logger.
|
134
|
-
debug("Spent #{event.duration} requesting #{event.url}")})
|
135
|
-
# You might not want to touch this if you're using RailsUtil.
|
136
|
-
# Otherwise, the default behavior is do nothing. (i.e. no logging)
|
137
|
-
|
138
|
-
And here are ALL the available options for rest_graph_setup. Note that all
|
139
|
-
options for RestGraph instance are also valid options for rest_graph_setup.
|
140
|
-
|
141
|
-
rest_graph_setup(#
|
142
|
-
# == All the above RestGraph options, plus
|
143
|
-
#
|
144
|
-
:canvas => 'mycanvas', # default ''
|
145
|
-
:iframe => true , # default false
|
146
|
-
:auto_authorize => true , # default false
|
147
|
-
:auto_authorize_scope => 'email' , # default ''
|
148
|
-
:auto_authorize_options => {} , # default {}
|
149
|
-
# auto_authorize means it will do redirect to oauth
|
150
|
-
# API automatically if the access_token is invalid or
|
151
|
-
# missing. So you would like to setup scope if you're
|
152
|
-
# using it. Note that: setting scope implies setting
|
153
|
-
# auto_authorize to true, even it's false.
|
154
|
-
|
155
|
-
:ensure_authorized => false , # default false
|
156
|
-
# This means if the access_token is not there,
|
157
|
-
# then do auto_authorize.
|
158
|
-
|
159
|
-
:write_session => true , # default false
|
160
|
-
:write_cookies => false , # default false
|
161
|
-
:write_handler =>
|
162
|
-
lambda{ |fbs| @cache[uid] = fbs } , # default nil
|
163
|
-
:check_handler =>
|
164
|
-
lambda{ @cache[uid] }) # default nil
|
165
|
-
# If we're not using Facebook JavaScript SDK,
|
166
|
-
# then we'll need to find a way to store the fbs,
|
167
|
-
# which contains access_token and/or user id.
|
168
|
-
# In a FBML canvas application, it seems session
|
169
|
-
# doesn't work right, so you'll need cookies or
|
170
|
-
# your custom handler to store it. In a standalone
|
171
|
-
# site or iframe canvas application, you might want
|
172
|
-
# to just use the Rails (or other framework) session.
|
173
|
-
|
174
|
-
=== Alternate ways to setup RestGraph:
|
175
|
-
|
176
|
-
1. Set upon RestGraph object creation:
|
177
|
-
|
178
|
-
rg = RestGraph.new :app_id => 1234
|
179
|
-
|
180
|
-
2. Set via the rest_graph_setup call in a Controller:
|
181
|
-
|
182
|
-
rest_graph_setup :app_id => 1234
|
183
|
-
|
184
|
-
3. Load from a YAML file
|
185
|
-
|
186
|
-
require 'rest-graph/load_config'
|
187
|
-
RestGraph::LoadConfig.load_config!('path/to/rest-graph.yaml', 'production')
|
188
|
-
rg = RestGraph.new
|
189
|
-
|
190
|
-
4. Load config automatically
|
191
|
-
|
192
|
-
require 'rest-graph/autoload' # under Rails, load config/rest-graph.yaml
|
193
|
-
rg = RestGraph.new
|
194
|
-
|
195
|
-
5. Override directly
|
196
|
-
|
197
|
-
module MyDefaults
|
198
|
-
def default_app_id
|
199
|
-
'456'
|
200
|
-
end
|
201
|
-
|
202
|
-
def default_secret
|
203
|
-
'category theory'
|
204
|
-
end
|
205
|
-
end
|
206
|
-
RestGraph.send(:extend, MyDefaults)
|
207
|
-
rg = RestGraph.new
|
208
|
-
|
209
|
-
== API REFERENCE:
|
210
|
-
|
211
|
-
=== Facebook Graph API:
|
212
|
-
|
213
|
-
==== get
|
214
|
-
# GET https://graph.facebook.com/me?access_token=TOKEN
|
215
|
-
rg.get('me')
|
216
|
-
|
217
|
-
# GET https://graph.facebook.com/me?metadata=1&access_token=TOKEN
|
218
|
-
rg.get('me', :metadata => '1')
|
219
|
-
|
220
|
-
==== post
|
221
|
-
rg.post('me/feed', :message => 'bread!')
|
222
|
-
|
223
|
-
==== fql
|
224
|
-
Make an arbitrary
|
225
|
-
{FQL}[http://developers.facebook.com/docs/reference/fql/] query
|
226
|
-
|
227
|
-
rg.fql('SELECT name FROM page WHERE page_id="123"')
|
228
|
-
|
229
|
-
==== fql_multi
|
230
|
-
rg.fql_multi(:q1 => 'SELECT name FROM page WHERE page_id="123"',
|
231
|
-
:q2 => 'SELECT name FROM page WHERE page_id="456"')
|
232
|
-
|
233
|
-
==== old_rest
|
234
|
-
Call functionality from Facebook's old REST API:
|
235
|
-
|
236
|
-
rg.old_rest(
|
237
|
-
'stream.publish',
|
238
|
-
{ :message => 'Greetings',
|
239
|
-
:attachment => {:name => 'Wikipedia',
|
240
|
-
:href => 'http://wikipedia.org/',
|
241
|
-
:caption => 'Wikipedia says hi.',
|
242
|
-
:media => [{:type => 'image',
|
243
|
-
:src => 'http://wikipedia.org/favicon.ico',
|
244
|
-
:href => 'http://wikipedia.org/'}]
|
245
|
-
}.to_json,
|
246
|
-
:action_links => [{:text => 'Go to Wikipedia',
|
247
|
-
:href => 'http://wikipedia.org/'}
|
248
|
-
].to_json
|
249
|
-
},
|
250
|
-
:auto_decode => false) # You'll need to set auto_decode to false for
|
251
|
-
# this API request if Facebook is not returning
|
252
|
-
# a proper formatted JSON response. Otherwise,
|
253
|
-
# this could be omitted.
|
254
|
-
|
255
|
-
# Some Old Rest API requires a special access token with app secret
|
256
|
-
# inside of it. For those methods, use secret_old_rest instead of the
|
257
|
-
# usual old_rest with common access token.
|
258
|
-
rg.secret_old_rest('admin.getAppProperties', :properties => 'app_id')
|
259
|
-
|
260
|
-
=== Utility Methods:
|
261
|
-
|
262
|
-
==== parse_xxxx
|
263
|
-
|
264
|
-
All the methods that obtain an access_token will automatically save it.
|
265
|
-
|
266
|
-
If you have the session in the cookies,
|
267
|
-
then RestGraph can parse the cookies:
|
268
|
-
|
269
|
-
rg.parse_cookies!(cookies)
|
270
|
-
|
271
|
-
If you're writing a Rack application, you might want to parse
|
272
|
-
the session directly from Rack env:
|
273
|
-
|
274
|
-
rg.parse_rack_env!(env)
|
275
|
-
|
276
|
-
==== access_token
|
277
|
-
|
278
|
-
rg.access_token
|
279
|
-
|
280
|
-
Data associated with the access_token (which might or might not
|
281
|
-
available, depending on how the access_token was obtained).
|
282
|
-
|
283
|
-
rg.data
|
284
|
-
rg.data['uid']
|
285
|
-
rg.data['expires']
|
286
|
-
|
287
|
-
==== Default values
|
288
|
-
|
289
|
-
Read from the rest-graph.yaml file.
|
290
|
-
|
291
|
-
RestGraph.default_xxxx
|
292
|
-
|
293
|
-
=== Other ways of getting an access token
|
294
|
-
|
295
|
-
==== authorize_url
|
296
|
-
|
297
|
-
Returns the redirect URL for authorizing
|
298
|
-
|
299
|
-
# https://graph.facebook.com/oauth/authorize?
|
300
|
-
# client_id=123&redirect_uri=http%3A%2F%2Fw3.org%2F
|
301
|
-
rg.authorize_url(:redirect_uri => 'http://w3.org/', :scope => 'email')
|
302
|
-
|
303
|
-
==== authorize!
|
304
|
-
|
305
|
-
Makes a call to Facebook to convert
|
306
|
-
the authorization "code" into an access token:
|
307
|
-
|
308
|
-
# https://graph.facebook.com/oauth/access_token?
|
309
|
-
# code=CODE&client_id=123&client_secret=1829&
|
310
|
-
# redirect_uri=http%3A%2F%2Fw3.org%2F
|
311
|
-
rg.authorize!(:redirect_uri => 'http://w3.org/', :code => 'CODE')
|
312
|
-
|
313
|
-
==== exchange_sessions
|
314
|
-
|
315
|
-
Takes a session key from the old REST API
|
316
|
-
(non-Graph API) and converts to an access token:
|
317
|
-
|
318
|
-
# https://graph.facebook.com/oauth/exchange_sessions?sessions=SESSION
|
319
|
-
rg.exchange_sessions(:sessions => params[:fb_sig_session_key])
|
320
|
-
|
321
|
-
== LICENSE:
|
322
|
-
|
323
|
-
Apache License 2.0
|
324
|
-
|
325
|
-
Copyright (c) 2010, Cardinal Blue
|
326
|
-
|
327
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
328
|
-
you may not use this file except in compliance with the License.
|
329
|
-
You may obtain a copy of the License at
|
330
|
-
|
331
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
332
|
-
|
333
|
-
Unless required by applicable law or agreed to in writing, software
|
334
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
335
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
336
|
-
See the License for the specific language governing permissions and
|
337
|
-
limitations under the License.
|
@@ -1,41 +0,0 @@
|
|
1
|
-
|
2
|
-
require 'erb'
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
require 'rest-graph'
|
6
|
-
require 'rest-graph/rails_util' if Object.const_defined?(:Rails)
|
7
|
-
|
8
|
-
module RestGraph::LoadConfig
|
9
|
-
module_function
|
10
|
-
def autoload!
|
11
|
-
RestGraph::LoadConfig.load_if_rails!
|
12
|
-
end
|
13
|
-
|
14
|
-
def load_if_rails!
|
15
|
-
return unless Object.const_defined?(:Rails)
|
16
|
-
root = Rails.root
|
17
|
-
file = ["#{root}/config/rest-graph.yaml", # YAML should use .yaml
|
18
|
-
"#{root}/config/rest-graph.yml"].find{|path| File.exist?(path)}
|
19
|
-
return unless file
|
20
|
-
|
21
|
-
RestGraph::LoadConfig.load_config!(file, Rails.env)
|
22
|
-
end
|
23
|
-
|
24
|
-
def load_config! file, env
|
25
|
-
config = YAML.load(ERB.new(File.read(file)).result(binding))
|
26
|
-
defaults = config[env]
|
27
|
-
return unless defaults
|
28
|
-
|
29
|
-
mod = Module.new
|
30
|
-
mod.module_eval(defaults.inject([]){ |r, (k, v)|
|
31
|
-
r << <<-RUBY
|
32
|
-
def default_#{k}
|
33
|
-
# quote strings, leave others free (e.g. false, numbers, etc)
|
34
|
-
#{v.kind_of?(String) ? "'#{v}'" : v}
|
35
|
-
end
|
36
|
-
RUBY
|
37
|
-
}.join)
|
38
|
-
|
39
|
-
RestGraph.send(:extend, mod)
|
40
|
-
end
|
41
|
-
end
|