patient_zero 0.2.2 → 0.3.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: 986f8ed157da8c5cfd002539e08ba67b214e5cec
4
- data.tar.gz: a89ba560beb80054824ec480edc7ca8681a281fd
3
+ metadata.gz: a037356146290a626fa56f41f3ea9ac05ef1ba8e
4
+ data.tar.gz: 140f175a8f2f333105e5ac50b8b4263419ca8bac
5
5
  SHA512:
6
- metadata.gz: c939f5cc167ead485b684cbf1e3f3aea8a0dc37b6aedeb9b66c2101e8f31346422a9d53a9bec67749dcaa0e54eb704572d8bdb4a5266a4b7022680875d40c361
7
- data.tar.gz: 67a76674e62505781e1067a8fc393251b70107db7e179b27f489edb8086fd09f1b5da39ba98796b0f1f48bf314022ce32e2fc2a72e4092b45eb59f1efe8658be
6
+ metadata.gz: b0e70002f5430e5b3453802ada0c013e9c8154087df702d50b8c21e2c0c310d344a70f32ea89b9822a9a6d2d0de9d3adfdad10115ba7e8eba513cf9acfab3209
7
+ data.tar.gz: af84b55608d2edfd6f415c883d80eba65f07ffe7f201ad9dd537553f9b131dd1c2abab310d80a9bd6ba10d8c6fd98548d801d33ae59b72b49fe61a84dbecb5e0
@@ -1,6 +1,8 @@
1
1
  module PatientZero
2
2
  module Analytics
3
- class Base < Client
3
+ class Base
4
+ include Client
5
+
4
6
  attr_accessor :token, :source_id, :start_date, :end_date
5
7
 
6
8
  def initialize token:, source_id:, start_date: nil, end_date: nil
@@ -1,5 +1,7 @@
1
1
  module PatientZero
2
- class Authorization < Client
2
+ class Authorization
3
+ include Client
4
+
3
5
  def self.token
4
6
  response = post '/mobile/api/v1/user/login', email: PatientZero.email, password: PatientZero.password
5
7
  response['user_token']
@@ -1,43 +1,33 @@
1
1
  module PatientZero
2
- class Client
3
- def self.connection
4
- @@connection ||= Faraday.new(PatientZero.url) do |faraday|
2
+ module Client
3
+ def self.included base
4
+ base.extend self
5
+ end
6
+
7
+ def connection
8
+ PatientZero.connection ||= Faraday.new(PatientZero.url) do |faraday|
5
9
  faraday.request :url_encoded
6
10
  faraday.response :logger
7
11
  faraday.adapter Faraday.default_adapter
8
12
  end
9
13
  end
10
14
 
11
- def self.parse request
15
+ def parse request
12
16
  response = JSON.parse request.body
13
17
  raise Error, response['error'] unless response['error'].nil?
14
18
  response
15
19
  end
16
20
 
17
- def self.get *args
18
- parse connection.get *args
19
- end
20
-
21
- def self.post *args
22
- parse connection.post *args
23
- end
24
-
25
- def self.put *args
26
- parse connection.put *args
27
- end
28
-
29
- private
30
-
31
21
  def get *args
32
- self.class.get *args
22
+ parse connection.get *args
33
23
  end
34
24
 
35
25
  def post *args
36
- self.class.post *args
26
+ parse connection.post *args
37
27
  end
38
28
 
39
29
  def put *args
40
- self.class.put *args
30
+ parse connection.put *args
41
31
  end
42
32
  end
43
33
  end
@@ -1,6 +1,6 @@
1
1
  module PatientZero
2
2
  module Configurable
3
- attr_accessor :url, :api_key, :email, :password
3
+ attr_accessor :url, :api_key, :email, :password, :connection
4
4
 
5
5
  def self.extended(base)
6
6
  base.reset
@@ -1,5 +1,6 @@
1
1
  module PatientZero
2
- class Organization < Client
2
+ class Organization
3
+ include Client
3
4
  attr_accessor :id, :name, :avatar
4
5
 
5
6
  def initialize attributes
@@ -1,5 +1,7 @@
1
1
  module PatientZero
2
- class Profile < Client
2
+ class Profile
3
+ include Client
4
+
3
5
  CATEGORIES = ['General', 'Brand', 'Television', 'Movies', 'Music', 'Person', 'Sports', 'Politics', 'Topic']
4
6
 
5
7
  attr_accessor :id, :expression, :name, :category
@@ -1,5 +1,7 @@
1
1
  module PatientZero
2
- class Source < Client
2
+ class Source
3
+ include Client
4
+
3
5
  attr_accessor :id, :name, :platform, :token
4
6
 
5
7
  def initialize attributes, token
@@ -1,3 +1,3 @@
1
1
  module PatientZero
