rest-graph 1.9.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README DELETED
@@ -1,371 +0,0 @@
1
- # rest-graph
2
- by Cardinal Blue <http://cardinalblue.com>
3
-
4
- Tutorial on setting up a sample Facebook application with Rails 3
5
- and RestGraph could be found on [samplergthree][]. Instead, if you're
6
- an experienced Ruby programmer, you might also want to look at
7
- [detailed documents][].
8
-
9
- [samplergthree]: https://github.com/cardinalblue/samplergthree
10
- [detailed documents]: https://github.com/cardinalblue/rest-graph/blob/master/doc/ToC.md
11
-
12
- ## LINKS:
13
-
14
- * [github](http://github.com/cardinalblue/rest-graph)
15
- * [rubygems](http://rubygems.org/gems/rest-graph)
16
- * [rdoc](http://rdoc.info/projects/cardinalblue/rest-graph)
17
- * [mailing list](http://groups.google.com/group/rest-graph/topics)
18
-
19
- ## DESCRIPTION:
20
-
21
- A lightweight Facebook Graph API client
22
-
23
- ## FEATURES:
24
-
25
- * Simple Graph API call
26
- * Simple FQL call
27
- * Utility to extract access_token and check sig in cookies/signed_request
28
-
29
- ## REQUIREMENTS:
30
-
31
- * Tested with MRI 1.8.7 and 1.9.2 and Rubinius 1.2.2.
32
- Because of development gems can't work well on JRuby,
33
- let me know if rest-graph is working on JRuby, thanks!
34
-
35
- * (must) pick one HTTP client:
36
- - gem install rest-client
37
- - gem install em-http-request
38
-
39
- * (optional) pick one JSON parser/generator:
40
- - gem install yajl-ruby
41
- - gem install json
42
- - gem install json_pure
43
-
44
- * (optional) parse access_token in HTTP_COOKIE
45
- - gem install rack
46
-
47
- * (optional) to use rest-graph/test_util
48
- - gem install rr
49
-
50
- ## INSTALLATION:
51
-
52
- gem install rest-graph
53
-
54
- Or if you want development version, put this in Gemfile:
55
-
56
- gem 'rest-graph', :git => 'git://github.com/cardinalblue/rest-graph.git'
57
-
58
- Or as a Rails2 plugin:
59
-
60
- ./script/plugin install git://github.com/cardinalblue/rest-graph.git
61
-
62
- ## QUICK START:
63
-
64
- require 'rest-graph'
65
- rg = RestGraph.new(:access_token => 'myaccesstokenfromfb')
66
- rg.get('me')
67
- rg.get('me/likes')
68
- rg.get('search', :q => 'taiwan')
69
-
70
- ### Obtaining an access token
71
-
72
- If you are using Rails, we recommend that you include a module called
73
- RestGraph::RailsUtil into your controllers. (Your code contributions
74
- for other Ruby frameworks would be appreciated!). RestGraph::RailsUtil
75
- adds the following two methods to your controllers:
76
-
77
- rest_graph_setup: Attempts to find an access_token from the environment
78
- and initializes a RestGraph object with it.
79
- Most commonly used inside a filter.
80
-
81
- rest_graph: Accesses the RestGraph object by rest_graph_setup.
82
-
83
- ### Example usage:
84
-
85
- class MyController < ActionController::Base
86
- include RestGraph::RailsUtil
87
- before_filter :setup
88
-
89
- def myaction
90
- @medata = rest_graph.get('me')
91
- end
92
-
93
- private
94
- def setup
95
- rest_graph_setup(:app_id => '123',
96
- :canvas => 'mycanvas',
97
- :auto_authorize_scope => 'email')
98
- # See below for more options
99
- end
100
- end
101
-
102
- ### Default setup
103
-
104
- New RestGraph objects can read their default setup configuration from a
105
- YAML configuration file. Which is the same as passing to rest_graph_setup.
106
-
107
- * [Example](test/config/rest-graph.yaml)
108
-
109
- To enable, just require anywhere:
110
-
111
- require 'rest-graph'
112
-
113
- Or if you're using bundler, add this line into Gemfile:
114
-
115
- gem 'rest-graph'
116
-
117
- ## SETUP OPTIONS:
118
-
119
- Here are ALL the available options for new instance of RestGraph.
120
-
121
- rg = RestGraph.new(
122
- :access_token => TOKEN , # default nil
123
- :graph_server => 'https://graph.facebook.com/', # this is default
124
- :old_server => 'https://api.facebook.com/' , # this is default
125
- :accept => 'text/javascript' , # this is default
126
- :lang => 'en-us' , # affect search
127
- :auto_decode => true , # decode by json
128
- # default true
129
- :app_id => '123' , # default nil
130
- :secret => '1829' , # default nil
131
-
132
- :cache => {} ,
133
- # A cache for the same API call. Any object quacks like a hash
134
- # should work, and Rails.cache works, too. (because of a patch in
135
- # RailsUtil)
136
-
137
- :error_handler => lambda{|hash| raise RestGraph::Error.new(hash)},
138
- # This handler callback is only called if auto_decode is
139
- # set to true, otherwise, it's ignored. And raising exception
140
- # is the default unless you're using RailsUtil and enabled
141
- # auto_authorize. That way, RailsUtil would do redirect
142
- # instead of raising an exception.
143
-
144
- :log_method => method(:puts),
145
- # This way, any log message would be output by puts. If you want to
146
- # change the log format, use log_handler instead. See below:
147
-
148
- :log_handler => lambda{ |event|
149
- Rails.logger.
150
- debug("Spent #{event.duration} requesting #{event.url}")})
151
- # You might not want to touch this if you're using RailsUtil.
152
- # Otherwise, the default behavior is do nothing. (i.e. no logging)
153
-
154
- And here are ALL the available options for rest_graph_setup. Note that all
155
- options for RestGraph instance are also valid options for rest_graph_setup.
156
-
157
- rest_graph_setup(#
158
- # == All the above RestGraph options, plus
159
- #
160
- :canvas => 'mycanvas', # default ''
161
- :auto_authorize => true , # default false
162
- :auto_authorize_scope => 'email' , # default ''
163
- :auto_authorize_options => {} , # default {}
164
- # auto_authorize means it will do redirect to oauth
165
- # API automatically if the access_token is invalid or
166
- # missing. So you would like to setup scope if you're
167
- # using it. Note that: setting scope implies setting
168
- # auto_authorize to true, even it's false.
169
-
170
- :ensure_authorized => false , # default false
171
- # This means if the access_token is not there,
172
- # then do auto_authorize.
173
-
174
- :write_session => true , # default false
175
- :write_cookies => false , # default false
176
- :write_handler =>
177
- lambda{ |fbs| @cache[uid] = fbs } , # default nil
178
- :check_handler =>
179
- lambda{ @cache[uid] }) # default nil
180
- # If we're not using Facebook JavaScript SDK,
181
- # then we'll need to find a way to store the fbs,
182
- # which contains access_token and/or user id. In a
183
- # standalone site or iframe canvas application, you might
184
- # want to just use the Rails (or other framework) session
185
-
186
- ### Alternate ways to setup RestGraph:
187
-
188
- 1. Set upon RestGraph object creation:
189
-
190
- rg = RestGraph.new :app_id => 1234
191
-
192
- 2. Set via the rest_graph_setup call in a Controller:
193
-
194
- rest_graph_setup :app_id => 1234
195
-
196
- 3. Load from a YAML file
197
-
198
- require 'rest-graph/config_util'
199
- RestGraph.load_config('path/to/rest-graph.yaml', 'production')
200
- rg = RestGraph.new
201
-
202
- 4. Load config automatically
203
-
204
- require 'rest-graph' # under Rails, would load config/rest-graph.yaml
205
- rg = RestGraph.new
206
-
207
- 5. Override directly
208
-
209
- module MyDefaults
210
- def default_app_id
211
- '456'
212
- end
213
-
214
- def default_secret
215
- 'category theory'
216
- end
217
- end
218
- RestGraph.send(:extend, MyDefaults)
219
- rg = RestGraph.new
220
-
221
- ## API REFERENCE:
222
-
223
- ### Facebook Graph API:
224
-
225
- #### get
226
- # GET https://graph.facebook.com/me?access_token=TOKEN
227
- rg.get('me')
228
-
229
- # GET https://graph.facebook.com/me?metadata=1&access_token=TOKEN
230
- rg.get('me', :metadata => '1')
231
-
232
- # extra options:
233
- # auto_decode: Bool # decode with json or not in this API request
234
- # # default: auto_decode in rest-graph instance
235
- # timeout: Int # the timeout for this API request
236
- # # default: timeout in rest-graph instance
237
- # secret: Bool # use secret_acccess_token or not
238
- # # default: false
239
- # cache: Bool # use cache or not; if it's false, update cache, too
240
- # # default: true
241
- # expires_in: Int # control when would the cache be expired
242
- # # default: nil
243
- # async: Bool # use eventmachine for http client or not
244
- # # default: false, but true in aget family
245
- # headers: Hash # additional hash you want to pass
246
- # # default: {}
247
- rg.get('me', {:metadata => '1'}, :secret => true, expires_in => 600)
248
-
249
- #### post
250
-
251
- rg.post('me/feed', :message => 'bread!')
252
-
253
- #### fql
254
-
255
- Make an arbitrary [FQL][] query
256
-
257
- [FQL]: http://developers.facebook.com/docs/reference/fql/
258
-
259
- rg.fql('SELECT name FROM page WHERE page_id="123"')
260
-
261
- #### fql_multi
262
-
263
- rg.fql_multi(:q1 => 'SELECT name FROM page WHERE page_id="123"',
264
- :q2 => 'SELECT name FROM page WHERE page_id="456"')
265
-
266
- #### old_rest
267
-
268
- Call functionality from Facebook's old REST API:
269
-
270
- rg.old_rest(
271
- 'stream.publish',
272
- { :message => 'Greetings',
273
- :attachment => {:name => 'Wikipedia',
274
- :href => 'http://wikipedia.org/',
275
- :caption => 'Wikipedia says hi.',
276
- :media => [{:type => 'image',
277
- :src => 'http://wikipedia.org/logo.png',
278
- :href => 'http://wikipedia.org/'}]
279
- }.to_json,
280
- :action_links => [{:text => 'Go to Wikipedia',
281
- :href => 'http://wikipedia.org/'}
282
- ].to_json
283
- },
284
- :auto_decode => false) # You'll need to set auto_decode to false for
285
- # this API request if Facebook is not returning
286
- # a proper formatted JSON response. Otherwise,
287
- # this could be omitted.
288
-
289
- # Some Old Rest API requires a special access token with app secret
290
- # inside of it. For those methods, use secret_old_rest instead of the
291
- # usual old_rest with common access token.
292
- rg.secret_old_rest('admin.getAppProperties', :properties => 'app_id')
293
-
294
- ### Utility Methods:
295
-
296
- #### parse_???
297
-
298
- All the methods that obtain an access_token will automatically save it.
299
-
300
- If you have the session in the cookies,
301
- then RestGraph can parse the cookies:
302
-
303
- rg.parse_cookies!(cookies)
304
-
305
- If you're writing a Rack application, you might want to parse
306
- the session directly from Rack env:
307
-
308
- rg.parse_rack_env!(env)
309
-
310
- #### access_token
311
-
312
- rg.access_token
313
-
314
- Data associated with the access_token (which might or might not
315
- available, depending on how the access_token was obtained).
316
-
317
- rg.data
318
- rg.data['uid']
319
- rg.data['expires']
320
-
321
- #### Default values
322
-
323
- Read from the rest-graph.yaml file.
324
-
325
- RestGraph.default_???
326
-
327
- ### Other ways of getting an access token
328
-
329
- #### authorize_url
330
-
331
- Returns the redirect URL for authorizing
332
-
333
- # https://graph.facebook.com/oauth/authorize?
334
- # client_id=123&redirect_uri=http%3A%2F%2Fw3.org%2F
335
- rg.authorize_url(:redirect_uri => 'http://w3.org/', :scope => 'email')
336
-
337
- #### authorize!
338
-
339
- Makes a call to Facebook to convert
340
- the authorization "code" into an access token:
341
-
342
- # https://graph.facebook.com/oauth/access_token?
343
- # code=CODE&client_id=123&client_secret=1829&
344
- # redirect_uri=http%3A%2F%2Fw3.org%2F
345
- rg.authorize!(:redirect_uri => 'http://w3.org/', :code => 'CODE')
346
-
347
- #### exchange_sessions
348
-
349
- Takes a session key from the old REST API
350
- (non-Graph API) and converts to an access token:
351
-
352
- # https://graph.facebook.com/oauth/exchange_sessions?sessions=SESSION
353
- rg.exchange_sessions(:sessions => params[:fb_sig_session_key])
354
-
355
- ## LICENSE:
356
-
357
- Apache License 2.0
358
-
359
- Copyright (c) 2010, Cardinal Blue
360
-
361
- Licensed under the Apache License, Version 2.0 (the "License");
362
- you may not use this file except in compliance with the License.
363
- You may obtain a copy of the License at
364
-
365
- <http://www.apache.org/licenses/LICENSE-2.0>
366
-
367
- Unless required by applicable law or agreed to in writing, software
368
- distributed under the License is distributed on an "AS IS" BASIS,
369
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
370
- See the License for the specific language governing permissions and
371
- limitations under the License.