graph-api 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/Rakefile +4 -0
- data/graph_api.gemspec +21 -0
- data/lib/graph_api.rb +117 -0
- data/lib/graph_api/version.rb +3 -0
- data/readme.md +44 -0
- data/spec/graph_api_spec.rb +78 -0
- data/spec/spec_helper.rb +7 -0
- metadata +90 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
graph-api (0.9.1)
|
5
|
+
rake
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.1.3)
|
11
|
+
rake (0.9.2.2)
|
12
|
+
rspec (2.11.0)
|
13
|
+
rspec-core (~> 2.11.0)
|
14
|
+
rspec-expectations (~> 2.11.0)
|
15
|
+
rspec-mocks (~> 2.11.0)
|
16
|
+
rspec-core (2.11.1)
|
17
|
+
rspec-expectations (2.11.3)
|
18
|
+
diff-lcs (~> 1.1.3)
|
19
|
+
rspec-mocks (2.11.3)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
graph-api!
|
26
|
+
rspec
|
data/Rakefile
ADDED
data/graph_api.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'graph_api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'graph-api'
|
7
|
+
gem.date = '2012-10-03'
|
8
|
+
gem.version = GraphAPI::VERSION
|
9
|
+
gem.authors = ['Nick Barth']
|
10
|
+
gem.email = ['nick@nickbarth.ca']
|
11
|
+
gem.summary = 'A Ruby Gem for common Facebook Graph API tasks.'
|
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/RakeAR'
|
14
|
+
|
15
|
+
gem.add_dependency('rake')
|
16
|
+
gem.add_development_dependency('rspec')
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.test_files = gem.files.grep /spec/
|
20
|
+
gem.require_paths = ['lib']
|
21
|
+
end
|
data/lib/graph_api.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
require 'json'
|
2
|
+
# Public: Various methods useful for interfacing with Facebook Graph protocol.
|
3
|
+
#
|
4
|
+
# Example:
|
5
|
+
#
|
6
|
+
# get '/facebook_login' do
|
7
|
+
# redirect FaceGraph::auth_url
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# get '/facebook_auth' do
|
11
|
+
# @facebook_user = GraphAPI::fetch_user(params[:code])
|
12
|
+
# @photo = GraphAPI::fetch_photo(@facebook_user['access_token'])
|
13
|
+
# render :signed_in
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
module GraphAPI
|
17
|
+
# Public: Required constant used for Facebook private application secret.
|
18
|
+
#
|
19
|
+
# Example:
|
20
|
+
#
|
21
|
+
# APP_SECRET = '124ca2a483f12723cafa7a5da33a3492'
|
22
|
+
|
23
|
+
# Public: Required constant used for Facebook private application client ID.
|
24
|
+
#
|
25
|
+
# Example
|
26
|
+
#
|
27
|
+
# CLIENT_ID = '234513432316919'
|
28
|
+
|
29
|
+
# Public: Reqired constant used for Facebook call back URL when receiving the Facebook connect code param.
|
30
|
+
#
|
31
|
+
# Example
|
32
|
+
#
|
33
|
+
# CALLBACK_URL = nil
|
34
|
+
|
35
|
+
# Public: Required constant used for setting Facebook application requirements.
|
36
|
+
#
|
37
|
+
# Example
|
38
|
+
#
|
39
|
+
# ACCESS_SCOPE = [:offline_access, :email, :user_photos, :user_location, :user_about_me]
|
40
|
+
|
41
|
+
# Public: Required constant used for setting the fields pulled for.
|
42
|
+
#
|
43
|
+
# Example
|
44
|
+
#
|
45
|
+
# USER_FIELDS = [:id, :picture, :name, :gender, :link, :email, :verified, :bio]
|
46
|
+
|
47
|
+
# Public: Creates and returns a Facebook Authentication URL based on the supplied constants.
|
48
|
+
#
|
49
|
+
# callback_url - With CALLBACK_URL set to nil setting this parameter will use
|
50
|
+
# the sent callback. This is useful when you're using dynamic
|
51
|
+
# URIs with subdomains.
|
52
|
+
def self.auth_url(callback_url=nil)
|
53
|
+
"https://graph.facebook.com/oauth/authorize?client_id=#{CLIENT_ID}" +
|
54
|
+
"&redirect_uri=#{CALLBACK_URL or callback_url}" +
|
55
|
+
"&scope=#{ACCESS_SCOPE.join(',')}"
|
56
|
+
end
|
57
|
+
|
58
|
+
# Public: Requests the Access Token from the Facebook Graph API and returns it as a string.
|
59
|
+
#
|
60
|
+
# code - The code parameter is the param you receive when the Facebook Graph
|
61
|
+
# API hits your call back URI.
|
62
|
+
#
|
63
|
+
# callback_url - With CALLBACK_URL set to nil setting this parameter will use
|
64
|
+
# the sent callback. This is useful when you're using dynamic
|
65
|
+
# URIs with subdomains.
|
66
|
+
def self.fetch_token(code, callback_url=nil)
|
67
|
+
RestClient.get('https://graph.facebook.com/oauth/access_token', { client_id: CLIENT_ID,
|
68
|
+
redirect_uri: (CALLBACK_URL or callback_url),
|
69
|
+
client_secret: APP_SECRET,
|
70
|
+
code: code
|
71
|
+
})[/access_token=(.+?)&/, 1]
|
72
|
+
end
|
73
|
+
|
74
|
+
# Creates a request to the Facebook graph API and returns the response.
|
75
|
+
#
|
76
|
+
# url - The URL of the request begining with a forward slash.
|
77
|
+
# access_token - The access token required for making the request on the Facebook users behalf.
|
78
|
+
#
|
79
|
+
# Returns a parsed JSON array returned from the Facebook service with a format like ['example' => 'some_data'].
|
80
|
+
def self.request(url, access_token)
|
81
|
+
JSON.parse(RestClient.get "https://graph.facebook.com#{url}&access_token=#{access_token}")
|
82
|
+
end
|
83
|
+
|
84
|
+
# Public: Returns a Facebook user array containing the fields set by the
|
85
|
+
# USER_FIELDS constant and the access token for convenience.
|
86
|
+
def self.request_user(access_token)
|
87
|
+
request("/me?&fields=#{USER_FIELDS.join(',')}", access_token).
|
88
|
+
merge('access_token' => access_token)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Public: Convenience method for fetching a Facebook user array from the
|
92
|
+
# Facebook token code.
|
93
|
+
#
|
94
|
+
# callback_url - With CALLBACK_URL set to nil setting this parameter will use
|
95
|
+
# the sent callback. This is useful when you're using dynamic
|
96
|
+
# URIs with subdomains.
|
97
|
+
def self.fetch_user(code, callback_url=nil)
|
98
|
+
access_token = fetch_token(code, callback_url)
|
99
|
+
request_user(access_token)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Public: Fetches and returns the cover photo src for a Facebook user.
|
103
|
+
#
|
104
|
+
# access_token - This method requires an Facebook Authentication token.
|
105
|
+
def self.fetch_photo(access_token)
|
106
|
+
albums = request('/me/albums?fields=id,cover_photo,type', access_token)['data']
|
107
|
+
photo_id = albums.find{|x| x['type'] == 'profile'}['cover_photo']
|
108
|
+
request("/#{photo_id}/?fields=source", access_token)['source']
|
109
|
+
end
|
110
|
+
|
111
|
+
# Public: Fetches and returns the current thumbnail src for a Facebook user.
|
112
|
+
#
|
113
|
+
# access_token - This method requires an Facebook Authentication token.
|
114
|
+
def self.fetch_thumbnail(access_token)
|
115
|
+
request('/me?fields=picture', access_token)['picture']
|
116
|
+
end
|
117
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# GraphAPI
|
2
|
+
GraphAPI is a Ruby Gem containing some common tasks to help manage Facebook users using the Facebook Graph API.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
Here is how to use it.
|
7
|
+
|
8
|
+
### Add it to your Gemfile
|
9
|
+
|
10
|
+
gem 'sprock-assets', require 'sprock_assets'
|
11
|
+
|
12
|
+
### Set up your Facebook Appications constants
|
13
|
+
|
14
|
+
You will have to configure the module before using it. Here is an example setup.
|
15
|
+
|
16
|
+
module GraphAPI
|
17
|
+
# Public: Required constant used for Facebook private application secret.
|
18
|
+
APP_SECRET = '124ca2a483f12723cafa7a5da33a3492'
|
19
|
+
# Public: Required constant used for Facebook private application client ID.
|
20
|
+
CLIENT_ID = '234513432316919'
|
21
|
+
# Public: Reqired constant used for Facebook call back URL when receiving the Facebook connect code param.
|
22
|
+
CALLBACK_URL = nil
|
23
|
+
# Public: Required constant used for setting Facebook application requirements.
|
24
|
+
ACCESS_SCOPE = [:offline_access, :email, :user_photos, :user_location, :user_about_me]
|
25
|
+
# Public: Required constant used for setting the fields pulled for.
|
26
|
+
USER_FIELDS = [:id, :picture, :name, :gender, :link, :email, :verified, :bio]
|
27
|
+
end
|
28
|
+
|
29
|
+
### Add it to your Application
|
30
|
+
|
31
|
+
Once configured you will be able to use any of its functions in your application. Here is basic example using Sinatra.
|
32
|
+
|
33
|
+
get '/facebook_login' do
|
34
|
+
redirect FaceGraph::auth_url
|
35
|
+
end
|
36
|
+
|
37
|
+
get '/facebook_auth' do
|
38
|
+
@facebook_user = GraphAPI::fetch_user(params[:code])
|
39
|
+
@photo = GraphAPI::fetch_photo(@facebook_user['access_token'])
|
40
|
+
render :signed_in
|
41
|
+
end
|
42
|
+
|
43
|
+
### License
|
44
|
+
WTFPL © 2012 Nick Barth
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GraphAPI do
|
4
|
+
before(:each) do
|
5
|
+
stub_const('RestClient', Class.new)
|
6
|
+
stub_const('GraphAPI::CLIENT_ID', 'CLIENT_ID')
|
7
|
+
stub_const('GraphAPI::APP_SECRET', 'APP_SECRET')
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#auth_url' do
|
11
|
+
before(:each) do
|
12
|
+
stub_const('GraphAPI::ACCESS_SCOPE', [:SCOPE1, :SCOPE2])
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should use generate a URI' do
|
16
|
+
stub_const('GraphAPI::CALLBACK_URL', nil)
|
17
|
+
GraphAPI.auth_url('CALLBACK').should == 'https://graph.facebook.com/oauth/authorize?client_id=CLIENT_ID&redirect_uri=CALLBACK&scope=SCOPE1,SCOPE2'
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should use CLIENT_ID const if avaliable' do
|
21
|
+
stub_const('GraphAPI::CALLBACK_URL', 'CALLBACK_URL')
|
22
|
+
GraphAPI.auth_url.should == 'https://graph.facebook.com/oauth/authorize?client_id=CLIENT_ID&redirect_uri=CALLBACK_URL&scope=SCOPE1,SCOPE2'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#fetch_token' do
|
27
|
+
it 'should return the access token' do
|
28
|
+
stub_const('GraphAPI::CALLBACK_URL', 'CALLBACK_URL')
|
29
|
+
RestClient.should_receive(:get).with('https://graph.facebook.com/oauth/access_token', { client_id: 'CLIENT_ID',
|
30
|
+
redirect_uri: 'CALLBACK_URL',
|
31
|
+
client_secret: 'APP_SECRET',
|
32
|
+
code: 'CODE'
|
33
|
+
}).and_return('access_token=ACCESS_TOKEN&')
|
34
|
+
GraphAPI.fetch_token('CODE').should == 'ACCESS_TOKEN'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#fetch_token' do
|
39
|
+
it 'should return a Ruby Array' do
|
40
|
+
RestClient.should_receive(:get).with('https://graph.facebook.com/URL/?&access_token=ACCESS_TOKEN').and_return('[]')
|
41
|
+
GraphAPI.request('/URL/?', 'ACCESS_TOKEN').should == []
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#request_user' do
|
46
|
+
it 'should return a user' do
|
47
|
+
stub_const('GraphAPI::USER_FIELDS', [:FIELD1, :FIELD2])
|
48
|
+
GraphAPI.should_receive(:request).with('/me?&fields=FIELD1,FIELD2', 'ACCESS_TOKEN').and_return({})
|
49
|
+
GraphAPI.request_user('ACCESS_TOKEN').should == {'access_token' => 'ACCESS_TOKEN'}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#fetch_user' do
|
54
|
+
it 'should return a user' do
|
55
|
+
stub_const('GraphAPI::USER_FIELDS', [:FIELD1, :FIELD2])
|
56
|
+
stub_const('GraphAPI::CALLBACK_URL', 'CALLBACK_URL')
|
57
|
+
GraphAPI.stub(:fetch_token).and_return('ACCESS_TOKEN')
|
58
|
+
GraphAPI.should_receive(:request).with('/me?&fields=FIELD1,FIELD2', 'ACCESS_TOKEN').and_return({})
|
59
|
+
GraphAPI.fetch_user('CODE').should == {'access_token' => 'ACCESS_TOKEN'}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#fetch_photo' do
|
64
|
+
it 'should return a photo URI' do
|
65
|
+
albums_data = {'data' => [{'type' => 'profile', 'cover_photo' => 'PHOTO_ID'}]}
|
66
|
+
GraphAPI.should_receive(:request).with('/me/albums?fields=id,cover_photo,type', 'ACCESS_TOKEN').and_return(albums_data)
|
67
|
+
GraphAPI.should_receive(:request).with('/PHOTO_ID/?fields=source', 'ACCESS_TOKEN').and_return({'source' => 'PHOTO_URI'})
|
68
|
+
GraphAPI.fetch_photo('ACCESS_TOKEN').should == 'PHOTO_URI'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe '#fetch_thumbnail' do
|
73
|
+
it 'should return a photo URI' do
|
74
|
+
GraphAPI.should_receive(:request).with('/me?fields=picture', 'ACCESS_TOKEN').and_return({'picture' => 'PHOTO_URI'})
|
75
|
+
GraphAPI.fetch_thumbnail('ACCESS_TOKEN').should == 'PHOTO_URI'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graph-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Nick Barth
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: GraphAPI is a Ruby Gem containing some common tasks to help manage Facebook
|
47
|
+
users using the Facebook Graph API.
|
48
|
+
email:
|
49
|
+
- nick@nickbarth.ca
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- Rakefile
|
57
|
+
- graph_api.gemspec
|
58
|
+
- lib/graph_api.rb
|
59
|
+
- lib/graph_api/version.rb
|
60
|
+
- readme.md
|
61
|
+
- spec/graph_api_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: https://github.com/nickbarth/RakeAR
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: A Ruby Gem for common Facebook Graph API tasks.
|
87
|
+
test_files:
|
88
|
+
- graph_api.gemspec
|
89
|
+
- spec/graph_api_spec.rb
|
90
|
+
- spec/spec_helper.rb
|