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.
@@ -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/RakeAR'
13
+ gem.homepage = 'https://github.com/nickbarth/GraphAPI'
14
14
 
15
15
  gem.add_dependency('rake')
16
16
  gem.add_dependency('json')
@@ -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::auth_url
10
+ # redirect FaceGraph.auth_url
10
11
  # end
11
12
  #
12
13
  # get '/facebook_auth' do
13
- # @facebook_user = GraphAPI::fetch_user(params[:code])
14
- # @photo = GraphAPI::fetch_photo(@facebook_user['access_token'])
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 constant used for Facebook private application secret.
20
+ # Public: Required setting used for Facebook private application secret.
20
21
  #
21
22
  # Example:
22
23
  #
23
- # APP_SECRET = '124ca2a483f12723cafa7a5da33a3492'
24
+ # @app_secret = '124ca2a483f12723cafa7a5da33a3492'
25
+ attr_accessor :app_secret
24
26
 
25
- # Public: Required constant used for Facebook private application client ID.
27
+ # Public: Required setting used for Facebook private application client ID.
26
28
  #
27
29
  # Example
28
30
  #
29
- # CLIENT_ID = '234513432316919'
31
+ # @client_id = '234513432316919'
32
+ attr_accessor :client_id
30
33
 
31
- # Public: Reqired constant used for Facebook call back URL when receiving the Facebook connect code param.
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
- # CALLBACK_URL = nil
38
+ # @callback_url = nil
39
+ attr_accessor :callback_url
36
40
 
37
- # Public: Required constant used for setting Facebook application requirements.
41
+ # Public: Required setting used for setting Facebook application requirements.
38
42
  #
39
43
  # Example
40
44
  #
41
- # ACCESS_SCOPE = [:offline_access, :email, :user_photos, :user_location, :user_about_me]
45
+ # @access_scope = [:offline_access, :email, :user_photos, :user_location, :user_about_me]
46
+ attr_accessor :access_scope
42
47
 
43
- # Public: Required constant used for setting the fields pulled for.
48
+ # Public: Required setting used for setting the fields pulled for.
44
49
  #
45
50
  # Example
46
51
  #
47
- # USER_FIELDS = [:id, :picture, :name, :gender, :link, :email, :verified, :bio]
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 constants for a nicer syntax.
55
+ # Public: Method for configuring the setting settings for a nicer syntax.
50
56
  #
51
57
  # Example:
52
58
  #
53
- # GraphAPI.config APP_SECRET: '124ca2a483f12723cafa7a5da33a3492',
54
- # CLIENT_ID: '234513432316919'
59
+ # GraphAPI.config app_secret: '124ca2a483f12723cafa7a5da33a3492',
60
+ # client_id: '234513432316919'
55
61
  #
56
- def config(consts)
57
- consts.each do |const, value|
58
- const_set(const.to_s, value)
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 constants.
68
+ # Public: Creates and returns a Facebook Authentication URL based on the supplied settings.
63
69
  #
64
- # callback_url - With CALLBACK_URL set to nil setting this parameter will use
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=#{CLIENT_ID}" +
69
- "&redirect_uri=#{CALLBACK_URL or callback_url}" +
70
- "&scope=#{ACCESS_SCOPE.join(',')}"
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 CALLBACK_URL set to nil setting this parameter will use
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: CLIENT_ID,
83
- redirect_uri: (CALLBACK_URL or callback_url),
84
- client_secret: APP_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
- # USER_FIELDS constant and the access token for convenience.
106
+ # @user_fields setting and the access token for convenience.
101
107
  def request_user(access_token)
102
- request("/me?&fields=#{USER_FIELDS.join(',')}", access_token).
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 CALLBACK_URL set to nil setting this parameter will use
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)
@@ -1,3 +1,3 @@
1
1
  module GraphAPI
2
- VERSION = '0.9.5'
2
+ VERSION = '0.9.6'
3
3
  end
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 APP_SECRET: '124ca2a483f12723cafa7a5da33a3492' # Constant required for setting the Facebook Application Secret
17
- CLIENT_ID: '234513432316919' # Constant required for setting the Facebook Client ID
18
- CALLBACK_URL: 'http://example.com/facebook_callback/' # Constant required for when receiving the Facebook connect code param
19
- ACCESS_SCOPE: [:offline_access, :email, :user_photos, :user_location] # Constant required for setting Facebook application requirements
20
- USER_FIELDS: [:id, :picture, :name, :gender, :link, :email] # Constant required for setting the user fields pulled for
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::auth_url
27
+ redirect FaceGraph.auth_url
28
28
  end
29
29
 
30
30
  get '/facebook_auth' do
31
- @facebook_user = GraphAPI::fetch_user(params[:code])
32
- @photo = GraphAPI::fetch_photo(@facebook_user['access_token'])
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
 
@@ -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 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
- GraphAPI::APP_SECRET.should == 'APP_SECRET'
16
- GraphAPI::CLIENT_ID.should == 'CLIENT_ID'
17
- GraphAPI::CALLBACK_URL.should == 'CALLBACK_URL'
18
- GraphAPI::ACCESS_SCOPE.should == 'ACCESS_SCOPE'
19
- GraphAPI::USER_FIELDS.should == 'USER_FIELDS'
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
- stub_const('GraphAPI::ACCESS_SCOPE', [:SCOPE1, :SCOPE2])
26
+ GraphAPI.access_scope = [:SCOPE1, :SCOPE2]
26
27
  end
27
28
 
28
29
  it 'should use generate a URI' do
29
- stub_const('GraphAPI::CALLBACK_URL', nil)
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
- stub_const('GraphAPI::CALLBACK_URL', 'CALLBACK_URL')
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
- stub_const('GraphAPI::CALLBACK_URL', 'CALLBACK_URL')
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
- stub_const('GraphAPI::USER_FIELDS', [:FIELD1, :FIELD2])
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
- stub_const('GraphAPI::USER_FIELDS', [:FIELD1, :FIELD2])
69
- stub_const('GraphAPI::CALLBACK_URL', 'CALLBACK_URL')
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.5
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/RakeAR
96
+ homepage: https://github.com/nickbarth/GraphAPI
97
97
  licenses: []
98
98
  post_install_message:
99
99
  rdoc_options: []