fb_graph 2.1.7 → 2.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/VERSION +1 -1
  2. data/lib/fb_graph.rb +8 -2
  3. data/lib/fb_graph/achievement.rb +44 -0
  4. data/lib/fb_graph/ad_account.rb +2 -0
  5. data/lib/fb_graph/ad_connection_object.rb +38 -0
  6. data/lib/fb_graph/application.rb +27 -6
  7. data/lib/fb_graph/broad_targeting_category.rb +13 -0
  8. data/lib/fb_graph/connections/achievements.rb +21 -0
  9. data/lib/fb_graph/connections/ad_connection_objects.rb +14 -0
  10. data/lib/fb_graph/connections/broad_targeting_categories.rb +14 -0
  11. data/lib/fb_graph/connections/mutual_friends.rb +22 -0
  12. data/lib/fb_graph/connections/user_achievements.rb +22 -0
  13. data/lib/fb_graph/exception.rb +41 -1
  14. data/lib/fb_graph/node.rb +2 -11
  15. data/lib/fb_graph/user.rb +2 -0
  16. data/lib/fb_graph/user_achievement.rb +35 -0
  17. data/spec/fb_graph/achievement_spec.rb +59 -0
  18. data/spec/fb_graph/ad_connection_object_spec.rb +30 -0
  19. data/spec/fb_graph/application_spec.rb +3 -1
  20. data/spec/fb_graph/broad_targeting_category_spec.rb +17 -0
  21. data/spec/fb_graph/connections/achievements_spec.rb +27 -0
  22. data/spec/fb_graph/connections/ad_connection_objects_spec.rb +20 -0
  23. data/spec/fb_graph/connections/broad_targeting_categories_spec.rb +18 -0
  24. data/spec/fb_graph/connections/mutual_friends_spec.rb +28 -0
  25. data/spec/fb_graph/connections/user_achievements_spec.rb +37 -0
  26. data/spec/fb_graph/exception_spec.rb +97 -1
  27. data/spec/fb_graph/user_achievement_spec.rb +53 -0
  28. data/spec/mock_json/ad_accounts/ad_connection_objects/test_connection_objects.json +34 -0
  29. data/spec/mock_json/ad_accounts/broad_targeting_categories/test_bct.json +19 -0
  30. data/spec/mock_json/applications/achievements/sample.json +24 -0
  31. data/spec/mock_json/users/mutual_friends/me_and_agektmr.json +126 -0
  32. data/spec/mock_json/users/user_achievements/created.json +1 -0
  33. data/spec/mock_json/users/user_achievements/sample.json +31 -0
  34. metadata +41 -2
@@ -0,0 +1,30 @@
1
+ describe FbGraph::AdConnectionObject, '.new' do
2
+ it 'should setup all supported attributes' do
3
+ attributes = {
4
+ :id => 354545238888,
5
+ :name => "MyPage",
6
+ :url => "http://www.facebook.com/MyPage",
7
+ :type => 1,
8
+ :tabs =>
9
+ {
10
+ "http://www.facebook.com/MyPage?sk=wall" => "Wall",
11
+ "http://www.facebook.com/MyPage?sk=info" => "Info",
12
+ "http://www.facebook.com/MyPage?sk=friendactivity" => "Friend Activity",
13
+ "http://www.facebook.com/MyPage?sk=photos" => "Photos",
14
+ "http://www.facebook.com/MyPage?sk=app_2373072222" => "Discussions"
15
+ },
16
+ :picture => "http://profile.ak.fbcdn.net/hprofile-ak-snc4/41591_354545238178_3195000_s.jpg"
17
+ }
18
+ ad_connection = FbGraph::AdConnectionObject.new(attributes.delete(:id), attributes)
19
+ ad_connection.identifier.should == 354545238888
20
+ ad_connection.name.should == "MyPage"
21
+ ad_connection.url.should == "http://www.facebook.com/MyPage"
22
+ ad_connection.type.should == 1
23
+ ad_connection.should be_page
24
+ ad_connection.object.should be_instance_of(FbGraph::Page)
25
+ ad_connection.object.identifier.should == 354545238888
26
+ ad_connection.picture.should == "http://profile.ak.fbcdn.net/hprofile-ak-snc4/41591_354545238178_3195000_s.jpg"
27
+ ad_connection.tabs.should be_instance_of(Hash)
28
+ ad_connection.tabs["http://www.facebook.com/MyPage?sk=wall"].should == "Wall"
29
+ end
30
+ end
@@ -11,7 +11,8 @@ describe FbGraph::Application do
11
11
  :description => 'Owsome Facebook Graph Wrapper',