2
- VERSION = "0.2.2"
2
+ VERSION = '0.3.0'
3
3
  end
@@ -10,11 +10,11 @@ module PatientZero
10
10
  let(:token) { 'token-shmoken' }
11
11
  let(:analytical_data) { { 'stats' => [ { 'id' => source_id, 'platform' => platform, 'name' => name, 'messages' => messages } ] } }
12
12
  subject(:analytics_base) { Base.new token: token, source_id: source_id }
13
- before { allow(Client).to receive(:get).with('/mobile/api/v1/analytics', anything).and_return analytical_data }
13
+ before { allow(analytics_base).to receive(:get).with('/mobile/api/v1/analytics', anything).and_return analytical_data }
14
14
 
15
15
  describe '#name' do
16
16
  it 'calls the analytics API endpoint' do
17
- expect(Client).to receive(:get).with('/mobile/api/v1/analytics', anything)
17
+ expect(analytics_base).to receive(:get).with('/mobile/api/v1/analytics', anything)
18
18
  analytics_base.name
19
19
  end
20
20
  it 'pulls the name from the analytical_data hash' do
@@ -24,7 +24,7 @@ module PatientZero
24
24
 
25
25
  describe '#platform' do
26
26
  it 'calls the analytics API endpoint' do
27
- expect(Client).to receive(:get).with('/mobile/api/v1/analytics', anything)
27
+ expect(analytics_base).to receive(:get).with('/mobile/api/v1/analytics', anything)
28
28
  analytics_base.name
29
29
  end
30
30
  it 'pulls the platform from the analytical_data hash' do
@@ -34,7 +34,7 @@ module PatientZero
34
34
 
35
35
  describe '#messages' do
36
36
  it 'calls the analytics API endpoint' do
37
- expect(Client).to receive(:get).with('/mobile/api/v1/analytics', anything)
37
+ expect(analytics_base).to receive(:get).with('/mobile/api/v1/analytics', anything)
38
38
  analytics_base.name
39
39
  end
40
40
  context 'when there are messages' do
@@ -5,9 +5,9 @@ module PatientZero
5
5
  describe '.token' do
6
6
  let(:token) { 'token-shmoken' }
7
7
  let(:authorization_response) { response_with_body user_token: token }
8
- before{ allow(Client.connection).to receive(:post).with('/mobile/api/v1/user/login', anything).and_return authorization_response }
8
+ before{ allow(Authorization.connection).to receive(:post).with('/mobile/api/v1/user/login', anything).and_return authorization_response }
9
9
  it 'calls the login api endpoint' do
10
- expect(Client.connection).to receive(:post).with('/mobile/api/v1/user/login', anything)
10
+ expect(Authorization.connection).to receive(:post).with('/mobile/api/v1/user/login', anything)
11
11
  Authorization.token
12
12
  end
13
13
  context 'when email and password are correct' do
@@ -1,31 +1,97 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module PatientZero
4
+ class FakeClient
5
+ include Client
6
+ end
7
+
4
8
  describe Client do
5
9
  let(:token) { 'token-shmoken' }
6
10
  let(:body) { "{}" }
7
- let(:response) { double body: body }
11
+ let(:response) { double :response, body: body }
12
+ let(:connection) { double :conn, get: response, post: response, put: response }
8
13
 
9
14
  describe '.connection' do
10
15
  it 'news up a connection to the Viral Heat API' do
11
- expect(Client.connection).to be_a Faraday::Connection
16
+ expect(FakeClient.connection).to be_a Faraday::Connection
17
+ end
18
+
19
+ it 'exists on the instance' do
20
+ expect { FakeClient.new.connection }.to_not raise_error
12
21
  end
13
22
  end
14
23
 
15
24
  describe '.parse' do
16
- subject(:parse) { Client.parse response }
25
+ it 'exists on the instance' do
26
+ expect { FakeClient.new.parse response }.to_not raise_error
27
+ end
17
28
  context 'when the response contains an error key with nil' do
18
29
  let(:body) { "{\"status\":200,\"error\":null,\"user_token\":\"#{token}\"}" }
19
30
  it 'returns a hash of data' do
20
- expect(parse).to be_a Hash
31
+ expect(FakeClient.parse response).to be_a Hash
21
32
  end
22
33
  end
23
34
  context 'when the response contains an error key that is not nil' do
24
35
  let(:body) { "{\"status\":403,\"error\":\"Invalid login/password\",\"user_token\":null}" }
25
36
  it 'raises an error' do
26
- expect { parse }.to raise_error Error
37
+ expect { FakeClient.parse response }.to raise_error Error
27
38
  end
28
39
  end
29
40
  end
