graph-api 0.9.7 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +10 -1
- data/graph_api.gemspec +3 -2
- data/lib/graph_api.rb +77 -65
- data/lib/graph_api/version.rb +2 -2
- data/readme.md +17 -7
- data/spec/graph_api_spec.rb +30 -28
- metadata +20 -4
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
graph-api (0.9.
|
4
|
+
graph-api (0.9.7)
|
5
5
|
json
|
6
6
|
rake
|
7
7
|
rest-client
|
@@ -9,6 +9,14 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: http://rubygems.org/
|
11
11
|
specs:
|
12
|
+
columnize (0.3.6)
|
13
|
+
debugger (1.2.0)
|
14
|
+
columnize (>= 0.3.1)
|
15
|
+
debugger-linecache (~> 1.1.1)
|
16
|
+
debugger-ruby_core_source (~> 1.1.3)
|
17
|
+
debugger-linecache (1.1.2)
|
18
|
+
debugger-ruby_core_source (>= 1.1.1)
|
19
|
+
debugger-ruby_core_source (1.1.3)
|
12
20
|
diff-lcs (1.1.3)
|
13
21
|
json (1.7.5)
|
14
22
|
mime-types (1.19)
|
@@ -28,5 +36,6 @@ PLATFORMS
|
|
28
36
|
ruby
|
29
37
|
|
30
38
|
DEPENDENCIES
|
39
|
+
debugger
|
31
40
|
graph-api!
|
32
41
|
rspec
|
data/graph_api.gemspec
CHANGED
@@ -8,14 +8,15 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = GraphAPI::VERSION
|
9
9
|
gem.authors = ['Nick Barth']
|
10
10
|
gem.email = ['nick@nickbarth.ca']
|
11
|
-
gem.summary = 'A Ruby Gem for
|
12
|
-
gem.description = 'GraphAPI is a Ruby Gem
|
11
|
+
gem.summary = 'A Ruby Gem for simplifying authentication using the Facebook Graph API.'
|
12
|
+
gem.description = 'GraphAPI is a Ruby Gem created to simplifiy and help manage authentication using the Facebook Graph API.'
|
13
13
|
gem.homepage = 'https://github.com/nickbarth/GraphAPI'
|
14
14
|
|
15
15
|
gem.add_dependency('rake')
|
16
16
|
gem.add_dependency('json')
|
17
17
|
gem.add_dependency('rest-client')
|
18
18
|
gem.add_development_dependency('rspec')
|
19
|
+
gem.add_development_dependency('debugger')
|
19
20
|
|
20
21
|
gem.files = `git ls-files`.split($/)
|
21
22
|
gem.test_files = gem.files.grep /spec/
|
data/lib/graph_api.rb
CHANGED
@@ -2,55 +2,58 @@ require 'json'
|
|
2
2
|
require 'rest_client'
|
3
3
|
require 'graph_api/version'
|
4
4
|
|
5
|
-
# Public:
|
5
|
+
# Public: GraphAPI is a Ruby Gem created to simplifiy and help manage
|
6
|
+
# authentication using the Facebook Graph API.
|
6
7
|
#
|
7
8
|
# Example:
|
8
9
|
#
|
9
10
|
# get '/facebook_login' do
|
10
|
-
# redirect
|
11
|
+
# redirect GraphAPI.auth_url
|
11
12
|
# end
|
12
13
|
#
|
13
|
-
# get '/
|
14
|
-
# @facebook_user = GraphAPI.
|
15
|
-
#
|
14
|
+
# get '/facebook_callback' do
|
15
|
+
# @facebook_user = GraphAPI.new(params[:code])
|
16
|
+
# session[:auth_token] = @facebook_user.auth_token
|
16
17
|
# render :signed_in
|
17
18
|
# end
|
18
19
|
#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
class GraphAPI
|
21
|
+
class << self
|
22
|
+
# Public: Required setting used for Facebook private application secret.
|
23
|
+
#
|
24
|
+
# Example:
|
25
|
+
#
|
26
|
+
# @app_secret = '124ca2a483f12723cafa7a5da33a3492'
|
27
|
+
attr_accessor :app_secret
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
# Public: Required setting used for Facebook application Id.
|
30
|
+
#
|
31
|
+
# Example
|
32
|
+
#
|
33
|
+
# @client_id = '234513432316919'
|
34
|
+
attr_accessor :client_id
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
# Public: Reqired setting used for Facebook call back URL when receiving the Facebook connect code param.
|
37
|
+
#
|
38
|
+
# Example
|
39
|
+
#
|
40
|
+
# @callback_url = nil
|
41
|
+
attr_accessor :callback_url
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
# Public: Required setting used for setting Facebook application requirements.
|
44
|
+
#
|
45
|
+
# Example
|
46
|
+
#
|
47
|
+
# @access_scope = [:offline_access, :email, :user_photos, :user_location, :user_about_me]
|
48
|
+
attr_accessor :access_scope
|
47
49
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
# Public: Required setting used for setting the fields pulled for.
|
51
|
+
#
|
52
|
+
# Example
|
53
|
+
#
|
54
|
+
# @user_fields = [:id, :picture, :name, :gender, :link, :email, :verified, :bio]
|
55
|
+
attr_accessor :user_fields
|
56
|
+
end
|
54
57
|
|
55
58
|
# Public: Method for configuring the setting settings for a nicer syntax.
|
56
59
|
#
|
@@ -59,7 +62,7 @@ module GraphAPI
|
|
59
62
|
# GraphAPI.config app_secret: '124ca2a483f12723cafa7a5da33a3492',
|
60
63
|
# client_id: '234513432316919'
|
61
64
|
#
|
62
|
-
def config(settings)
|
65
|
+
def self.config(settings)
|
63
66
|
settings.each do |setting, value|
|
64
67
|
self.send("#{setting}=", value)
|
65
68
|
end
|
@@ -70,7 +73,7 @@ module GraphAPI
|
|
70
73
|
# callback_url - With @callback_url set to nil setting this parameter will use
|
71
74
|
# the sent callback. This is useful when you're using dynamic
|
72
75
|
# URIs with subdomains.
|
73
|
-
def auth_url(callback_url=nil)
|
76
|
+
def self.auth_url(callback_url=nil)
|
74
77
|
"https://graph.facebook.com/oauth/authorize?client_id=#{@client_id}" +
|
75
78
|
"&redirect_uri=#{@callback_url or callback_url}" +
|
76
79
|
"&scope=#{@access_scope.join(',')}"
|
@@ -84,7 +87,7 @@ module GraphAPI
|
|
84
87
|
# callback_url - With @callback_url set to nil setting this parameter will use
|
85
88
|
# the sent callback. This is useful when you're using dynamic
|
86
89
|
# URIs with subdomains.
|
87
|
-
def fetch_token(code, callback_url=nil)
|
90
|
+
def self.fetch_token(code, callback_url=nil)
|
88
91
|
RestClient.post('https://graph.facebook.com/oauth/access_token', { client_id: @client_id,
|
89
92
|
redirect_uri: (@callback_url or callback_url),
|
90
93
|
client_secret: @app_secret,
|
@@ -98,43 +101,52 @@ module GraphAPI
|
|
98
101
|
# access_token - The access token required for making the request on the Facebook users behalf.
|
99
102
|
#
|
100
103
|
# Returns a parsed JSON array returned from the Facebook service with a format like ['example' => 'some_data'].
|
101
|
-
def request(url, access_token)
|
104
|
+
def self.request(url, access_token)
|
102
105
|
JSON.parse(RestClient.get "https://graph.facebook.com#{url}&access_token=#{access_token}")
|
103
106
|
end
|
104
107
|
|
105
|
-
# Public:
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
merge('access_token' => access_token)
|
110
|
-
end
|
108
|
+
# Public: Get and set the facebook access token.
|
109
|
+
attr_accessor :access_token
|
110
|
+
# Public: Get and set a users data based on the current access token.
|
111
|
+
attr_accessor :user_data
|
111
112
|
|
112
|
-
# Public:
|
113
|
-
#
|
114
|
-
#
|
115
|
-
#
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
113
|
+
# Public: Creates a new Graph API instance.
|
114
|
+
#
|
115
|
+
# Returns a object representing the current Facebook user with properties
|
116
|
+
# specified in the self.class.user_fields hash array.
|
117
|
+
def initialize(access_token, code=nil, callback_url=nil)
|
118
|
+
@access_token = if not code.nil?
|
119
|
+
self.class.fetch_token(code, callback_url)
|
120
|
+
else
|
121
|
+
access_token
|
122
|
+
end
|
121
123
|
end
|
122
124
|
|
123
125
|
# Public: Fetches and returns the cover photo src for a Facebook user.
|
124
|
-
|
125
|
-
|
126
|
-
def fetch_photo(access_token)
|
127
|
-
albums = request('/me/albums?fields=id,cover_photo,type', access_token)['data']
|
126
|
+
def photo
|
127
|
+
albums = self.class.request('/me/albums?fields=id,cover_photo,type', @access_token)['data']
|
128
128
|
photo_id = albums.find{|x| x['type'] == 'profile'}['cover_photo']
|
129
|
-
request("/#{photo_id}/?fields=source", access_token)['source']
|
129
|
+
self.class.request("/#{photo_id}/?fields=source", @access_token)['source']
|
130
130
|
end
|
131
131
|
|
132
132
|
# Public: Fetches and returns the current thumbnail src for a Facebook user.
|
133
|
-
|
134
|
-
|
135
|
-
def fetch_thumbnail(access_token)
|
136
|
-
request('/me?fields=picture', access_token)['picture']['data']['url']
|
133
|
+
def thumbnail
|
134
|
+
self.picture['data']['url']
|
137
135
|
end
|
138
136
|
|
139
|
-
|
137
|
+
# Public: Meta methods for each of the user fields declared.
|
138
|
+
#
|
139
|
+
# Example:
|
140
|
+
#
|
141
|
+
# facebook_user = GraphAPI.new(auth_token) # Creates a new user
|
142
|
+
# puts facebook_user.name # Returns the Facebook users full name.
|
143
|
+
def method_missing(method, *args, &block)
|
144
|
+
user_fields = self.class.user_fields << :picture
|
145
|
+
if user_fields.include? method.to_sym
|
146
|
+
@user_data ||= self.class.request("/me?fields=#{user_fields.join(',')}", @access_token)
|
147
|
+
@user_data[method.to_s]
|
148
|
+
else
|
149
|
+
super
|
150
|
+
end
|
151
|
+
end
|
140
152
|
end
|
data/lib/graph_api/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = '0.9.
|
1
|
+
class GraphAPI
|
2
|
+
VERSION = '0.9.8'
|
3
3
|
end
|
data/readme.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# GraphAPI
|
2
|
-
GraphAPI is a Ruby Gem
|
2
|
+
GraphAPI is a Ruby Gem created to simplifiy and help manage authentication using
|
3
|
+
the Facebook Graph API.
|
3
4
|
|
4
5
|
## Usage
|
5
6
|
|
@@ -11,29 +12,38 @@ Here is how to use it.
|
|
11
12
|
|
12
13
|
### Set up your Facebook Appications constants
|
13
14
|
|
14
|
-
You will have to configure the
|
15
|
+
You will have to configure the gem before using it. Here is an example setup.
|
15
16
|
|
16
17
|
GraphAPI.config app_secret: '124ca2a483f12723cafa7a5da33a3492', # The Facebook Application Secret
|
17
|
-
client_id: '234513432316919', # The Facebook
|
18
|
+
client_id: '234513432316919', # The Facebook Application Id
|
18
19
|
callback_url: 'http://example.com/facebook_callback/', # URI for receiving the Facebook code param
|
19
20
|
access_scope: [:offline_access, :email, :user_photos], # The Facebook application requirements
|
20
21
|
user_fields: [:id, :picture, :name, :gender, :email] # The user fields pulled for
|
21
22
|
|
22
|
-
Visit https://developers.facebook.com/apps to register your Facebook application
|
23
|
+
Visit https://developers.facebook.com/apps to register your Facebook application
|
24
|
+
or checkout https://developers.facebook.com/docs/reference/api/user/ for a list
|
25
|
+
of user fields and permissions.
|
23
26
|
|
24
27
|
### Add it to your Application
|
25
28
|
|
26
|
-
Once configured you will be able to use
|
29
|
+
Once configured you will be able to use GraphAPI to retrieve any Facebook
|
30
|
+
user fields in your application. Here is basic example using Sinatra.
|
27
31
|
|
28
32
|
get '/facebook_login' do
|
29
33
|
redirect GraphAPI.auth_url
|
30
34
|
end
|
31
35
|
|
32
36
|
get '/facebook_callback' do
|
33
|
-
@facebook_user = GraphAPI.
|
34
|
-
|
37
|
+
@facebook_user = GraphAPI.new(params[:code])
|
38
|
+
session[:auth_token] = @facebook_user.auth_token
|
35
39
|
render :signed_in
|
36
40
|
end
|
37
41
|
|
42
|
+
get '/profile'
|
43
|
+
@facebook_user = GraphAPI.new(session[:auth_token])
|
44
|
+
logger.info "User #{@facebook_user.name} viewed their profile."
|
45
|
+
render :profile
|
46
|
+
end
|
47
|
+
|
38
48
|
### License
|
39
49
|
WTFPL © 2012 Nick Barth
|
data/spec/graph_api_spec.rb
CHANGED
@@ -40,10 +40,10 @@ describe GraphAPI do
|
|
40
40
|
describe '#fetch_token' do
|
41
41
|
it 'should return the access token' do
|
42
42
|
GraphAPI.callback_url = 'CALLBACK_URL'
|
43
|
-
RestClient.should_receive(:
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
RestClient.should_receive(:post).with('https://graph.facebook.com/oauth/access_token', { client_id: 'CLIENT_ID',
|
44
|
+
redirect_uri: 'CALLBACK_URL',
|
45
|
+
client_secret: 'APP_SECRET',
|
46
|
+
code: 'CODE'
|
47
47
|
}).and_return('access_token=ACCESS_TOKEN&')
|
48
48
|
GraphAPI.fetch_token('CODE').should == 'ACCESS_TOKEN'
|
49
49
|
end
|
@@ -56,37 +56,39 @@ describe GraphAPI do
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
describe '#
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
59
|
+
describe '#new' do
|
60
|
+
context 'with an access_token param' do
|
61
|
+
it 'should set the auth_token' do
|
62
|
+
GraphAPI.new('ACCESS_TOKEN').access_token.should == 'ACCESS_TOKEN'
|
63
|
+
end
|
64
64
|
end
|
65
|
-
end
|
66
65
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
GraphAPI.should_receive(:request).with('/me?&fields=FIELD1,FIELD2', 'ACCESS_TOKEN').and_return({})
|
73
|
-
GraphAPI.fetch_user('CODE').should == {'access_token' => 'ACCESS_TOKEN'}
|
66
|
+
context 'with a code param' do
|
67
|
+
it 'should set the auth_token' do
|
68
|
+
GraphAPI.stub(:fetch_token).and_return('ACCESS_TOKEN')
|
69
|
+
GraphAPI.new(nil, 'CODE').access_token.should == 'ACCESS_TOKEN'
|
70
|
+
end
|
74
71
|
end
|
75
72
|
end
|
76
73
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
74
|
+
context 'Instance' do
|
75
|
+
describe '#photo' do
|
76
|
+
it 'should return a photo URI' do
|
77
|
+
albums_data = {'data' => [{'type' => 'profile', 'cover_photo' => 'PHOTO_ID'}]}
|
78
|
+
GraphAPI.should_receive(:request).with('/me/albums?fields=id,cover_photo,type', 'ACCESS_TOKEN').and_return(albums_data)
|
79
|
+
GraphAPI.should_receive(:request).with('/PHOTO_ID/?fields=source', 'ACCESS_TOKEN').and_return({'source' => 'PHOTO_URI'})
|
80
|
+
fg_user = GraphAPI.new('ACCESS_TOKEN')
|
81
|
+
fg_user.photo.should == 'PHOTO_URI'
|
82
|
+
end
|
83
83
|
end
|
84
|
-
end
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
85
|
+
describe '#thumbnail' do
|
86
|
+
it 'should return a photo URI' do
|
87
|
+
GraphAPI.user_fields = []
|
88
|
+
GraphAPI.should_receive(:request).with('/me?fields=picture', 'ACCESS_TOKEN').and_return({'picture' => {'data' => {'url' => 'PHOTO_URI'}}})
|
89
|
+
fg_user = GraphAPI.new('ACCESS_TOKEN')
|
90
|
+
fg_user.thumbnail.should == 'PHOTO_URI'
|
91
|
+
end
|
90
92
|
end
|
91
93
|
end
|
92
94
|
end
|
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.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -75,8 +75,24 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
-
|
79
|
-
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: debugger
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: GraphAPI is a Ruby Gem created to simplifiy and help manage authentication
|
95
|
+
using the Facebook Graph API.
|
80
96
|
email:
|
81
97
|
- nick@nickbarth.ca
|
82
98
|
executables: []
|
@@ -116,7 +132,7 @@ rubyforge_project:
|
|
116
132
|
rubygems_version: 1.8.24
|
117
133
|
signing_key:
|
118
134
|
specification_version: 3
|
119
|
-
summary: A Ruby Gem for
|
135
|
+
summary: A Ruby Gem for simplifying authentication using the Facebook Graph API.
|
120
136
|
test_files:
|
121
137
|
- graph_api.gemspec
|
122
138
|
- spec/graph_api_spec.rb
|