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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graph-api (0.9.5)
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
@@ -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 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.'
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/
@@ -2,55 +2,58 @@ require 'json'
2
2
  require 'rest_client'
3
3
  require 'graph_api/version'
4
4
 
5
- # Public: Various methods useful for interfacing with Facebook Graph protocol.
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 FaceGraph.auth_url
11
+ # redirect GraphAPI.auth_url
11
12
  # end
12
13
  #
13
- # get '/facebook_auth' do
14
- # @facebook_user = GraphAPI.fetch_user(params[:code])
15
- # @photo = GraphAPI.fetch_photo(@facebook_user['access_token'])
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
- module GraphAPI
20
- # Public: Required setting used for Facebook private application secret.
21
- #
22
- # Example:
23
- #
24
- # @app_secret = '124ca2a483f12723cafa7a5da33a3492'
25
- attr_accessor :app_secret
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
- # Public: Required setting used for Facebook private application client ID.
28
- #
29
- # Example
30
- #
31
- # @client_id = '234513432316919'
32
- attr_accessor :client_id
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
- # Public: Reqired setting used for Facebook call back URL when receiving the Facebook connect code param.
35
- #
36
- # Example
37
- #
38
- # @callback_url = nil
39
- attr_accessor :callback_url
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
- # Public: Required setting used for setting Facebook application requirements.
42
- #
43
- # Example
44
- #
45
- # @access_scope = [:offline_access, :email, :user_photos, :user_location, :user_about_me]
46
- attr_accessor :access_scope
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
- # Public: Required setting used for setting the fields pulled for.
49
- #
50
- # Example
51
- #
52
- # @user_fields = [:id, :picture, :name, :gender, :link, :email, :verified, :bio]
53
- attr_accessor :user_fields
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: Returns a Facebook user array containing the fields set by the
106
- # @user_fields setting and the access token for convenience.
107
- def request_user(access_token)
108
- request("/me?&fields=#{@user_fields.join(',')}", access_token).
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: Convenience method for fetching a Facebook user array from the
113
- # Facebook token code.
114
- #
115
- # callback_url - With @callback_url set to nil setting this parameter will use
116
- # the sent callback. This is useful when you're using dynamic
117
- # URIs with subdomains.
118
- def fetch_user(code, callback_url=nil)
119
- access_token = fetch_token(code, callback_url)
120
- request_user(access_token)
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
- # access_token - This method requires an Facebook Authentication token.
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
- # access_token - This method requires an Facebook Authentication token.
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
- extend self
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
@@ -1,3 +1,3 @@
1
- module GraphAPI
2
- VERSION = '0.9.7'
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 containing some common tasks to help manage Facebook users using the Facebook Graph API.
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 module before using it. Here is an example setup.
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 Client ID
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 any of its functions in your application. Here is basic example using Sinatra.
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.fetch_user(params[:code])
34
- @photo = GraphAPI.fetch_photo(@facebook_user['access_token'])
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 &copy; 2012 Nick Barth
@@ -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(:get).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'
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 '#request_user' do
60
- it 'should return a user' do
61
- GraphAPI.user_fields = [:FIELD1, :FIELD2]
62
- GraphAPI.should_receive(:request).with('/me?&fields=FIELD1,FIELD2', 'ACCESS_TOKEN').and_return({})
63
- GraphAPI.request_user('ACCESS_TOKEN').should == {'access_token' => 'ACCESS_TOKEN'}
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
- describe '#fetch_user' do
68
- it 'should return a user' do
69
- GraphAPI.user_fields = [:FIELD1, :FIELD2]
70
- GraphAPI.callback_url = 'CALLBACK_URL'
71
- GraphAPI.stub(:fetch_token).and_return('ACCESS_TOKEN')
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
- describe '#fetch_photo' do
78
- it 'should return a photo URI' do
79
- albums_data = {'data' => [{'type' => 'profile', 'cover_photo' => 'PHOTO_ID'}]}
80
- GraphAPI.should_receive(:request).with('/me/albums?fields=id,cover_photo,type', 'ACCESS_TOKEN').and_return(albums_data)
81
- GraphAPI.should_receive(:request).with('/PHOTO_ID/?fields=source', 'ACCESS_TOKEN').and_return({'source' => 'PHOTO_URI'})
82
- GraphAPI.fetch_photo('ACCESS_TOKEN').should == 'PHOTO_URI'
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
- describe '#fetch_thumbnail' do
87
- it 'should return a photo URI' do
88
- GraphAPI.should_receive(:request).with('/me?fields=picture', 'ACCESS_TOKEN').and_return({'picture' => 'PHOTO_URI'})
89
- GraphAPI.fetch_thumbnail('ACCESS_TOKEN').should == 'PHOTO_URI'
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.7
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
- description: GraphAPI is a Ruby Gem containing some common tasks to help manage Facebook
79
- users using the Facebook Graph API.
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 common Facebook Graph API tasks.
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