graph-api 0.9.5 → 0.9.6
Sign up to get free protection for your applications and to get access to all the features.
- data/graph_api.gemspec +1 -1
- data/lib/graph_api.rb +37 -31
- data/lib/graph_api/version.rb +1 -1
- data/readme.md +8 -8
- data/spec/graph_api_spec.rb +18 -17
- metadata +2 -2
data/graph_api.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.email = ['nick@nickbarth.ca']
|
11
11
|
gem.summary = 'A Ruby Gem for common Facebook Graph API tasks.'
|
12
12
|
gem.description = 'GraphAPI is a Ruby Gem containing some common tasks to help manage Facebook users using the Facebook Graph API.'
|
13
|
-
gem.homepage = 'https://github.com/nickbarth/
|
13
|
+
gem.homepage = 'https://github.com/nickbarth/GraphAPI'
|
14
14
|
|
15
15
|
gem.add_dependency('rake')
|
16
16
|
gem.add_dependency('json')
|
data/lib/graph_api.rb
CHANGED
@@ -1,73 +1,79 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'rest_client'
|
3
|
+
require 'graph_api/version'
|
3
4
|
|
4
5
|
# Public: Various methods useful for interfacing with Facebook Graph protocol.
|
5
6
|
#
|
6
7
|
# Example:
|
7
8
|
#
|
8
9
|
# get '/facebook_login' do
|
9
|
-
# redirect FaceGraph
|
10
|
+
# redirect FaceGraph.auth_url
|
10
11
|
# end
|
11
12
|
#
|
12
13
|
# get '/facebook_auth' do
|
13
|
-
# @facebook_user = GraphAPI
|
14
|
-
# @photo = GraphAPI
|
14
|
+
# @facebook_user = GraphAPI.fetch_user(params[:code])
|
15
|
+
# @photo = GraphAPI.fetch_photo(@facebook_user['access_token'])
|
15
16
|
# render :signed_in
|
16
17
|
# end
|
17
18
|
#
|
18
19
|
module GraphAPI
|
19
|
-
# Public: Required
|
20
|
+
# Public: Required setting used for Facebook private application secret.
|
20
21
|
#
|
21
22
|
# Example:
|
22
23
|
#
|
23
|
-
#
|
24
|
+
# @app_secret = '124ca2a483f12723cafa7a5da33a3492'
|
25
|
+
attr_accessor :app_secret
|
24
26
|
|
25
|
-
# Public: Required
|
27
|
+
# Public: Required setting used for Facebook private application client ID.
|
26
28
|
#
|
27
29
|
# Example
|
28
30
|
#
|
29
|
-
#
|
31
|
+
# @client_id = '234513432316919'
|
32
|
+
attr_accessor :client_id
|
30
33
|
|
31
|
-
# Public: Reqired
|
34
|
+
# Public: Reqired setting used for Facebook call back URL when receiving the Facebook connect code param.
|
32
35
|
#
|
33
36
|
# Example
|
34
37
|
#
|
35
|
-
#
|
38
|
+
# @callback_url = nil
|
39
|
+
attr_accessor :callback_url
|
36
40
|
|
37
|
-
# Public: Required
|
41
|
+
# Public: Required setting used for setting Facebook application requirements.
|
38
42
|
#
|
39
43
|
# Example
|
40
44
|
#
|
41
|
-
#
|
45
|
+
# @access_scope = [:offline_access, :email, :user_photos, :user_location, :user_about_me]
|
46
|
+
attr_accessor :access_scope
|
42
47
|
|
43
|
-
# Public: Required
|
48
|
+
# Public: Required setting used for setting the fields pulled for.
|
44
49
|
#
|
45
50
|
# Example
|
46
51
|
#
|
47
|
-
#
|
52
|
+
# @user_fields = [:id, :picture, :name, :gender, :link, :email, :verified, :bio]
|
53
|
+
attr_accessor :user_fields
|
48
54
|
|
49
|
-
# Public: Method for configuring the setting
|
55
|
+
# Public: Method for configuring the setting settings for a nicer syntax.
|
50
56
|
#
|
51
57
|
# Example:
|
52
58
|
#
|
53
|
-
# GraphAPI.config
|
54
|
-
#
|
59
|
+
# GraphAPI.config app_secret: '124ca2a483f12723cafa7a5da33a3492',
|
60
|
+
# client_id: '234513432316919'
|
55
61
|
#
|
56
|
-
def config(
|
57
|
-
|
58
|
-
|
62
|
+
def config(settings)
|
63
|
+
settings.each do |setting, value|
|
64
|
+
self.send("#{setting}=", value)
|
59
65
|
end
|
60
66
|
end
|
61
67
|
|
62
|
-
# Public: Creates and returns a Facebook Authentication URL based on the supplied
|
68
|
+
# Public: Creates and returns a Facebook Authentication URL based on the supplied settings.
|
63
69
|
#
|
64
|
-
# callback_url - With
|
70
|
+
# callback_url - With @callback_url set to nil setting this parameter will use
|
65
71
|
# the sent callback. This is useful when you're using dynamic
|
66
72
|
# URIs with subdomains.
|
67
73
|
def auth_url(callback_url=nil)
|
68
|
-
"https://graph.facebook.com/oauth/authorize?client_id=#{
|
69
|
-
"&redirect_uri=#{
|
70
|
-
"&scope=#{
|
74
|
+
"https://graph.facebook.com/oauth/authorize?client_id=#{@client_id}" +
|
75
|
+
"&redirect_uri=#{@callback_url or callback_url}" +
|
76
|
+
"&scope=#{@access_scope.join(',')}"
|
71
77
|
end
|
72
78
|
|
73
79
|
# Public: Requests the Access Token from the Facebook Graph API and returns it as a string.
|
@@ -75,13 +81,13 @@ module GraphAPI
|
|
75
81
|
# code - The code parameter is the param you receive when the Facebook Graph
|
76
82
|
# API hits your call back URI.
|
77
83
|
#
|
78
|
-
# callback_url - With
|
84
|
+
# callback_url - With @callback_url set to nil setting this parameter will use
|
79
85
|
# the sent callback. This is useful when you're using dynamic
|
80
86
|
# URIs with subdomains.
|
81
87
|
def fetch_token(code, callback_url=nil)
|
82
|
-
RestClient.get('https://graph.facebook.com/oauth/access_token', { client_id:
|
83
|
-
redirect_uri: (
|
84
|
-
client_secret:
|
88
|
+
RestClient.get('https://graph.facebook.com/oauth/access_token', { client_id: @client_id,
|
89
|
+
redirect_uri: (@callback_url or callback_url),
|
90
|
+
client_secret: @app_secret,
|
85
91
|
code: code
|
86
92
|
})[/access_token=(.+?)&/, 1]
|
87
93
|
end
|
@@ -97,16 +103,16 @@ module GraphAPI
|
|
97
103
|
end
|
98
104
|
|
99
105
|
# Public: Returns a Facebook user array containing the fields set by the
|
100
|
-
#
|
106
|
+
# @user_fields setting and the access token for convenience.
|
101
107
|
def request_user(access_token)
|
102
|
-
request("/me?&fields=#{
|
108
|
+
request("/me?&fields=#{@user_fields.join(',')}", access_token).
|
103
109
|
merge('access_token' => access_token)
|
104
110
|
end
|
105
111
|
|
106
112
|
# Public: Convenience method for fetching a Facebook user array from the
|
107
113
|
# Facebook token code.
|
108
114
|
#
|
109
|
-
# callback_url - With
|
115
|
+
# callback_url - With @callback_url set to nil setting this parameter will use
|
110
116
|
# the sent callback. This is useful when you're using dynamic
|
111
117
|
# URIs with subdomains.
|
112
118
|
def fetch_user(code, callback_url=nil)
|
data/lib/graph_api/version.rb
CHANGED
data/readme.md
CHANGED
@@ -13,23 +13,23 @@ Here is how to use it.
|
|
13
13
|
|
14
14
|
You will have to configure the module before using it. Here is an example setup.
|
15
15
|
|
16
|
-
GraphAPI.config
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
GraphAPI.config app_secret: '124ca2a483f12723cafa7a5da33a3492' # The Facebook Application Secret
|
17
|
+
client_id: '234513432316919' # The Facebook Client ID
|
18
|
+
callback_url: 'http://example.com/facebook_callback/' # URI for receiving the Facebook connect code param
|
19
|
+
access_scope: [:offline_access, :email, :user_photos, :user_location] # The Facebook application requirements
|
20
|
+
user_fields: [:id, :picture, :name, :gender, :link, :email] # The user fields pulled for
|
21
21
|
|
22
22
|
### Add it to your Application
|
23
23
|
|
24
24
|
Once configured you will be able to use any of its functions in your application. Here is basic example using Sinatra.
|
25
25
|
|
26
26
|
get '/facebook_login' do
|
27
|
-
redirect FaceGraph
|
27
|
+
redirect FaceGraph.auth_url
|
28
28
|
end
|
29
29
|
|
30
30
|
get '/facebook_auth' do
|
31
|
-
@facebook_user = GraphAPI
|
32
|
-
@photo = GraphAPI
|
31
|
+
@facebook_user = GraphAPI.fetch_user(params[:code])
|
32
|
+
@photo = GraphAPI.fetch_photo(@facebook_user['access_token'])
|
33
33
|
render :signed_in
|
34
34
|
end
|
35
35
|
|
data/spec/graph_api_spec.rb
CHANGED
@@ -7,38 +7,39 @@ describe GraphAPI do
|
|
7
7
|
|
8
8
|
describe '#config' do
|
9
9
|
it 'should configuration constants to be set' do
|
10
|
-
GraphAPI.config
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
GraphAPI
|
17
|
-
GraphAPI
|
18
|
-
GraphAPI
|
19
|
-
GraphAPI
|
10
|
+
GraphAPI.config app_secret: 'APP_SECRET',
|
11
|
+
client_id: 'CLIENT_ID',
|
12
|
+
callback_url: 'CALLBACK_URL',
|
13
|
+
access_scope: 'ACCESS_SCOPE',
|
14
|
+
user_fields: 'USER_FIELDS'
|
15
|
+
|
16
|
+
GraphAPI.app_secret.should == 'APP_SECRET'
|
17
|
+
GraphAPI.client_id.should == 'CLIENT_ID'
|
18
|
+
GraphAPI.callback_url.should == 'CALLBACK_URL'
|
19
|
+
GraphAPI.access_scope.should == 'ACCESS_SCOPE'
|
20
|
+
GraphAPI.user_fields.should == 'USER_FIELDS'
|
20
21
|
end
|
21
22
|
end
|
22
23
|
|
23
24
|
describe '#auth_url' do
|
24
25
|
before(:each) do
|
25
|
-
|
26
|
+
GraphAPI.access_scope = [:SCOPE1, :SCOPE2]
|
26
27
|
end
|
27
28
|
|
28
29
|
it 'should use generate a URI' do
|
29
|
-
|
30
|
+
GraphAPI.callback_url = nil
|
30
31
|
GraphAPI.auth_url('CALLBACK').should == 'https://graph.facebook.com/oauth/authorize?client_id=CLIENT_ID&redirect_uri=CALLBACK&scope=SCOPE1,SCOPE2'
|
31
32
|
end
|
32
33
|
|
33
34
|
it 'should use CLIENT_ID const if avaliable' do
|
34
|
-
|
35
|
+
GraphAPI.callback_url = 'CALLBACK_URL'
|
35
36
|
GraphAPI.auth_url.should == 'https://graph.facebook.com/oauth/authorize?client_id=CLIENT_ID&redirect_uri=CALLBACK_URL&scope=SCOPE1,SCOPE2'
|
36
37
|
end
|
37
38
|
end
|
38
39
|
|
39
40
|
describe '#fetch_token' do
|
40
41
|
it 'should return the access token' do
|
41
|
-
|
42
|
+
GraphAPI.callback_url = 'CALLBACK_URL'
|
42
43
|
RestClient.should_receive(:get).with('https://graph.facebook.com/oauth/access_token', { client_id: 'CLIENT_ID',
|
43
44
|
redirect_uri: 'CALLBACK_URL',
|
44
45
|
client_secret: 'APP_SECRET',
|
@@ -57,7 +58,7 @@ describe GraphAPI do
|
|
57
58
|
|
58
59
|
describe '#request_user' do
|
59
60
|
it 'should return a user' do
|
60
|
-
|
61
|
+
GraphAPI.user_fields = [:FIELD1, :FIELD2]
|
61
62
|
GraphAPI.should_receive(:request).with('/me?&fields=FIELD1,FIELD2', 'ACCESS_TOKEN').and_return({})
|
62
63
|
GraphAPI.request_user('ACCESS_TOKEN').should == {'access_token' => 'ACCESS_TOKEN'}
|
63
64
|
end
|
@@ -65,8 +66,8 @@ describe GraphAPI do
|
|
65
66
|
|
66
67
|
describe '#fetch_user' do
|
67
68
|
it 'should return a user' do
|
68
|
-
|
69
|
-
|
69
|
+
GraphAPI.user_fields = [:FIELD1, :FIELD2]
|
70
|
+
GraphAPI.callback_url = 'CALLBACK_URL'
|
70
71
|
GraphAPI.stub(:fetch_token).and_return('ACCESS_TOKEN')
|
71
72
|
GraphAPI.should_receive(:request).with('/me?&fields=FIELD1,FIELD2', 'ACCESS_TOKEN').and_return({})
|
72
73
|
GraphAPI.fetch_user('CODE').should == {'access_token' => 'ACCESS_TOKEN'}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graph-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -93,7 +93,7 @@ files:
|
|
93
93
|
- readme.md
|
94
94
|
- spec/graph_api_spec.rb
|
95
95
|
- spec/spec_helper.rb
|
96
|
-
homepage: https://github.com/nickbarth/
|
96
|
+
homepage: https://github.com/nickbarth/GraphAPI
|
97
97
|
licenses: []
|
98
98
|
post_install_message:
|
99
99
|
rdoc_options: []
|