41
+
42
+ describe '.get' do
43
+ before { allow(PatientZero).to receive(:connection).and_return connection }
44
+
45
+ it 'delegates to the connection' do
46
+ expect(PatientZero.connection).to receive(:get).with('an arg') { response }
47
+ FakeClient.get 'an arg'
48
+ end
49
+
50
+ it 'calls parse' do
51
+ expect(FakeClient).to receive(:parse).with response
52
+ FakeClient.get 'an arg'
53
+ end
54
+
55
+ it 'exists on the instance' do
56
+ expect { FakeClient.new.get 'an arg' }.to_not raise_error
57
+ end
58
+ end
59
+
60
+ describe '.post' do
61
+ before { allow(PatientZero).to receive(:connection).and_return connection }
62
+
63
+ it 'delegates to the connection' do
64
+ expect(PatientZero.connection).to receive(:post).with('an arg') { response }
65
+ FakeClient.post 'an arg'
66
+ end
67
+
68
+ it 'calls parse' do
69
+ expect(FakeClient).to receive(:parse).with response
70
+ FakeClient.post 'an arg'
71
+ end
72
+
73
+ it 'exists on the instance' do
74
+ expect { FakeClient.new.post 'an arg' }.to_not raise_error
75
+ end
76
+ end
77
+
78
+ describe '.put' do
79
+ before { allow(PatientZero).to receive(:connection).and_return connection }
80
+
81
+ it 'delegates to the connection' do
82
+ expect(PatientZero.connection).to receive(:put).with('an arg') { response }
83
+ FakeClient.put 'an arg'
84
+ end
85
+
86
+ it 'calls parse' do
87
+ expect(FakeClient).to receive(:parse).with response
88
+ FakeClient.put 'an arg'
89
+ end
90
+
91
+ it 'exists on the instance' do
92
+ expect { FakeClient.new.put 'an arg' }.to_not raise_error
93
+ end
94
+ end
95
+
30
96
  end
31
97
  end
@@ -20,10 +20,10 @@ module PatientZero
20
20
  describe '.all' do
21
21
  let(:organizations) { [organization] }
22
22
  let(:organizations_response) { response_with_body organizations: organizations_response_data }
23
- before { allow(Client.connection).to receive(:get).with('/mobile/api/v1/organizations', anything) { organizations_response } }
23
+ before { allow(Organization.connection).to receive(:get).with('/mobile/api/v1/organizations', anything) { organizations_response } }
24
24
 
25
25
  it 'calls the organizations api endpoint' do
26
- expect(Client.connection).to receive(:get).with('/mobile/api/v1/organizations', anything)
26
+ expect(Organization.connection).to receive(:get).with('/mobile/api/v1/organizations', anything)
27
27
  Organization.all
28
28
  end
29
29
 
@@ -52,10 +52,10 @@ module PatientZero
52
52
  describe '#token' do
53
53
  let(:organization_specific_token) { 'organization-token' }
54
54
  let(:switch_response) { response_with_body user_token: organization_specific_token }
55
- before { allow(Client.connection).to receive(:get).with("/mobile/api/v1/organizations/#{id}/switch", anything).and_return switch_response }
55
+ before { allow(Organization.connection).to receive(:get).with("/mobile/api/v1/organizations/#{id}/switch", anything).and_return switch_response }
56
56
 
57
57
  it 'calls the organization switch api endpoint' do
58
- expect(Client.connection).to receive(:get).with("/mobile/api/v1/organizations/#{id}/switch", anything)
58
+ expect(Organization.connection).to receive(:get).with("/mobile/api/v1/organizations/#{id}/switch", anything)
59
59
  organization.token
60
60
  end
61
61
 
@@ -21,10 +21,10 @@ module PatientZero
21
21
 
22
22
  describe '.all' do
23
23
  let(:profile_response) { response_with_body profiles: profiles }
24
- before { allow(Client.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything) { profile_response } }
24
+ before { allow(Profile.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything) { profile_response } }
25
25
 
26
26
  it 'calls the profiles api endpoint' do
27
- expect(Client.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything)
27
+ expect(Profile.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything)
28
28
  Profile.all
29
29
  end
30
30
 
@@ -35,10 +35,10 @@ module PatientZero
35
35
 
36
36
  describe '.find' do
37
37
  let(:profile_response) { response_with_body profiles: [profile_data] }
38
- before { allow(Client.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything) { profile_response } }
38
+ before { allow(Profile.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything) { profile_response } }
39
39
 
40
40
  it 'calls the profiles api endpoint' do
41
- expect(Client.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything)
41
+ expect(Profile.connection).to receive(:get).with('/social/api/v2/monitoring/profiles', anything)
42
42
  Profile.find id
43
43
  end
44
44
 
