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/TODO
CHANGED
data/doc/ToC.md
ADDED
data/doc/dependency.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
|
2
|
+
# Dependency
|
3
|
+
|
4
|
+
## Introduction
|
5
|
+
|
6
|
+
Because rest-graph is designed to be lightweight and modular, it should
|
7
|
+
depend on as little things as possible, and give people the power to choose
|
8
|
+
their preference. rest-graph is depending on at least a HTTP client, and
|
9
|
+
optionally depending on a JSON parser if you want to auto-decode the JSON
|
10
|
+
from servers. (and `auto_decode` is actually the default.) It might also be
|
11
|
+
depending on [Rack][] for some operation, for example,
|
12
|
+
`RestGraph#parse_rack_env!` and `RestGraph#parse_cookies!` is using
|
13
|
+
`Rack::Utils.parse_query`. For those operations, Rack is needed.
|
14
|
+
|
15
|
+
[Rack]: https://github.com/rack/rack
|
16
|
+
|
17
|
+
## HTTP client (must pick one)
|
18
|
+
|
19
|
+
At the beginning, rest-graph uses [rest-client][], and is a must install
|
20
|
+
runtime dependency. Later, the support of [em-http-request][] is added,
|
21
|
+
so now you can pick either of them or both of them.
|
22
|
+
|
23
|
+
Usually, rest-client is used for synchronized (blocking) operations;
|
24
|
+
Contrarily, em-http-request is used for asynchronized (evented) operations.
|
25
|
+
|
26
|
+
If you don't know what's the difference between them, just use rest-client.
|
27
|
+
It's a lot easier to use, and have been tested more. If you don't know how
|
28
|
+
to pick, then you might be already using rest-client.
|
29
|
+
|
30
|
+
This is an example of using rest-client:
|
31
|
+
|
32
|
+
data = RestGraph.new.get('me')
|
33
|
+
|
34
|
+
This is an example of using em-http-request:
|
35
|
+
|
36
|
+
RestGraph.new.aget('me'){ |data| }
|
37
|
+
|
38
|
+
This is using em-http-request, too:
|
39
|
+
|
40
|
+
RestGraph.new.get('me', {}, {:async => true}){ |data| }
|
41
|
+
|
42
|
+
[rest-client]: https://github.com/archiloque/rest-client
|
43
|
+
[em-http-request]: https://github.com/igrigorik/em-http-request
|
44
|
+
|
45
|
+
## JSON parser (optional, but needed by default)
|
46
|
+
|
47
|
+
When `auto_decode` is set to true, rest-graph would use a JSON parser to
|
48
|
+
parse the JSON and return a Ruby object corresponding to that JSON. The most
|
49
|
+
widely used JSON parser is [json][], it has two distributions, one is `json`,
|
50
|
+
another one is `json_pure`. The former is written in Ruby and C, the latter
|
51
|
+
is purely written in Ruby. They are too widely used so you might want to
|
52
|
+
use it on your application, too. But [yajl-ruby][] is a lot more recommended,
|
53
|
+
it's... generally better, you can take a look on [yajl-ruby's README][]
|
54
|
+
|
55
|
+
rest-graph would first try to use Yajl, if it's not defined, then try JSON.
|
56
|
+
If it's not defined either, then it would try to `require 'yajl'`, rescue
|
57
|
+
`LoadError`, and `request 'json'`. The latter would either load json or
|
58
|
+
json_pure depending on the system.
|
59
|
+
|
60
|
+
So to force using yajl-ruby, you could require 'yajl-ruby' before rest-graph.
|
61
|
+
There's no way to force using json when yajl-ruby is already used though.
|
62
|
+
Anyone needs this? File a ticket on our [issue tracker][]
|
63
|
+
|
64
|
+
[json]: https://github.com/flori/json
|
65
|
+
[yajl-ruby]: https://github.com/brianmario/yajl-ruby
|
66
|
+
[yajl-ruby's README]: https://github.com/brianmario/yajl-ruby/blob/master/README.rdoc
|
67
|
+
[issue tracker]: https://github.com/cardinalblue/rest-graph/issues
|
68
|
+
|
69
|
+
## Rack (optional, needed when parsing cookie)
|
70
|
+
|
71
|
+
Actually I wonder if anyone would not use [Rack][]. But since it's really
|
72
|
+
optional, so I'll just leave it as optional.
|
73
|
+
|
74
|
+
## RR (optional, needed when using rest-graph/test_util)
|
75
|
+
|
76
|
+
test_util is built on top of [RR][], so to use test_util, RR is required.
|
77
|
+
|
78
|
+
[RR]: https://github.com/btakita/rr
|
data/doc/design.md
ADDED
@@ -0,0 +1,206 @@
|
|
1
|
+
|
2
|
+
# Design
|
3
|
+
|
4
|
+
## Introduction
|
5
|
+
|
6
|
+
rest-graph is a lightweight Facebook Graph API client. By lightweight, it
|
7
|
+
means it's modular and compact, and only provides essential functionality.
|
8
|
+
It's designed to be transparent to Facebook Graph API, so it doesn't try to
|
9
|
+
fix Facebook's bugs nor inconsistency problems. People should be able to
|
10
|
+
read Facebook's documentation (though sometimes it's not quite helpful) in
|
11
|
+
order to use rest-graph, instead of learning how rest-graph would work.
|
12
|
+
(of course, Ruby experience is required.)
|
13
|
+
|
14
|
+
In other words, before starts, you might need to know how Facebook's Graph
|
15
|
+
API works, for example, how to fetch data, how to do authentication, etc.
|
16
|
+
Here's the links:
|
17
|
+
|
18
|
+
* Graph API: <http://developers.facebook.com/docs/reference/api>
|
19
|
+
* Authentication: <http://developers.facebook.com/docs/authentication>
|
20
|
+
|
21
|
+
For advanced usage, you might want to read the followings, too:
|
22
|
+
|
23
|
+
* FQL: <http://developers.facebook.com/docs/reference/fql>
|
24
|
+
* Old REST API: <http://developers.facebook.com/docs/reference/rest>
|
25
|
+
|
26
|
+
Since rest-graph is trying to be transparent to Facebook Graph API, some
|
27
|
+
might find it's not so useful because of Facebook's bugs or inconsistency
|
28
|
+
problems. This might be a disadvantage comparing to others client libraries
|
29
|
+
which mimic the issues for you, but the advantage would be for people who
|
30
|
+
have already known how Facebook Graph API works, say, people who used to
|
31
|
+
develop Facebook Apps with PHP or any other tools, could easily know how to
|
32
|
+
use rest-graph with their old knowledges. On the other hand, if Facebook
|
33
|
+
fixed their bugs or inconsistency problems, you don't need to wait for
|
34
|
+
rest-graph fixing the problems. You will directly depend on Facebook,
|
35
|
+
but not depend on rest-graph which depends on Facebook. More layers,
|
36
|
+
more problems. rest-graph is a client library, but not Facebook framework.
|
37
|
+
|
38
|
+
Still, if the inconsistency problems are very obvious and would not change
|
39
|
+
in the near future, for example, `oauth/access_token` API would not return
|
40
|
+
a JSON as typical Graph APIs do, instead, it returns a query string as in
|
41
|
+
URL. In this case, rest-graph uses `Rack::Utils.parse_query` to parse the
|
42
|
+
query for you. If you feel there are more cases rest-graph should handle
|
43
|
+
like this, please feel free to file a ticket on our [issue tracker][] on
|
44
|
+
Github.
|
45
|
+
|
46
|
+
* <https://github.com/cardinalblue/rest-graph/issues>
|
47
|
+
|
48
|
+
Or you could start a topic on our mailing list:
|
49
|
+
|
50
|
+
* <http://groups.google.com/group/rest-graph/topics>
|
51
|
+
|
52
|
+
Either one is welcomed.
|
53
|
+
|
54
|
+
## Name
|
55
|
+
|
56
|
+
[rest-graph][] is named after its first dependency [rest-client][].
|
57
|
+
|
58
|
+
[rest-graph]: https://github.com/cardinalblue/rest-graph
|
59
|
+
[rest-client]: https://github.com/archiloque/rest-client
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
rest-graph is licensed under Apache License 2.0.
|
64
|
+
|
65
|
+
## Target Usage
|
66
|
+
|
67
|
+
rest-graph is split into many parts to better target different users coming
|
68
|
+
from different areas. Essentially, the core functionality is all in one file
|
69
|
+
which is `lib/rest-graph/core.rb`. You can copy that file and put into your
|
70
|
+
projects' load path, or use gem to install rest-graph, and then just require
|
71
|
+
the file with `require 'rest-graph/core'`. If you don't care about memory
|
72
|
+
footprint or code bloats, want something that "Just Work™" it's ok to
|
73
|
+
just `require 'rest-graph'`. It would try to load up anything you *might*
|
74
|
+
or *might not* need. It should Just Work™ out of the box, otherwise,
|
75
|
+
please file a ticket on our [issue tracker][].
|
76
|
+
|
77
|
+
[issue tracker]: https://github.com/cardinalblue/rest-graph/issues
|
78
|
+
|
79
|
+
Target usages are as following:
|
80
|
+
|
81
|
+
* No matter where rest-graph whould be used, for convenience and laziness,
|
82
|
+
just `require 'rest-graph'`. Then you're good to go.
|
83
|
+
|
84
|
+
* Used in backend engine or non-web application. You might only want the
|
85
|
+
core functionality that rest-graph provides. In this case, simply use
|
86
|
+
`require 'rest-graph/core'`
|
87
|
+
|
88
|
+
* Used in web applications, and then mostly you'll want more than the core.
|
89
|
+
For example, how to get the access token, how to authenticate to Facebook,
|
90
|
+
and how to use different settings (e.g. app_id, secret, etc) in different
|
91
|
+
environments. In this case, you'll want `require 'rest-graph/rails_util'`
|
92
|
+
(if your web framework is Rails) and `require 'rest-graph/config_util'`.
|
93
|
+
See below for usages of those two extra utilities.
|
94
|
+
|
95
|
+
## File Structure
|
96
|
+
|
97
|
+
rest-graph is modular, so utilities are all separated. Different `require`
|
98
|
+
will get you different things. Here we list all different requires.
|
99
|
+
|
100
|
+
* `require 'rest-graph'`
|
101
|
+
|
102
|
+
This is for convenience and lazies which just loads everything.
|
103
|
+
You can see below for usages.
|
104
|
+
|
105
|
+
* `require 'rest-graph/core'`
|
106
|
+
|
107
|
+
This is for the core functionality. Other than API calls (which is
|
108
|
+
documented in [rdoc][]), you might want to have some default values
|
109
|
+
for `RestGraph.new`, then you don't have to do this all the time:
|
110
|
+
`RestGraph.new(:app_id => '1829')`. For example, you might want:
|
111
|
+
`RestGraph.new.app_id` to return value `'1829'` instead of `nil`,
|
112
|
+
and still be able to overwrite it when passing `:app_id` like this:
|
113
|
+
`RestGraph.new(:app_id => 'abcd')`. If so, you'll do:
|
114
|
+
|
115
|
+
require 'rest-graph/core'
|
116
|
+
|
117
|
+
module MyRestGraphSettings
|
118
|
+
def default_app_id
|
119
|
+
'1829'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
RestGraph.send(:extend, MyRestGraphSettings)
|
124
|
+
|
125
|
+
RestGraph.new.app_id # => '1829'
|
126
|
+
|
127
|
+
Or you can simply define it in `RestGraph`.
|
128
|
+
|
129
|
+
class RestGraph
|
130
|
+
def self.default_app_id
|
131
|
+
'1829'
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
RestGraph.new.app_id # => '1829'
|
136
|
+
|
137
|
+
If you want to set those defaults in a config file with different
|
138
|
+
environments, then `require 'rest-graph/config_util'` is for you.
|
139
|
+
See below.
|
140
|
+
|
141
|
+
[rdoc]: http://rdoc.info/projects/cardinalblue/rest-graph
|
142
|
+
|
143
|
+
* `require 'rest-graph/config_util`
|
144
|
+
|
145
|
+
This is for automatically reading settings from a certain config file.
|
146
|
+
To use it, use: `RestGraph.load_config(path_to_yaml_file, environment)`
|
147
|
+
A config file would look like this: [rest-graph.yaml][] You can embed
|
148
|
+
ERB template in it. After the config has been loaded, every call to
|
149
|
+
`RestGraph.new` would respect those settings. e.g. `RestGraph.new.app_id`
|
150
|
+
would return the app_id you set in the config file, instead of `nil`.
|
151
|
+
|
152
|
+
[rest-graph.yaml]: ../test/config/rest-graph.yaml
|
153
|
+
|
154
|
+
* `require 'rest-graph/rails_util'`
|
155
|
+
|
156
|
+
This is for people using Rails. (compatible and tested with both Rails 2
|
157
|
+
and Rails 3) `include RestGraph::RailsUtil` in your controller would
|
158
|
+
give you `rest_graph_setup` and `rest_graph` methods in both controller
|
159
|
+
and helper. The former is used to configure the behaviour for each action,
|
160
|
+
the latter is used to access the instance of `RestGraph` which is setup in
|
161
|
+
`rest_graph_setup`.
|
162
|
+
|
163
|
+
See [rails.md][] to learn more about this utility.
|
164
|
+
|
165
|
+
[rails.md]: rails.md
|
166
|
+
|
167
|
+
* `require 'rest-graph/test_util'`
|
168
|
+
|
169
|
+
Quoted from Wikipedia's description about [Unit testing][]:
|
170
|
+
|
171
|
+
> Ideally, each test case is independent from the others: substitutes
|
172
|
+
> like method stubs, mock objects, fakes and test harnesses can be
|
173
|
+
> used to assist testing a module in isolation.
|
174
|
+
|
175
|
+
We won't even want to be depending on the Internet. It's slow, and
|
176
|
+
unstable. You might have already tried [webmock][] or [fakeweb][],
|
177
|
+
they are good tools, but a bit tedious to use if we're faking graph
|
178
|
+
API calls. That's why `RestGraph::TestUtil` comes into play. It uses
|
179
|
+
[RR][] to make stubs for API calls, and you can change the data that
|
180
|
+
the stubs provide. This way, it's a lot easier to test your application.
|
181
|
+
|
182
|
+
You can emulate a user login with `RestGraph::TestUtil.login(1234)`,
|
183
|
+
which will give you a fake user data upon calling `/me`. It will
|
184
|
+
give you a fake access token, too.
|
185
|
+
|
186
|
+
See [test.md][] to learn more about this utility.
|
187
|
+
|
188
|
+
See Martin Fowler's great article to learn more about mocks:
|
189
|
+
[Mocks Aren't Stubs][]
|
190
|
+
|
191
|
+
[Unit testing]: http://en.wikipedia.org/wiki/Unit_testing
|
192
|
+
[webmock]: https://github.com/bblimke/webmock
|
193
|
+
[fakeweb]: https://github.com/chrisk/fakeweb
|
194
|
+
[RR]: https://github.com/btakita/rr
|
195
|
+
[test.md]: test.md
|
196
|
+
[Mocks Aren't Stubs]: http://martinfowler.com/articles/mocksArentStubs.html
|
197
|
+
|
198
|
+
* `require 'rest-graph/facebook_util'`
|
199
|
+
|
200
|
+
Facebook has some very inconsistent behaviour. This utility is here to
|
201
|
+
fix those inconsistencies, providing you a more comprehensive operation
|
202
|
+
on data. Also, it has permission list build in, without the trouble
|
203
|
+
looking through Facebook's documentation.
|
204
|
+
|
205
|
+
This utility is not fully and carefully written, please file a ticket
|
206
|
+
on our [issue tracker][] if you want something not presented currently.
|
data/doc/rails.md
ADDED
data/doc/test.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
# Test
|
3
|
+
|
4
|
+
## Introduction
|
5
|
+
|
6
|
+
A collection of tools integrated with [RR][] to ease the pain of
|
7
|
+
testing. There are 3 levels of tools to stub the result of
|
8
|
+
calling APIs. The highest level is `TestUtil.login(1234)` which
|
9
|
+
would stub a number of results to pretend the user 1234 is
|
10
|
+
logged-in.
|
11
|
+
|
12
|
+
The second level are the get/post/put/delete methods for
|
13
|
+
TestUtil. For example, to make rg.get('1234') return a
|
14
|
+
particular value (such as a hash {'a' => 1}), use
|
15
|
+
TestUtil.get('1234'){ {'a' => 1} } to set it up to return
|
16
|
+
the specified value (typically a hash).
|
17
|
+
|
18
|
+
The third level is for setting default_data and default_response
|
19
|
+
for TestUtil. The default_data is the default value for rg.data,
|
20
|
+
which includes the access_token and the user_id (uid). The
|
21
|
+
default_response is the response given by any RestGraph API call
|
22
|
+
(e.g. get, post) when no explicit response has been defined in
|
23
|
+
the second level.
|
24
|
+
|
25
|
+
To use TestUtil, remember to install RR (gem install rr) and
|
26
|
+
require 'rest-graph/test_util'. Then put
|
27
|
+
RestGraph::TestUtil.setup before any test case starts, and put
|
28
|
+
RestGraph::TestUtil.teardown after any test case ends. Setup
|
29
|
+
would stub default_data and default_response for you, and
|
30
|
+
teardown would remove any stubs on RestGraph. For Rails, you
|
31
|
+
might want to put these in test_helper.rb under "setup" and
|
32
|
+
"teardown" block, just as the name suggested. For bacon or
|
33
|
+
rspec style testing, these can be placed in the "before" and
|
34
|
+
"after" blocks.
|
35
|
+
|
36
|
+
In addition, you can get the API calls history via
|
37
|
+
RestGraph::TestUtil.history. This would get cleaned up in
|
38
|
+
RestGraph::TestUtil.teardown as well.
|
39
|
+
|
40
|
+
[RR]: https://github.com/btakita/rr
|
41
|
+
|
42
|
+
## Login emulation
|
43
|
+
|
44
|
+
## default_response
|
45
|
+
|
46
|
+
##
|
data/doc/tutorial.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
The code in this tutorial could be found on [samplergthree][]
|
2
|
+
|
3
|
+
[samplergthree]: https://github.com/cardinalblue/samplergthree
|
4
|
+
|
5
|
+
# How to build a Facebook application within Rails 3 using the RestGraph gem
|
6
|
+
|
7
|
+
1. Before you start, I strongly recommend reading these:
|
8
|
+
|
9
|
+
* Apps on Facebook.com -> <http://developers.facebook.com/docs/guides/canvas/>
|
10
|
+
* Graph API -> <http://developers.facebook.com/docs/reference/api/>
|
11
|
+
* Authentication -> <http://developers.facebook.com/docs/authentication/>
|
12
|
+
* Heroku: Building a Facebook Application -> <http://devcenter.heroku.com/articles/facebook/>
|
13
|
+
|
14
|
+
|
15
|
+
2. Go to [FB Developers website](http://facebook.com/developers) and create a new FB app. Set its canvas name, canvas url and your site URL. Make sure the canvas type is set to "iframe" (which should be the case by default).
|
16
|
+
|
17
|
+
|
18
|
+
3. Build a new Rails application.
|
19
|
+
|
20
|
+
rails new <name>
|
21
|
+
|
22
|
+
|
23
|
+
4. Declare RestGraph and its dependencies in the Gemfile. Add these lines:
|
24
|
+
|
25
|
+
gem 'rest-graph'
|
26
|
+
|
27
|
+
# these gems are used in rest-graph
|
28
|
+
gem 'rest-client', '>=1.6'
|
29
|
+
gem 'json' # you may also use other JSON parsers/generators, i.e. 'yajl-ruby' or 'json_pure'
|
30
|
+
|
31
|
+
|
32
|
+
5. In order to configure your Rails application for the Facebook application you created, you must create a rest-graph.yaml file in your /config directory and fill it with your Facebook configuration. If you plan to run your application in the Facebook canvas, also provide a canvas name.
|
33
|
+
|
34
|
+
Example:
|
35
|
+
|
36
|
+
development:
|
37
|
+
app_id: 'XXXXXXXXXXXXXX'
|
38
|
+
secret: 'YYYYYYYYYYYYYYYYYYYYYYYYYYY'
|
39
|
+
callback_host: 'my.dev.host.com'
|
40
|
+
|
41
|
+
production:
|
42
|
+
app_id: 'XXXXXXXXXXXXXX'
|
43
|
+
secret: 'YYYYYYYYYYYYYYYYYYYYYYYYYYY'
|
44
|
+
canvas: 'yourcanvasname'
|
45
|
+
callback_host: 'my.production.host.com'
|
46
|
+
|
47
|
+
|
48
|
+
If you push to Heroku, your production callback_host should be `yourappname.heroku.com`. You can also access your app directly running `rails server` (or just `rails s`) in your console, but if you do not have an external IP address (e.g. you are behind a NAT), you will need to use a service called a tunnel in order to make your application accessible to the outer world (and Facebook callbacks). You'll find more information on setting up a tunnel here: <http://tunnlr.com/>.
|
49
|
+
|
50
|
+
6. Let's create a first controller for your app - ScratchController.
|
51
|
+
|
52
|
+
rails generate controller Scratch
|
53
|
+
|
54
|
+
7. The next step will be to include rest-graph in your controller. You should put this line in:
|
55
|
+
|
56
|
+
include RestGraph::RailsUtil
|
57
|
+
|
58
|
+
Now you can make use of the RestGraph commands :)
|
59
|
+
|
60
|
+
8. To actually use rest-graph in a controller action, you will need to first call "rest_graph_setup", which reads the configuration from the rest-graph.yaml and creates a rest_graph object. Let's set this up in a before_filter.
|
61
|
+
|
62
|
+
Add this line after the `include RestGraph::RailsUtil`:
|
63
|
+
|
64
|
+
before_filter :filter_setup_rest_graph
|
65
|
+
|
66
|
+
And declare filter_setup_rest_graph as a private function:
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def filter_setup_rest_graph
|
71
|
+
rest_graph_setup(:auto_authorize => true)
|
72
|
+
end
|
73
|
+
|
74
|
+
The `:auto_authorize` argument of rest_graph_setup tells RestGraph to redirect users to the app authorization page if the app is not authorized yet.
|
75
|
+
|
76
|
+
Hooray! You can now perform all kinds of Graph API operations using the rest_graph object.
|
77
|
+
|
78
|
+
9. Let's start with following sample action in the Scratch controller:
|
79
|
+
|
80
|
+
def me
|
81
|
+
render :text => rest_graph.get('me').inspect
|
82
|
+
end
|
83
|
+
|
84
|
+
10. To run this, go to the /config/routes.rb file to set up the default routing. For now you will just need this line:
|
85
|
+
|
86
|
+
match ':controller/:action'
|
87
|
+
|
88
|
+
11. Commit your change in git, and then push to Heroku:
|
89
|
+
|
90
|
+
git add .
|
91
|
+
git commit -m "first test of rest-graph using scratch/me"
|
92
|
+
git push heroku master
|
93
|
+
|
94
|
+
After you push your app to Heroku, you can open <http://yourappname.heroku.com/scratch/me> in your browser. If you are not yet logged into Facebook, it will ask you to log in. If you are logged in your Facebook account, this address should redirect you to the authorization page and ask if you want to let your application access your public information. After you confirm, you should be redirected back to 'scratch/me' action which will show your basic information.
|
95
|
+
|
96
|
+
12. To see other information, such as your home feed, is very easy. You can add another sample action to your controller:
|
97
|
+
|
98
|
+
def feed
|
99
|
+
render :text => rest_graph.get('me/home').inspect
|
100
|
+
end
|
101
|
+
|
102
|
+
If you will push the changes to Heroku and go to <http://yourappname.heroku.com/scratch/feed>, the page should display a JSON hash with all the data from your Facebook home feed.
|
103
|
+
|
104
|
+
|
105
|
+
13. Now let's try to access your Facebook wall. You need to add a new action to your controller:
|
106
|
+
|
107
|
+
def wall
|
108
|
+
render :text => rest_graph.get('me/feed').inspect
|
109
|
+
end
|
110
|
+
|
111
|
+
Note that Facebook's naming is such that your home news feeds is accessed via `me/home` and your profile walls is accessed via `me/feed` ...
|
112
|
+
|
113
|
+
Actually, I need to warn you that this time the action won't work properly. Why? Because users didn't grant you the permission to access their walls! You need to ask them for this special permission and that means you need to add something more to your controller.
|
114
|
+
|
115
|
+
So, we will organize all the permissions we need as a scope and pass them to the rest_graph_setup call. I find it handy to make the scope array and declare what kind of permissions I need just inside this array. If you feel it's a good idea, you can add this line to your private setup function, just before you call rest_graph_setup:
|
116
|
+
|
117
|
+
scope = []
|
118
|
+
scope << 'read_stream'
|
119
|
+
|
120
|
+
The only permission you need right now is the 'read_stream' permission. You can find out more about different kinds of user permissions here: <http://developers.facebook.com/docs/authentication/permissions/>
|
121
|
+
|
122
|
+
You also need to add the auto_authorize_scope argument to the rest_graph_setup. It will look this way now:
|
123
|
+
|
124
|
+
rest_graph_setup(:auto_authorize => true, :auto_authorize_scope => scope.join(','))
|
125
|
+
|
126
|
+
As you see, you might as well pass the argument like this `:auto_authorize_scope => 'read_stream'`, but once you have to get a lot of different permissions, it's very useful to put them all in an array, because it's more readable and you can easily delete or comment out any one of them.
|
127
|
+
|
128
|
+
Now save your work and push it to Heroku or just try in your tunneled development environment. /scratch/wall URL should give you the hash with user's wall data now!
|
129
|
+
|
130
|
+
Remember. Anytime you need to get data of a new kind, you need to ask user for a certain permission first and that means you need to declare this permission in your scope array!
|
131
|
+
|
132
|
+
14. What else? If you know how to deal with hashes then you will definitely know how to get any kind of data you want using the rest_graph object. Let's say you want to get a last object from a user's wall (last in terms of time, last posted, so the first on the wall and therefore first to Ruby). Let's take a look at the /scratch/feed page. The hash which is printed on this page has 2 keys - data and paging. Let's leave the paging key aside. What's more interesting here comes as a value of 'data'. So the last object in any user's wall will be simply:
|
133
|
+
|
134
|
+
rest_graph.get('me/feed')['data'].first
|
135
|
+
|
136
|
+
Now let's say you want only to keep the name of the author of this particular object. You can get it by using:
|
137
|
+
|
138
|
+
rest_graph.get('me/feed')['data'].first['from']['name']
|
139
|
+
|
140
|
+
That's it!
|
141
|
+
|
142
|
+
15. More information on customizing RestGraph and its functions are to be found here: <https://github.com/cardinalblue/rest-graph>
|