12
12
  :category => 'Programming',
13
13
  :link => 'http://github.com/nov/fb_graph',
14
- :secret => 'sec sec'
14
+ :secret => 'sec sec',
15
+ :daily_active_users => '10'
15
16
  }
16
17
  app = FbGraph::Application.new(attributes.delete(:id), attributes)
17
18
  app.identifier.should == '12345'
@@ -20,6 +21,7 @@ describe FbGraph::Application do
20
21
  app.category.should == 'Programming'
21
22
  app.link.should == 'http://github.com/nov/fb_graph'
22
23
  app.secret.should == 'sec sec'
24
+ app.daily_active_users.should == 10
23
25
  end
24
26
  end
25
27
 
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe FbGraph::BroadTargetingCategory, '.new' do
4
+ it 'should setup all supported attributes' do
5
+ attributes = {
6
+ :id => 6002714401172,
7
+ :name => "Baby Boomers",
8
+ :parent_category => "Family Status"
9
+ }
10
+
11
+ btc = FbGraph::BroadTargetingCategory.new(attributes.delete(:id), attributes)
12
+ btc.identifier.should == 6002714401172
13
+ btc.name.should == "Baby Boomers"
14
+ btc.parent_category.should == "Family Status"
15
+ end
16
+ end
17
+
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe FbGraph::Connections::Achievements do
4
+ let(:app) { FbGraph::Application.new('app_id', :access_token => 'app_token') }
5
+
6
+ describe '#achievements' do
7
+ it 'should return an Array of FbGraph::Achievement' do
8
+ mock_graph :get, 'app_id/achievements', 'applications/achievements/sample', :access_token => 'app_token' do
9
+ app.achievements.each do |achievement|
10
+ achievement.should be_instance_of FbGraph::Achievement
11
+ achievement.access_token.should == 'app_token'
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#achievement!' do
18
+ let(:achievement_url) { 'http://fbgraphsample.heroku.com/achievements/1' }
19
+ it 'should return true' do
20
+ mock_graph :post, 'app_id/achievements', 'true', :access_token => 'app_token', :params => {
21
+ :achievement => achievement_url
22
+ } do
23
+ app.achievement!(achievement_url).should be_true
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ describe FbGraph::Connections::AdConnectionObjects, '#ad_connection_objects' do
2
+ context 'when included by FbGraph::AdAccount' do
3
+ context 'when access_token is given' do
4
+ it 'should return ad_connection_objects as FbGraph::AdConnectionObject' do
5
+ mock_graph :get, 'act_11223344/connectionobjects', 'ad_accounts/ad_connection_objects/test_connection_objects', :access_token => 'access_token' do
6
+ ad_connections = FbGraph::AdAccount.new("act_11223344", :access_token => 'access_token').connection_objects
7
+ ad_connections.size.should == 3
8
+
9
+ ad_connections.first.identifier.should == 354545238888
10
+ ad_connections.first.name.should == "MyPage"
11
+ ad_connections.first.url.should == "http://www.facebook.com/MyPage"
12
+ ad_connections.first.type.should == 1
13
+ ad_connections.first.picture.should == "http://profile.ak.fbcdn.net/hprofile-ak-snc4/41591_354545238178_3195000_s.jpg"
14
+ ad_connections.first.tabs.keys.size.should == 5
15
+ ad_connections.first.tabs["http://www.facebook.com/MyPage?sk=wall"].should == "Wall"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe FbGraph::Connections::BroadTargetingCategories, '#broad_targeting_categories' do
4
+ context 'when included by FbGraph::AdAccount' do
5
+ context 'when access_token is given' do
6
+ it 'should return broad_targeting_categories as FbGraph::BroadTargetingCategory' do
7
+ mock_graph :get, 'act_22334455/broadtargetingcategories', 'ad_accounts/broad_targeting_categories/test_bct', :access_token => 'access_token' do
8
+ bcts = FbGraph::AdAccount.new("act_22334455").broad_targeting_categories(:access_token => 'access_token')
9
+ bcts.size.should == 3
10
+ bcts.each {|x| x.should be_instance_of(FbGraph::BroadTargetingCategory)}
11
+ bcts[0].identifier.should == 6002714885172
12
+ bcts[0].name.should == "Cooking"
13
+ bcts[0].parent_category.should == "Activities"
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe FbGraph::Connections::MutualFriends do
4
+ let(:me) { FbGraph::User.me('access_token') }
5
+
6
+ shared_examples_for :fetch_mutual_friends_between_me_and_friend do
7
+ it 'should return an Array of FbGraph::User' do
8
+ mock_graph :get, 'me/mutualfriends/agektmr', 'users/mutual_friends/me_and_agektmr', :access_token => 'access_token' do
9
+ friends = me.mutual_friends(friend)
10
+ friends.each do |friend|
11
+ friend.should be_instance_of FbGraph::User
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ describe '#mutual_friends' do
18
+ context 'when friend is a FbGraph::User' do
19
+ let(:friend) { FbGraph::User.new('agektmr') }
20
+ it_behaves_like :fetch_mutual_friends_between_me_and_friend
21
+ end
22
+
23
+ context 'when friend is just an identifier' do
24
+ let(:friend) { 'agektmr' }
25
+ it_behaves_like :fetch_mutual_friends_between_me_and_friend
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe FbGraph::Connections::UserAchievements do
4
+ let(:user) { FbGraph::User.new('matake', :access_token => 'access_token') }
5
+
6
+ describe '#achievements' do
7
+ it 'should return an Array of FbGraph::UserAchievement' do
8
+ mock_graph :get, 'matake/achievements', 'users/user_achievements/sample', :access_token => 'access_token' do
9
+ user.achievements.each do |achievement|
10
+ achievement.should be_instance_of FbGraph::UserAchievement
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ describe '#achieve!' do
17
+ let(:achievement_url) { 'http://fbgraphsample.heroku.com/achievements/1' }
18
+ it 'should return FbGraph::UserAchievement' do
19
+ mock_graph :post, 'matake/achievements', 'users/user_achievements/created', :access_token => 'access_token', :params => {
20
+ :achievement => achievement_url
21
+ } do
22
+ user.achieve!(achievement_url).should be_instance_of FbGraph::UserAchievement
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#unachieve!' do
28
+ let(:achievement_url) { 'http://fbgraphsample.heroku.com/achievements/1' }
29
+ it 'should return true' do
30
+ mock_graph :delete, 'matake/achievements', 'true', :access_token => 'access_token', :params => {
31
+ :achievement => achievement_url
32
+ } do
33
+ user.unachieve!(achievement_url).should be_true
34
+ end
35
+ end
36
+ end
37
+ end
@@ -44,4 +44,100 @@ describe FbGraph::NotFound do
44
44
  err = FbGraph::NotFound.new 'Not Found'
45
45
  err.code.should == 404
46
46
  end
47
- end
47
+ end
48
+
49
+ describe FbGraph::Exception, ".handle_httpclient_error" do
50
+ context "when WWW-Authenticate header is present" do
51
+ context "with an expired access token" do
52
+ let(:parsed_response) do
53
+ {
54
+ :error => {
55
+ :message => "Error validating access token: The session has been invalidated because the user has changed the password.",
56
+ :type => "OAuthException"
57
+ }
58
+ }
59
+ end
60
+ let(:headers) do
61
+ {
62
+ "WWW-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Error validating access token: The session has been invalidated because the user has changed the password.'
63
+ }
64
+ end
65
+
66
+ it "should raise an InvalidSession exception" do
67
+ lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::InvalidSession)
68
+ end
69
+ end
70
+
71
+ context "with an invalid access token" do
72
+ let(:parsed_response) do
73
+ {
74
+ :error => {
75
+ :message => 'Invalid OAuth access token.',
76
+ :type => "OAuthException"
77
+ }
78
+ }
79
+ end
80
+ let(:headers) do
81
+ {
82
+ "WWW-Authenticate" => 'OAuth "Facebook Platform" "invalid_token" "Invalid OAuth access token."'
83
+ }
84
+ end
85
+
86
+ it "should raise an InvalidToken exception" do
87
+ lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::InvalidToken)
88
+ end
89
+ end
90
+
91
+ context "with an invalid request" do
92
+ let(:parsed_response) do
93
+ {
94
+ :error => {
95
+ :message => '(#100) Must include the \"campaign_id\" index',
96
+ :type => "OAuthException"
97
+ }
98
+ }
99
+ end
100
+ let(:headers) do
101
+ {
102
+ "WWW-Authenticate" =>'OAuth "Facebook Platform" "invalid_request" "(#100) Must include the \"campaign_id\" index"'
103
+ }
104
+ end
105
+
106
+ it "should raise an InvalidRequest exception" do
107
+ lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, headers)}.should raise_exception(FbGraph::InvalidRequest)
108
+ end
109
+ end
110
+ end
111
+
112
+ context "without the WWW-Authenticate header" do
113
+ context "with an OAuthException" do
114
+ let(:parsed_response) do
115
+ {
116
+ :error => {
117
+ :message => 'Some kind of OAuthException',
118
+ :type => "OAuthException"
119
+ }
120
+ }
121
+ end
122
+
123
+ it "should raise an Unauthorized exception" do
124
+ lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::Unauthorized)
125
+ end
126
+ end
127
+
128
+ context "with an Exception" do
129
+ let(:parsed_response) do
130
+ {
131
+ :error => {
132
+ :message => 'Some kind of Exception',
133
+ :type => "Exception"
134
+ }
135
+ }
136
+ end
137
+
138
+ it "should raise a BadRequst exception" do
139
+ lambda {FbGraph::Exception.handle_httpclient_error(parsed_response, {})}.should raise_exception(FbGraph::BadRequest)
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe FbGraph::UserAchievement do
4
+ subject { achievement }
5
+ let(:achievement) { FbGraph::UserAchievement.new(attributes[:id], attributes) }
6
+ let(:attributes) do
7
+ {
8
+ :id => "10150351898227277",
9
+ :from => {
10
+ :id => "10150351898227277",
11
+ :name => "Nov Matake"
12
+ },
13
+ :start_time => "2011-09-27T14:18:33+0000",
14
+ :end_time => "2011-09-27T14:18:33+0000",
15
+ :publish_time => "2011-09-27T14:18:33+0000",
16
+ :application => {
17
+ :id => "134145643294322",
18
+ :name => "gem sample"
19
+ },
20
+ :achievement => {
21
+ :id => "10150310611431721",
22
+ :url => "http:\/\/fbgraphsample.heroku.com\/achievements\/1",
23
+ :type => "game.achievement",
24
+ :title => "1st Achievement"
25
+ },
26
+ :likes => {
27
+ :count => 0
28
+ },
29
+ :comments => {
30
+ :count => 0
31
+ }
32
+ }
33
+ end
34
+
35
+ its(:from) { should be_a FbGraph::User }
36
+ its(:achievement) { should be_a FbGraph::Achievement }
37
+ its(:application) { should be_a FbGraph::Application }
38
+ its(:created_time) { should == Time.parse(attributes[:publish_time]).utc }
39
+
40
+ describe '#destroy' do
41
+ it 'should call DELETE /:app_id/achievements' do
42
+ expect { achievement.destroy }.should request_to('10150351898227277/achievements', :delete)
43
+ end
44
+
45
+ it 'should delete achievement' do
46
+ mock_graph :delete, '10150351898227277/achievements', 'true', :access_token => 'app_token', :params => {
47
+ :achievement => achievement.achievement.url
48
+ } do
49
+ achievement.destroy(:access_token => 'app_token').should be_true
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,34 @@
1
+ {
2
+ "data":[
3
+ {
4
+ "id":354545238888,
5
+ "name":"MyPage",
6
+ "url":"http://www.facebook.com/MyPage",
7
+ "type":1,
8
+ "tabs":
9
+ {
10
+ "http://www.facebook.com/MyPage?sk=wall":"Wall",
11
+ "http://www.facebook.com/MyPage?sk=info":"Info",
12
+ "http://www.facebook.com/MyPage?sk=friendactivity":"Friend Activity",
13
+ "http://www.facebook.com/MyPage?sk=photos":"Photos",
14
+ "http://www.facebook.com/MyPage?sk=app_2373072222":"Discussions"
15
+ },
16
+ "picture":"http://profile.ak.fbcdn.net/hprofile-ak-snc4/41591_354545238178_3195000_s.jpg"
17
+ },
18
+ {
19
+ "id":469506610000,
20
+ "name":"My App",
21
+ "url":"http://www.facebook.com/apps/application.php?id=469506610000",
22
+ "type":2,
23
+ "tabs":[],
24
+ "picture":"http://profile.ak.fbcdn.net/static-ak/rsrc.php/v1/y0/r/XsEg9L6III_.jpg"
25
+ },
26
+ {
27
+ "id":403374556666,
28
+ "name":"www.example.com",
29
+ "url":"http://www.example.com/",
30
+ "type":7,
31
+ "tabs":[]
32
+ }
33
+ ]
34
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "data":[
3
+ {
4
+ "id":6002714885172,
5
+ "name":"Cooking",
6
+ "parent_category":"Activities"
7
+ },
8
+ {
9
+ "id":6002714885972,
10
+ "name":"Dancing",
11
+ "parent_category":"Activities"
12
+ },
13
+ {
14
+ "id":6002714885772,
15
+ "name":"DIYCrafts",
16
+ "parent_category":"Activities"
17
+ }
18
+ ]
19
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "data": [{
3
+ "url": "http:\/\/fbgraphsample.heroku.com\/achievements\/1",
4
+ "type": "game.achievement",
5
+ "title": "1st Achievement",
6
+ "image": [{
7
+ "url": "http:\/\/matake.jp\/images\/nov.gif"
8
+ }],
9
+ "description": "For testing purpose",
10
+ "data": {
11
+ "points": 50
12
+ },
13
+ "updated_time": "2011-09-27T08:06:59+0000",
14
+ "id": "10150310611431721",
15
+ "application": {
16
+ "id": "134145643294322",
17
+ "name": "gem sample",
18
+ "url": "http:\/\/www.facebook.com\/apps\/application.php?id=134145643294322"
19
+ },
20
+ "context": {
21
+ "display_order": 0
22
+ }
23
+ }]
24
+ }
@@ -0,0 +1,126 @@
1
+ {
2
+ "data": [{
3
+ "name": "Tatsuo Kudo",
4
+ "id": "500014620"
5
+ },
6
+ {
7
+ "name": "Shinichi Tomita",
8
+ "id": "500093930"
9
+ },
10
+ {
11
+ "name": "Toru Yamaguchi",
12
+ "id": "501566911"
13
+ },
14
+ {
15
+ "name": "Shingo Yamanaka",
16
+ "id": "508276415"
17
+ },
18
+ {
19
+ "name": "Yoichiro Tanaka",
20
+ "id": "579499538"
21
+ },
22
+ {
23
+ "name": "Daichi Morifuji",
24
+ "id": "619217309"
25
+ },
26
+ {
27
+ "name": "Hal Seki",
28
+ "id": "634229039"
29
+ },
30
+ {
31
+ "name": "Serkan Toto",
32
+ "id": "646151095"
33
+ },
34
+ {
35
+ "name": "Ando Yasushi",
36
+ "id": "666734137"
37
+ },
38
+ {
39
+ "name": "Ian Lewis",
40
+ "id": "693399432"
41
+ },
42
+ {
43
+ "name": "Yuichiro Masui",
44
+ "id": "707808977"
45
+ },
46
+ {
47
+ "name": "Hiroki Itoh",
48
+ "id": "727276664"
49
+ },
50
+ {
51
+ "name": "Lyo Kato",
52
+ "id": "727545420"
53
+ },
54
+ {
55
+ "name": "Yusuke Kawasaki",
56
+ "id": "731850967"
57
+ },
58
+ {
59
+ "name": "Peter Van der Wee\u00ebn",
60
+ "id": "751125729"
61
+ },
62
+ {
63
+ "name": "Hayashi Lef Tatsuya",
64
+ "id": "772689824"
65
+ },
66
+ {
67
+ "name": "Nat Sakimura",
68
+ "id": "1048138174"
69
+ },
70
+ {
71
+ "name": "Fumiko Kurano",
72
+ "id": "1111551581"
73
+ },
74
+ {
75
+ "name": "Taizo Matsuoka",
76
+ "id": "1185203299"
77
+ },
78
+ {
79
+ "name": "Yusuke Kondo",
80
+ "id": "1229826130"
81
+ },
82
+ {
83
+ "name": "Yusuke Kamiya",
84
+ "id": "1253149183"
85
+ },
86
+ {
87
+ "name": "Nobuhiro Nakajima",
88
+ "id": "1260873121"
89
+ },
90
+ {
91
+ "name": "Takashi Matsuo",
92
+ "id": "1533481595"
93
+ },
94
+ {
95
+ "name": "Ryo Ito",
96
+ "id": "1610605526"
97
+ },
98
+ {
99
+ "name": "Tatsuya Katsuhara",
100
+ "id": "1784765822"
101
+ },
102
+ {
103
+ "name": "Toyoaki Ohgochi",
104
+ "id": "100000648277699"
105
+ },
106
+ {
107
+ "name": "Atsuhiko Kimura",
108
+ "id": "100000982035037"
109
+ },
110
+ {
111
+ "name": "Yuya Yoshida",
112
+ "id": "100001212195915"
113
+ },
114
+ {
115
+ "name": "Rieko Suzuki",
116
+ "id": "100001220441239"
117
+ },
118
+ {
119
+ "name": "Kaori Miyata",
120
+ "id": "100002161472751"
121
+ },
122
+ {
123
+ "name": "Takamasa Uchiyama",
124
+ "id": "100002223021751"
125
+ }]
126
+ }