social_count 0.0.4 → 0.0.6
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.
- checksums.yaml +8 -8
- data/lib/social_count/error.rb +11 -0
- data/lib/social_count/facebook_user.rb +13 -2
- data/lib/social_count/version.rb +1 -1
- data/spec/social_count/facebook_user_spec.rb +32 -4
- data/spec/social_count/twitter_user_spec.rb +2 -2
- data/spec/support/credentials.rb.example +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZDg1MDdkOTUxZTlhZjM1ZTZiODM1MmI0Y2EzM2ZkZmU1NGMwZWY5NQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjE4YzY1NGFiMjUwODRhYWFiNmFlOTU1NmMxMDZmNDkyOWNiZDAyMA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Y2Y5MDc1MzVlYzQ2MmEyY2FhZTU0MjQxZjhmNDRlNThkMDBiYTY5ZjJhNGU4
|
10
|
+
MmE5OGFmNDA0YjQ4OGU3NmZmNjEyNjBkOTg1MDM4NmMxNzJkMGZlNzY3OWFk
|
11
|
+
MjUyMDJlZDZjOGJhOWU2NTE3ZjYyNGY2OTE1ZGFhMzI1YjJiMzk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NzA3OGJjZDUzMzRmMjk4NjhmZTUxOWFkZWY5NTUwNTMxYWM5M2UyNDQ3NTdm
|
14
|
+
YTczNjI3N2JhODBmNzBmMjJjYWEwZDVlMzY1NDhiMDNkZjUwNTA2YWRkYTJj
|
15
|
+
Njc4Y2EzYjY0YzZiYTczYTM0Nzc5Yzc4OWUwMmM0ODVjZGIwMTA=
|
data/lib/social_count/error.rb
CHANGED
@@ -11,4 +11,15 @@ module SocialCount
|
|
11
11
|
"Code(s): #{code}\nSee code explanations at https://dev.twitter.com/docs/error-codes-responses"
|
12
12
|
end
|
13
13
|
end
|
14
|
+
class FacebookApiError < Error
|
15
|
+
attr_reader :code
|
16
|
+
def initialize(msg, code)
|
17
|
+
@code = code
|
18
|
+
@msg = msg
|
19
|
+
super(msg)
|
20
|
+
end
|
21
|
+
def to_s
|
22
|
+
"Code: #{code}\n#{@msg}\nSee code explanations at https://developers.facebook.com/docs/reference/api/errors/"
|
23
|
+
end
|
24
|
+
end
|
14
25
|
end
|
@@ -28,7 +28,12 @@ module SocialCount
|
|
28
28
|
return unless valid?
|
29
29
|
url = "#{DOMAIN}/fql?q=#{query(column)}"
|
30
30
|
response = self.class.get_http_response(url)
|
31
|
-
JSON.parse(response.body)
|
31
|
+
response = JSON.parse(response.body)
|
32
|
+
data = response['data']
|
33
|
+
raise SocialCount::FacebookApiError.new("The social_count gem could not parse the response from the Facebook Graph API: #{response}", 0) unless data.is_a?(Array)
|
34
|
+
return nil if data.empty?
|
35
|
+
raise SocialCount::FacebookApiError.new("The social_count gem could not parse the response from the Facebook Graph API: #{response}", 0) unless data[0].is_a?(Hash)
|
36
|
+
data[0]["#{column}_count"]
|
32
37
|
end
|
33
38
|
def query(column)
|
34
39
|
"SELECT #{column}_count FROM user WHERE uid=#{id}"
|
@@ -36,7 +41,13 @@ module SocialCount
|
|
36
41
|
|
37
42
|
class << self
|
38
43
|
def access_token
|
39
|
-
@access_token
|
44
|
+
return @access_token unless @access_token.nil?
|
45
|
+
response = get_http_response(access_url)
|
46
|
+
unless response.is_a?(Net::HTTPOK)
|
47
|
+
response = JSON.parse(response.body)
|
48
|
+
raise SocialCount::FacebookApiError.new("Facebook API returned the following error: #{response["error"]["message"]}", response["error"]["code"])
|
49
|
+
end
|
50
|
+
@access_token = response.body.split("access_token=")[1]
|
40
51
|
end
|
41
52
|
private
|
42
53
|
def access_url
|
data/lib/social_count/version.rb
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
module SocialCount
|
4
|
+
class FacebookUser
|
5
|
+
class << self
|
6
|
+
def reset_credentials
|
7
|
+
@access_token = nil
|
8
|
+
@access_url = nil
|
9
|
+
@credentials = nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
describe SocialCount::FacebookUser do
|
4
16
|
before(:all) do
|
5
17
|
SocialCount.credentials = TestCredentials::INSTANCE
|
@@ -23,10 +35,10 @@ describe SocialCount::FacebookUser do
|
|
23
35
|
|
24
36
|
describe "name" do
|
25
37
|
it "cannot be an empty string" do
|
26
|
-
expect{SocialCount::
|
38
|
+
expect{SocialCount::FacebookUser.new('')}.to raise_error(SocialCount::Error, "SocialCount::FacebookUser#name cannot be blank")
|
27
39
|
end
|
28
40
|
it "cannot be nil" do
|
29
|
-
expect{SocialCount::
|
41
|
+
expect{SocialCount::FacebookUser.new(nil)}.to raise_error(SocialCount::Error, "SocialCount::FacebookUser#name cannot be blank")
|
30
42
|
end
|
31
43
|
end
|
32
44
|
|
@@ -52,7 +64,7 @@ describe SocialCount::FacebookUser do
|
|
52
64
|
it "should get the user's follower count" do
|
53
65
|
count = @facebook.follower_count
|
54
66
|
count.should be_a(Fixnum)
|
55
|
-
count.should eq(
|
67
|
+
count.should eq(20394278)
|
56
68
|
end
|
57
69
|
it "should handle friend_count gracefully" do
|
58
70
|
@facebook.friend_count.should be_nil
|
@@ -66,7 +78,7 @@ describe SocialCount::FacebookUser do
|
|
66
78
|
it "should get the user's friend count" do
|
67
79
|
count = @facebook.friend_count
|
68
80
|
count.should be_a(Fixnum)
|
69
|
-
count.should eq(
|
81
|
+
count.should eq(586)
|
70
82
|
end
|
71
83
|
it "should handle friend_count gracefully" do
|
72
84
|
@facebook.follower_count.should be_nil
|
@@ -96,4 +108,20 @@ describe SocialCount::FacebookUser do
|
|
96
108
|
non_existent_user.follower_count.should be_nil
|
97
109
|
end
|
98
110
|
end
|
111
|
+
|
112
|
+
describe "expired credentials" do
|
113
|
+
before(:all) do
|
114
|
+
@old_credentials = SocialCount.credentials
|
115
|
+
SocialCount::FacebookUser.reset_credentials
|
116
|
+
SocialCount.credentials = TestCredentials::EXPIRED_CREDENTIALS
|
117
|
+
@facebook = SocialCount::FacebookUser.new(username)
|
118
|
+
end
|
119
|
+
it "should raise an exception when generating the access token" do
|
120
|
+
expect{@facebook.follower_count}.to raise_error(SocialCount::FacebookApiError, "Code: 1\nFacebook API returned the following error: Error validating client secret.\nSee code explanations at https://developers.facebook.com/docs/reference/api/errors/")
|
121
|
+
end
|
122
|
+
after(:all) do
|
123
|
+
SocialCount.credentials = @old_credentials
|
124
|
+
SocialCount::FacebookUser.reset_credentials
|
125
|
+
end
|
126
|
+
end
|
99
127
|
end
|
@@ -39,7 +39,7 @@ describe SocialCount::TwitterUser do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should get the follow count" do
|
42
|
-
@twitter.follower_count.should eq(
|
42
|
+
@twitter.follower_count.should eq(12872)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
@@ -69,7 +69,7 @@ describe SocialCount::TwitterUser do
|
|
69
69
|
@twitter = SocialCount::TwitterUser.new(username)
|
70
70
|
end
|
71
71
|
it "should raise an exception" do
|
72
|
-
expect{@twitter.follower_count}.to raise_error(SocialCount::TwitterApiError, "Code(s):
|
72
|
+
expect{@twitter.follower_count}.to raise_error(SocialCount::TwitterApiError, "Code(s): 32\nSee code explanations at https://dev.twitter.com/docs/error-codes-responses")
|
73
73
|
end
|
74
74
|
after(:all) do
|
75
75
|
SocialCount.credentials = @old_credentials
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: social_count
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Isaac Betesh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|