rest-graph 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/README +16 -4
- data/lib/rest-graph.rb +31 -15
- data/lib/rest-graph/version.rb +1 -1
- data/test/test_rest-graph.rb +23 -18
- metadata +5 -5
data/CHANGES
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
= rest-graph changes history
|
2
2
|
|
3
|
+
== rest-graph 1.0.0 -- 2010-05-06
|
4
|
+
* now access_token is saved in data attributes.
|
5
|
+
* cookies related methods got renamed, and saved all data in RestGraph
|
6
|
+
* parse failed would return nil, while data is always a hash
|
7
|
+
|
3
8
|
== rest-graph 0.9.0 -- 2010-05-04
|
4
9
|
* renamed :server option to :graph_server
|
5
10
|
* added :fql_server option and fql support.
|
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= rest-graph 0.
|
1
|
+
= rest-graph 1.0.0
|
2
2
|
by Lin Jen-Shin (aka godfat-真常[http://godfat.org])
|
3
3
|
godfat (XD) godfat.org
|
4
4
|
|
@@ -12,6 +12,12 @@ by Lin Jen-Shin (aka godfat-真常[http://godfat.org])
|
|
12
12
|
|
13
13
|
super simple facebook open graph api client
|
14
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
|
+
|
15
21
|
== SYNOPSIS:
|
16
22
|
|
17
23
|
require 'rest-graph'
|
@@ -31,21 +37,27 @@ by Lin Jen-Shin (aka godfat-真常[http://godfat.org])
|
|
31
37
|
# for rack
|
32
38
|
lambda{ |env|
|
33
39
|
rg = RestGraph.new(:app_id => '123', :secret => '1829')
|
34
|
-
rg.
|
40
|
+
rg.parse_rack_env!(env) # auto save access_token if sig checked
|
35
41
|
rg.access_token # => ...
|
42
|
+
rg.data['uid'] # => facebook uid
|
36
43
|
}
|
37
44
|
|
38
45
|
# for fully blown cookies hash
|
39
46
|
rg = RestGraph.new(:app_id => '123', :secret => '1829')
|
40
|
-
rg.
|
47
|
+
rg.parse_cookies!(cookies) # auto save access_token if sig checked
|
41
48
|
rg.access_token # => ...
|
49
|
+
rg.data['uid'] # => facebook uid
|
42
50
|
|
43
51
|
# for fbs in cookies
|
44
52
|
app_id = '456'
|
45
53
|
rg = RestGraph.new(:app_id => '123', :secret => '1829')
|
46
54
|
fbs = cookies["fbs_#{rg.app_id}"]
|
47
|
-
rg.
|
55
|
+
rg.parse_fbs!(fbs) # auto save access_token if sig checked
|
48
56
|
rg.access_token # => ...
|
57
|
+
rg.data['uid'] # => facebook uid
|
58
|
+
|
59
|
+
# fql query
|
60
|
+
rg.fql('SELECT name FROM page WHERE page_id="123"')
|
49
61
|
|
50
62
|
== REQUIREMENTS:
|
51
63
|
|
data/lib/rest-graph.rb
CHANGED
@@ -3,10 +3,9 @@ require 'rest_client'
|
|
3
3
|
|
4
4
|
require 'cgi'
|
5
5
|
|
6
|
-
class RestGraph < Struct.new(:
|
6
|
+
class RestGraph < Struct.new(:data, :graph_server, :fql_server,
|
7
7
|
:accept, :lang, :auto_decode, :app_id, :secret)
|
8
8
|
def initialize o = {}
|
9
|
-
self.access_token = o[:access_token]
|
10
9
|
self.graph_server = o[:graph_server] || 'https://graph.facebook.com/'
|
11
10
|
self.fql_server = o[:fql_server] || 'https://api.facebook.com/'
|
12
11
|
self.accept = o[:accept] || 'text/javascript'
|
@@ -14,10 +13,27 @@ class RestGraph < Struct.new(:access_token, :graph_server, :fql_server,
|
|
14
13
|
self.auto_decode = o.key?(:auto_decode) ? o[:auto_decode] : true
|
15
14
|
self.app_id = o[:app_id]
|
16
15
|
self.secret = o[:secret]
|
16
|
+
self.access_token = o[:access_token]
|
17
17
|
|
18
18
|
check_arguments!
|
19
19
|
end
|
20
20
|
|
21
|
+
def access_token
|
22
|
+
data['access_token']
|
23
|
+
end
|
24
|
+
|
25
|
+
def access_token= token
|
26
|
+
data['access_token'] = token
|
27
|
+
end
|
28
|
+
|
29
|
+
def data
|
30
|
+
super || self.data = {}
|
31
|
+
end
|
32
|
+
|
33
|
+
def authorized?
|
34
|
+
!!access_token
|
35
|
+
end
|
36
|
+
|
21
37
|
def get path, opts = {}
|
22
38
|
request(graph_server, path, opts, :get)
|
23
39
|
end
|
@@ -41,18 +57,18 @@ class RestGraph < Struct.new(:access_token, :graph_server, :fql_server,
|
|
41
57
|
|
42
58
|
# cookies, app_id, secrect related below
|
43
59
|
|
44
|
-
def
|
45
|
-
self.
|
46
|
-
|
60
|
+
def parse_rack_env! env
|
61
|
+
self.data = env['HTTP_COOKIE'] =~ /fbs_#{app_id}="(.+?)"/ &&
|
62
|
+
check_sig_and_return_data(Rack::Utils.parse_query($1))
|
47
63
|
end
|
48
64
|
|
49
|
-
def
|
50
|
-
self.
|
65
|
+
def parse_cookies! cookies
|
66
|
+
self.data = parse_fbs!(cookies["fbs_#{app_id}"])
|
51
67
|
end
|
52
68
|
|
53
|
-
def
|
54
|
-
self.
|
55
|
-
|
69
|
+
def parse_fbs! fbs
|
70
|
+
self.data = fbs &&
|
71
|
+
check_sig_and_return_data(Rack::Utils.parse_query(fbs[1..-2]))
|
56
72
|
end
|
57
73
|
|
58
74
|
private
|
@@ -69,7 +85,7 @@ class RestGraph < Struct.new(:access_token, :graph_server, :fql_server,
|
|
69
85
|
require 'digest/md5'
|
70
86
|
require 'rack'
|
71
87
|
elsif app_id || secret
|
72
|
-
raise ArgumentError.new("You may want pass both"
|
88
|
+
raise ArgumentError.new("You may want to pass both" \
|
73
89
|
" app_id(#{app_id.inspect}) and" \
|
74
90
|
" secret(#{secret.inspect})")
|
75
91
|
end
|
@@ -100,14 +116,14 @@ class RestGraph < Struct.new(:access_token, :graph_server, :fql_server,
|
|
100
116
|
auto_decode ? JSON.parse(result) : result
|
101
117
|
end
|
102
118
|
|
103
|
-
def
|
104
|
-
cookies
|
119
|
+
def check_sig_and_return_data cookies
|
120
|
+
cookies if calculate_sig(cookies) == cookies['sig']
|
105
121
|
end
|
106
122
|
|
107
123
|
def calculate_sig cookies
|
108
|
-
|
124
|
+
args = cookies.reject{ |(k, v)| k == 'sig' }.sort.
|
109
125
|
map{ |a| a.join('=') }.join
|
110
126
|
|
111
|
-
Digest::MD5.hexdigest(
|
127
|
+
Digest::MD5.hexdigest(args + secret)
|
112
128
|
end
|
113
129
|
end
|
data/lib/rest-graph/version.rb
CHANGED
data/test/test_rest-graph.rb
CHANGED
@@ -86,15 +86,9 @@ describe RestGraph do
|
|
86
86
|
stub_request(:put, 'https://graph.facebook.com/feed/me').
|
87
87
|
with(:body => 'message=hi%20there').to_return(:body => '[]')
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
mock.proxy(rg = RestGraph.new).post_request('[]')
|
93
|
-
rg.put('feed/me', :message => 'hi there').
|
94
|
-
should == []
|
95
|
-
ensure
|
96
|
-
RestGraph.send(:private, :post_request) # TODO: rr, why??
|
97
|
-
end
|
89
|
+
mock.proxy(rg = RestGraph.new).post_request('[]')
|
90
|
+
rg.put('feed/me', :message => 'hi there').
|
91
|
+
should == []
|
98
92
|
end
|
99
93
|
|
100
94
|
it 'would not raise exception when encountering 500' do
|
@@ -118,17 +112,17 @@ describe RestGraph do
|
|
118
112
|
"fbs_#{app_id}=#{fbs}"
|
119
113
|
|
120
114
|
rg = RestGraph.new(:app_id => app_id, :secret => secret)
|
121
|
-
rg.
|
122
|
-
should
|
123
|
-
rg.access_token.should ==
|
115
|
+
rg.parse_rack_env!('HTTP_COOKIE' => http_cookie).
|
116
|
+
should.kind_of?(token ? Hash : NilClass)
|
117
|
+
rg.access_token.should == token
|
124
118
|
|
125
|
-
rg.
|
126
|
-
should
|
127
|
-
rg.access_token.should ==
|
119
|
+
rg.parse_cookies!({"fbs_#{app_id}" => fbs}).
|
120
|
+
should.kind_of?(token ? Hash : NilClass)
|
121
|
+
rg.access_token.should == token
|
128
122
|
|
129
|
-
rg.
|
130
|
-
should
|
131
|
-
rg.access_token.should ==
|
123
|
+
rg.parse_fbs!(fbs).
|
124
|
+
should.kind_of?(token ? Hash : NilClass)
|
125
|
+
rg.access_token.should == token
|
132
126
|
}
|
133
127
|
check.call(access_token)
|
134
128
|
fbs.chop!
|
@@ -136,6 +130,17 @@ describe RestGraph do
|
|
136
130
|
check.call(nil)
|
137
131
|
end
|
138
132
|
|
133
|
+
it 'would return true in authorized? if there is an access_token' do
|
134
|
+
RestGraph.new(:access_token => '1').authorized?.should == true
|
135
|
+
RestGraph.new(:access_token => nil).authorized?.should == false
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'would return nil if parse error, but not when call data directly' do
|
139
|
+
rg = RestGraph.new
|
140
|
+
rg.parse_cookies!({}).should == nil
|
141
|
+
rg.data .should == {}
|
142
|
+
end
|
143
|
+
|
139
144
|
it 'would do fql query with/without access_token' do
|
140
145
|
fql = 'SELECT name FROM likes where id="123"'
|
141
146
|
query = "query=#{fql}&format=json"
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: rest-graph
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
+
- 1
|
6
7
|
- 0
|
7
|
-
- 9
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Lin Jen-Shin (aka godfat \xE7\x9C\x9F\xE5\xB8\xB8)"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-06 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -41,8 +41,8 @@ dependencies:
|
|
41
41
|
segments:
|
42
42
|
- 1
|
43
43
|
- 4
|
44
|
-
-
|
45
|
-
version: 1.4.
|
44
|
+
- 3
|
45
|
+
version: 1.4.3
|
46
46
|
type: :development
|
47
47
|
version_requirements: *id002
|
48
48
|
- !ruby/object:Gem::Dependency
|