patient_zero 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1270910c6c0716586db130a0bb3bc42a135b884
4
- data.tar.gz: 651df620375f274d438be4b03e7824ab818a3794
3
+ metadata.gz: ce71c0439f0fe87f14660f320fc6cbde9a4fe9dc
4
+ data.tar.gz: 55b5fe0d9fd823d7e547f77e5395d7e88d73a484
5
5
  SHA512:
6
- metadata.gz: 52aa5cb238aac7d9262199b4a34f5b1b1227ea8d23ebdfe9384c3fadb68b71777a804b9eea9361d342e4ae5adab2e993b67b95409ff07227d2f46e0052ec0e64
7
- data.tar.gz: 39be0fe939bb7e9e1b098c561cc5a01047a6b1608b2eaa2dcc83edc2748a5060645f51d5823bb59a4775e38148b2053737802904f7381547ce1a672128e68076
6
+ metadata.gz: 32961c40e4712bbcf9d023ebd6cc5ae390648717df4818ddd80df2a9a4629ea6a93210e2c7d695b62984fb903719328c866b0cd14c56ad1cce0c18de9661eb19
7
+ data.tar.gz: 89b559a859c5ea756a4f57502bff911c3b1bc87e86975aff46862de3fb59e094574ba153a8d67e17ef6886870d013e4e0b65758139acf5094d1955b86ee5a1f2
@@ -6,7 +6,13 @@ module PatientZero
6
6
  end
7
7
 
8
8
  def impressions
9
- page_impressions + message_impressions
9
+ @impressions ||= page_impressions + message_impressions
10
+ end
11
+
12
+ def reach
13
+ @reach ||= analytical_data['total_reach'].find do |reach_hash|
14
+ reach_hash['key'] == 'Total Reach'
15
+ end['values'].each_value.reduce(:+)
10
16
  end
11
17
 
12
18
  def impressions_by_city
@@ -8,6 +8,10 @@ module PatientZero
8
8
  def engagements
9
9
  @engagements ||= messages.reduce(0) { |sum, message| sum + message.engagements }
10
10
  end
11
+
12
+ def reach
13
+ @reach ||= (0.12 * analytical_data['followers']).round
14
+ end
11
15
  end
12
16
  end
13
17
  end
@@ -2,7 +2,7 @@ module PatientZero
2
2
  class Source
3
3
  include Client
4
4
 
5
- attr_accessor :id, :name, :platform, :token
5
+ attr_accessor :id, :name, :platform, :token, :delete_id
6
6
 
7
7
  def initialize attributes, token
8
8
  @id = attributes.fetch 'id'
@@ -10,6 +10,7 @@ module PatientZero
10
10
  @invalid = attributes.fetch 'is_invalid'
11
11
  @tracked = attributes.fetch 'is_tracked'
12
12
  @platform = attributes.fetch 'platform'
13
+ @delete_id = attributes.fetch 'delete_id'
13
14
  @token = token
14
15
  end
15
16
 
@@ -27,10 +28,22 @@ module PatientZero
27
28
  raise NotFoundError, e
28
29
  end
29
30
 
30
- def profile_id
31
+ def platform_id
31
32
  id.split('#').last
32
33
  end
33
34
 
35
+ def social_type
36
+ id.scan(/\d+#(\w+)#\d+/).flatten.first
37
+ end
38
+
39
+ def parent
40
+ if delete_id == id
41
+ self
42
+ else
43
+ Source.find delete_id, token
44
+ end
45
+ end
46
+
34
47
  def analytics start_date: nil, end_date: nil
35
48
  @analytics ||= {}
36
49
  @analytics["#{start_date}#{end_date}"] ||= Analytics.for_platform platform, token: token, source_id: id, start_date: start_date, end_date: end_date
@@ -1,3 +1,3 @@
1
1
  module PatientZero
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -4,6 +4,7 @@ module PatientZero
4
4
  module Analytics
5
5
  describe Facebook do
6
6
  let(:source_id) { "12345##{platform}#1234567890" }
7
+ let(:token) { 'token-shmoken' }
7
8
  let(:platform) { 'facebook' }
8
9
  let(:message) do
9
10
  { 'platform' => 'FB',
@@ -14,22 +15,19 @@ module PatientZero
14
15
  'clicks' => 38 }
15
16
  end
16
17
  let(:messages) { [ message ] }
17
- let(:page_impressions) do
18
- [ { 'key'=>'Total',
19
- 'values'=>
20
- { '2010-10-10'=> 99 } } ]
21
- end
22
- let(:age_data) { {"13-17"=>14_000, "18-24"=>240_000, "25-34"=>650_000, "35-44"=>660_000, "45-54"=>380_000, "55-64"=>170_000, "65+"=>93_000} }
18
+ let(:page_impressions) { [ { 'key'=>'Total', 'values'=> { '2010-10-10'=> 99 } } ] }
23
19
  let(:impressions_by_city) { [{"title"=>"Atlanta, GA", "count"=>16_000}, {"title"=>"Los Angeles, CA", "count"=>15_000}, {"title"=>"San Diego, CA", "count"=>14_000}] }
24
20
  let(:impressions_by_age) { [{"key"=>"Users", "values"=>age_data}] }
25
21
  let(:impressions_by_gender) { {"F"=>100_000, "M"=>70_000, "U"=>10_000} }
26
- let(:token) { 'token-shmoken' }
22
+ let(:age_data) { {"13-17"=>14_000, "18-24"=>240_000, "25-34"=>650_000, "35-44"=>660_000, "45-54"=>380_000, "55-64"=>170_000, "65+"=>93_000} }
23
+ let(:total_reach) { [ { 'key'=>'Total Reach', 'values'=> { '2010-10-10'=> 4567 } } ] }
27
24
  let(:analytical_data) do
28
25
  { 'messages' => messages,
29
26
  'page_impressions' => page_impressions,
30
27
  'impressions_by_cities' => impressions_by_city,
31
28
  'impressions_by_ages' => impressions_by_age,
32
- 'impressions_by_genders' => impressions_by_gender }
29
+ 'impressions_by_genders' => impressions_by_gender,
30
+ 'total_reach' => total_reach }
33
31
  end