@@ -49,10 +49,10 @@ module PatientZero
49
49
 
50
50
  describe '.create' do
51
51
  let(:profile_response) { response_with_body profile: profile_data }
52
- before { allow(Client.connection).to receive(:post).with('/social/api/v2/monitoring/profiles', anything) { profile_response } }
52
+ before { allow(Profile.connection).to receive(:post).with('/social/api/v2/monitoring/profiles', anything) { profile_response } }
53
53
 
54
54
  it 'calls the profiles api endpoint' do
55
- expect(Client.connection).to receive(:post).with('/social/api/v2/monitoring/profiles', anything)
55
+ expect(Profile.connection).to receive(:post).with('/social/api/v2/monitoring/profiles', anything)
56
56
  Profile.create profile_data
57
57
  end
58
58
 
@@ -60,20 +60,20 @@ module PatientZero
60
60
  expect(Profile.create(profile_data)).to be_a Profile
61
61
  end
62
62
  end
63
-
63
+
64
64
  describe '.update' do
65
- let(:params) { {id: 1} }
65
+ let(:params) { {id: 1} }
66
66
  let(:profile_response) { response_with_body profile: profile_data }
67
- before { allow(Client.connection).to receive(:put).with("/social/api/v2/monitoring/profiles/#{params[:id]}", anything) { profile_response } }
67
+ before { allow(Profile.connection).to receive(:put).with("/social/api/v2/monitoring/profiles/#{params[:id]}", anything) { profile_response } }
68
68
 
69
69
  it 'calls the profiles api endpoint' do
70
- expect(Client.connection).to receive(:put).with("/social/api/v2/monitoring/profiles/#{params[:id]}", anything)
70
+ expect(Profile.connection).to receive(:put).with("/social/api/v2/monitoring/profiles/#{params[:id]}", anything)
71
71
  Profile.update params
72
72
  end
73
73
 
74
74
  it 'returns a Profile object' do
75
75
  expect(Profile.update(params)).to be_a Profile
76
76
  end
77
- end
77
+ end
78
78
  end
79
79
  end
@@ -18,10 +18,10 @@ module PatientZero
18
18
  describe '.all' do
19
19
  before do
20
20
  allow(Authorization).to receive(:token).and_return token
21
- allow(Client.connection).to receive(:get).with('/mobile/api/v1/sources/', anything).and_return response_with_body sources: sources_response_data
21
+ allow(Source.connection).to receive(:get).with('/mobile/api/v1/sources/', anything).and_return response_with_body sources: sources_response_data
22
22
  end
23
23
  it 'calls the sources index api' do
24
- expect(Client.connection).to receive(:get).with('/mobile/api/v1/sources/', anything)
24
+ expect(Source.connection).to receive(:get).with('/mobile/api/v1/sources/', anything)
25
25
  Source.all
26
26
  end
27
27
  it 'calls Authorization.token if no token is passed in' do
@@ -35,7 +35,7 @@ module PatientZero
35
35
  end
36
36
  context 'when the associated organization token has no sources' do
37
37
  it 'returns an empty array' do
38
- allow(Client.connection).to receive(:get).with('/mobile/api/v1/sources/', anything).and_return response_with_body sources: []
38
+ allow(Source.connection).to receive(:get).with('/mobile/api/v1/sources/', anything).and_return response_with_body sources: []
39
39
  expect(Source.all).to be_empty
40
40
  end
41
41
  end
@@ -44,10 +44,10 @@ module PatientZero
44
44
  describe '.find' do
45
45
  before do
46
46
  allow(Authorization).to receive(:token).and_return token
47
- allow(Client.connection).to receive(:get).with('/mobile/api/v1/sources/show/', anything).and_return response_with_body source: source_response_data
47
+ allow(Source.connection).to receive(:get).with('/mobile/api/v1/sources/show/', anything).and_return response_with_body source: source_response_data
48
48
  end
49
49
  it 'calls the sources show api' do
50
- expect(Client.connection).to receive(:get).with('/mobile/api/v1/sources/show/', anything)
50
+ expect(Source.connection).to receive(:get).with('/mobile/api/v1/sources/show/', anything)
51
51
  Source.find id, token
52
52
  end
53
53
  context 'when the source exists' do
@@ -57,7 +57,7 @@ module PatientZero
57
57
  end
58
58
  context 'when no source exists' do
59
59
  it 'throws an PatientZero::Error' do
60
- allow(Client).to receive(:get).and_raise Error, 'The source could not be found. Please check your inputs.'
60
+ allow(Source).to receive(:get).and_raise Error, 'The source could not be found. Please check your inputs.'
61
61
  expect{ Source.find id, token }.to raise_error PatientZero::NotFoundError
62
62
  end
63
63
  end
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.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Zaninovich