34
32
  let(:facebook_analytics) { Facebook.new token: token, source_id: source_id }
35
33
  before{ allow(facebook_analytics).to receive(:analytical_data).and_return analytical_data }
@@ -72,6 +70,12 @@ module PatientZero
72
70
  expect(facebook_analytics.engagements).to eq 160
73
71
  end
74
72
  end
73
+
74
+ describe '#reach' do
75
+ it 'returns the sum of total_reach for each day' do
76
+ expect(facebook_analytics.reach).to eq 4567
77
+ end
78
+ end
75
79
  end
76
80
  end
77
81
  end
@@ -14,7 +14,8 @@ module PatientZero
14
14
  end
15
15
  let(:messages) { [ message ] }
16
16
  let(:token) { 'token-shmoken' }
17
- let(:analytical_data) { { 'messages' => messages, 'total_impressions' => 13 } }
17
+ let(:followers) { 35 }
18
+ let(:analytical_data) { { 'messages' => messages, 'total_impressions' => 13, 'followers' => followers } }
18
19
  let(:twitter_analytics) { Twitter.new token: token, source_id: source_id }
19
20
  before{ allow(twitter_analytics).to receive(:analytical_data).and_return analytical_data }
20
21
 
@@ -29,6 +30,12 @@ module PatientZero
29
30
  expect(twitter_analytics.engagements).to eq 20
30
31
  end
31
32
  end
33
+
34
+ describe '#reach' do
35
+ it 'returns 12% of the number of follers, rounded to the nearest whole' do
36
+ expect(twitter_analytics.reach).to eq (followers * 0.12).round
37
+ end
38
+ end
32
39
  end
33
40
  end
34
41
  end
@@ -2,7 +2,9 @@ require 'spec_helper'
2
2
 
3
3
  module PatientZero
4
4
  describe Source do
5
- let(:id) { '12345#instagram_account#1234567890' }
5
+ let(:id) { "12345##{social_type}#1234567890" }
6
+ let(:delete_id) { id }
7
+ let(:social_type) { 'facebook_account' }
6
8
  let(:token) { 'token-shmoken' }
7
9
  let(:sources_response_data) { [source_response_data] }
8
10
  let(:source_response_data) do
@@ -10,8 +12,8 @@ module PatientZero
10
12
  'name' => 'account_name',
11
13
  'is_invalid' => false,
12
14
  'is_tracked' => true,
13
- 'platform' => 'instagram',
14
- 'delete_id' => id }
15
+ 'platform' => 'facebook',
16
+ 'delete_id' => delete_id }
15
17
  end
16
18
  let(:source) { Source.new source_response_data, token }
17
19
 
@@ -63,9 +65,34 @@ module PatientZero
63
65
  end
64
66
  end
65
67
 
66
- describe '#profile_id' do
68
+ describe '#platform_id' do
67
69
  it 'returns the number at the end of the id' do
68
- expect(source.profile_id).to eq '1234567890'
70
+ expect(source.platform_id).to eq '1234567890'
71
+ end
72
+ end
73
+
74
+ describe '#social_type' do
75
+ it 'returns the source type' do
76
+ expect(source.social_type).to eq social_type
77
+ end
78
+ end
79
+
80
+ describe '#parent' do
81
+ context 'when the delete_id is the same as the id' do
82
+ it 'returns self' do
83
+ expect(source.parent).to eq source
84
+ end
85
+ end
86
+ context 'when the delete_id is not the same as the id' do
87
+ let(:delete_id) { '12345#facebook_page#1234567890' }
88
+ before { allow(Source).to receive(:find).and_return double :parent_source, id: delete_id }
89
+ it 'calls Source.find' do
90
+ expect(Source).to receive(:find)
91
+ source.parent
92
+ end
93
+ it 'returns a source that is not the same as itself' do
94
+ expect(source.parent.id).to_not eq source.id
95
+ end
69
96
  end
70
97
  end
71
98
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patient_zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Zaninovich
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-22 00:00:00.000000000 Z
12
+ date: 2015-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -198,4 +198,3 @@ test_files:
198
198
  - spec/patient_zero/profile_spec.rb
199
199
  - spec/patient_zero/source_spec.rb
200
200
  - spec/spec_helper.rb
201
- has_rdoc: