data-com-api 0.0.1 → 0.1.0

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.
Files changed (38) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +81 -3
  3. data/data-com-api.gemspec +3 -1
  4. data/lib/data-com-api/api_uri.rb +40 -0
  5. data/lib/data-com-api/client.rb +247 -0
  6. data/lib/data-com-api/company.rb +17 -0
  7. data/lib/data-com-api/company_contact_count/department.rb +17 -0
  8. data/lib/data-com-api/company_contact_count/level.rb +17 -0
  9. data/lib/data-com-api/contact.rb +32 -0
  10. data/lib/data-com-api/errors.rb +96 -0
  11. data/lib/data-com-api/query_parameters.rb +66 -0
  12. data/lib/data-com-api/responses/base.rb +19 -0
  13. data/lib/data-com-api/responses/company_contact_count.rb +96 -0
  14. data/lib/data-com-api/responses/contacts.rb +97 -0
  15. data/lib/data-com-api/responses/search_base.rb +112 -0
  16. data/lib/data-com-api/responses/search_company.rb +23 -0
  17. data/lib/data-com-api/responses/search_contact.rb +23 -0
  18. data/lib/data-com-api/version.rb +1 -1
  19. data/lib/data-com-api.rb +14 -1
  20. data/spec/factories/client_factory.rb +12 -0
  21. data/spec/factories/data-com/company_contact_count/department_factory.rb +14 -0
  22. data/spec/factories/data-com/company_contact_count/level_factory.rb +14 -0
  23. data/spec/factories/data-com/company_contact_count_response_factory.rb +39 -0
  24. data/spec/factories/data-com/contact_factory.rb +35 -0
  25. data/spec/factories/data-com/contacts_response_factory.rb +30 -0
  26. data/spec/factories/data-com/search_contact_response_factory.rb +30 -0
  27. data/spec/factories/query_parameters_factory.rb +11 -0
  28. data/spec/factories/responses/search_contact_factory.rb +13 -0
  29. data/spec/integration/client_spec.rb +167 -0
  30. data/spec/integration/webmock_spec.rb +34 -0
  31. data/spec/models/client_spec.rb +164 -0
  32. data/spec/models/query_parameters_spec.rb +42 -0
  33. data/spec/models/search_base_spec.rb +80 -0
  34. data/spec/spec_helper.rb +22 -2
  35. data/spec/support/data_com_api_stub_requests.rb +229 -0
  36. metadata +75 -8
  37. data/spec/factories.rb +0 -4
  38. data/spec/requests/client_spec.rb +0 -6
@@ -0,0 +1,167 @@
1
+ require 'spec_helper'
2
+ require 'faker'
3
+ require 'data-com-api/responses/search_contact'
4
+ require 'data-com-api/responses/search_company'
5
+ require 'data-com-api/responses/company_contact_count'
6
+ require 'data-com-api/responses/contacts'
7
+ require 'data-com-api/client'
8
+ require 'data-com-api/company_contact_count/department'
9
+ require 'data-com-api/company_contact_count/level'
10
+ require 'data-com-api/contact'
11
+
12
+ describe DataComApi::Client do
13
+
14
+ subject(:client) { FactoryGirl.build(:client) }
15
+
16
+ describe "#search_contact" do
17
+
18
+ it "has records when searched with no params" do
19
+ DataComApiStubRequests.stub_search_contact(
20
+ total_hits: 10
21
+ )
22
+
23
+ expect(client.search_contact.size).to be > 0
24
+ end
25
+
26
+ context "when searching by firstname" do
27
+ before do
28
+ DataComApiStubRequests.stub_search_few_contacts(
29
+ total_hits: 1,
30
+ query: {
31
+ firstname: 'Dummy'
32
+ },
33
+ records: FactoryGirl.build_list(:data_com_contact, 1,
34
+ firstname: 'Dummy'
35
+ )
36
+ )
37
+
38
+ DataComApiStubRequests.stub_search_few_contacts(
39
+ total_hits: 0,
40
+ query: {
41
+ firstname: 'DoesntExist'
42
+ }
43
+ )
44
+ end
45
+
46
+ it "with firstname Dummy it has records with Dummy as firstname" do
47
+ expect(
48
+ client.search_contact(firstname: 'Dummy').all.first.first_name
49
+ ).to eq('Dummy')
50
+ end
51
+
52
+ it "with firstname DoesntExist it doesn't find records" do
53
+ expect(client.search_contact(firstname: 'DoesntExist').all.first).to be_nil
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ describe "#company_contact_count" do
61
+ before do
62
+ DataComApiStubRequests.stub_company_contact_count(
63
+ company_id,
64
+ total_count: contacts_count
65
+ )
66
+ end
67
+
68
+ let!(:company_id) { 1 }
69
+ let!(:contacts_count) { 10 }
70
+
71
+ it { expect(client.company_contact_count(company_id).size).to eq contacts_count }
72
+ it { expect(client.company_contact_count(company_id).url).not_to be_blank }
73
+
74
+ it "has some levels" do
75
+ expect(client.company_contact_count(company_id).levels).not_to be_empty
76
+ end
77
+
78
+ it "has some departments" do
79
+ expect(client.company_contact_count(company_id).departments).not_to be_empty
80
+ end
81
+
82
+ it "doesn't have nil levels" do
83
+ client.company_contact_count(company_id).levels.each do |level|
84
+ expect(level).to be_an_instance_of DataComApi::CompanyContactCount::Level
85
+ end
86
+ end
87
+
88
+ it "doesn't have nil departments" do
89
+ client.company_contact_count(company_id).departments.each do |department|
90
+ expect(department).to be_an_instance_of DataComApi::CompanyContactCount::Department
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ describe "#contact" do
97
+ before do
98
+ DataComApiStubRequests.stub_contacts(
99
+ contact_ids,
100
+ username: username,
101
+ password: password,
102
+ purchase_flag: purchase_flag,
103
+ total_hits: total_hits,
104
+ used_points: used_points,
105
+ purchased_contacts: purchased_contacts,
106
+ point_balance: point_balance
107
+ )
108
+ end
109
+
110
+ let!(:contact_ids) { [1, 2] }
111
+ let!(:username) { Faker::Internet.user_name }
112
+ let!(:password) { Faker::Internet.password }
113
+ let!(:purchase_flag) { false }
114
+ let!(:total_hits) { 1 }
115
+ let!(:used_points) { 2000 }
116
+ let!(:purchased_contacts) { 1 }
117
+ let!(:point_balance) { 4818 }
118
+
119
+ it "has used some points" do
120
+ expect(
121
+ client.contacts(contact_ids, username, password, purchase_flag).used_points
122
+ ).to be > 0
123
+ end
124
+
125
+ it "has found some records" do
126
+ expect(
127
+ client.contacts(contact_ids, username, password, purchase_flag).size
128
+ ).to eq total_hits
129
+ end
130
+
131
+ it "has purchased some contacts" do
132
+ expect(
133
+ client.contacts(contact_ids, username, password, purchase_flag).purchased_contacts
134
+ ).to eq purchased_contacts
135
+ end
136
+
137
+ it "reports new point balance" do
138
+ expect(
139
+ client.contacts(contact_ids, username, password, purchase_flag).point_balance
140
+ ).to eq point_balance
141
+ end
142
+
143
+ it "has some contacts" do
144
+ expect(
145
+ client.contacts(
146
+ contact_ids,
147
+ username,
148
+ password,
149
+ purchase_flag
150
+ ).contacts
151
+ ).not_to be_empty
152
+ end
153
+
154
+ it "doesn't have nil contacts" do
155
+ client.contacts(
156
+ contact_ids,
157
+ username,
158
+ password,
159
+ purchase_flag
160
+ ).contacts.each do |contact|
161
+ expect(contact).to be_an_instance_of DataComApi::Contact
162
+ end
163
+ end
164
+
165
+ end
166
+
167
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'data-com-api/query_parameters'
3
+
4
+ describe WebMock do
5
+ let!(:dummy_url) { 'http://dummyurl.com' }
6
+
7
+ before do
8
+ # Be careful, when stubbing, all values must be strings
9
+ stub_request(
10
+ :get,
11
+ dummy_url
12
+ ).with(
13
+ query: hash_including(DataComApi::QueryParameters.stringify_hash_values({
14
+ param1: 5,
15
+ param2: 'random1'
16
+ }))
17
+ ).to_return(
18
+ body: 'body 1'
19
+ )
20
+ end
21
+
22
+ it "receive a request when mocked with query param" do
23
+ expect(
24
+ HTTParty.get(dummy_url, {
25
+ query: {
26
+ param1: 5,
27
+ param2: 'random1',
28
+ param3: 'random3'
29
+ }
30
+ }).body
31
+ ).to eq 'body 1'
32
+ end
33
+
34
+ end
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+ require 'data-com-api/contact'
3
+ require 'data-com-api/responses/base'
4
+ require 'data-com-api/responses/search_contact'
5
+ require 'data-com-api/api_uri'
6
+ require 'data-com-api/client'
7
+
8
+ describe DataComApi::Client do
9
+
10
+ subject(:client) do
11
+ clt = FactoryGirl.build(:client)
12
+ clt.page_size = 2
13
+
14
+ clt
15
+ end
16
+
17
+ it "has a valid factory" do
18
+ expect{client}.not_to raise_error
19
+ end
20
+
21
+ context "env #{ described_class::ENV_NAME_TOKEN } empty" do
22
+ before do
23
+ @env_data_com_token = ENV[described_class::ENV_NAME_TOKEN]
24
+ ENV[described_class::ENV_NAME_TOKEN] = nil
25
+ end
26
+
27
+ after do
28
+ ENV[described_class::ENV_NAME_TOKEN] = @env_data_com_token
29
+ end
30
+
31
+ it ".new raises error when both no token passed and no env DATA_COM_TOKEN set" do
32
+ expect{described_class.new}.to raise_error DataComApi::TokenFailError
33
+ end
34
+
35
+ end
36
+
37
+ describe "#page_size=" do
38
+
39
+ it "accepts values between 1 and 100" do
40
+ expect{client.page_size = Random.rand(100) + 1}.not_to raise_error
41
+ end
42
+
43
+ it "doesn't accept values > 100" do
44
+ expect{client.page_size = 101}.to raise_error DataComApi::ParamError
45
+ end
46
+
47
+ it "doesn't accept values < 1" do
48
+ expect{client.page_size = 0}.to raise_error DataComApi::ParamError
49
+ end
50
+
51
+ end
52
+
53
+ describe "#search_contact" do
54
+
55
+ it "is an instance of SearchContact" do
56
+ DataComApiStubRequests.stub_search_contact(
57
+ page_size: client.page_size,
58
+ total_hits: 6
59
+ )
60
+
61
+ expect(client.search_contact).to be_an_instance_of DataComApi::Responses::SearchContact
62
+ end
63
+
64
+ it "calls search_contact_raw_json on client" do
65
+ DataComApiStubRequests.stub_search_contact(
66
+ page_size: client.page_size,
67
+ total_hits: 6
68
+ )
69
+ expect(client).to receive(:search_contact_raw_json).once.and_call_original
70
+
71
+ client.search_contact.size
72
+ end
73
+
74
+ describe "#all" do
75
+ [0, 2, 4, 10, 11, 15].each do |total_contacts_count|
76
+ context "with #{ total_contacts_count } total_hits" do
77
+ before do
78
+ DataComApiStubRequests.stub_search_contact(
79
+ page_size: client.page_size,
80
+ total_hits: total_contacts_count
81
+ )
82
+ end
83
+
84
+ it "is an array containing only Contact records" do
85
+ search_contact = client.search_contact
86
+
87
+ search_contact.all.each do |contact|
88
+ expect(contact).to be_an_instance_of DataComApi::Contact
89
+ end
90
+ end
91
+
92
+ it "is an array containing all records possible for request" do
93
+ expect(client.search_contact.all.size).to eq total_contacts_count
94
+ end
95
+
96
+ end
97
+ end
98
+
99
+ it "doesn't have more records than max_size" do
100
+ max_offset = 10
101
+ client.page_size = 2
102
+ max_size = max_offset + client.page_size
103
+ client.stub(:max_offset).and_return(max_offset)
104
+
105
+ DataComApiStubRequests.stub_search_contact(
106
+ page_size: client.page_size,
107
+ total_hits: 25
108
+ )
109
+
110
+ expect(client.search_contact.all.size).to eq max_size
111
+ end
112
+
113
+ it "doesn't have more records than max_size when page size is odd" do
114
+ max_offset = 10
115
+ client.page_size = 3
116
+ max_size = max_offset - (max_offset % client.page_size)
117
+ max_size += client.page_size
118
+ client.stub(:max_offset).and_return(max_offset)
119
+
120
+ DataComApiStubRequests.stub_search_contact(
121
+ page_size: client.page_size,
122
+ total_hits: 25
123
+ )
124
+
125
+ expect(client.search_contact.all.size).to eq max_size
126
+ end
127
+
128
+ it "doesn't have records when total_hits is 0" do
129
+ DataComApiStubRequests.stub_search_contact(
130
+ page_size: client.page_size,
131
+ total_hits: 0
132
+ )
133
+
134
+ expect(client.search_contact.all.size).to eq 0
135
+ end
136
+ end
137
+
138
+ describe "#each" do
139
+ before do
140
+ DataComApiStubRequests.stub_search_contact(
141
+ page_size: client.page_size,
142
+ total_hits: total_contacts_count
143
+ )
144
+ end
145
+
146
+ let!(:total_contacts_count) { 10 }
147
+
148
+ it "is an array containing only Contact records" do
149
+ search_contact = client.search_contact
150
+
151
+ search_contact.all.each do |contact|
152
+ expect(contact).to be_an_instance_of DataComApi::Contact
153
+ end
154
+ end
155
+
156
+ it "is an array containing all records possible for request" do
157
+ expect(client.search_contact.all.size).to eq total_contacts_count
158
+ end
159
+
160
+ end
161
+
162
+ end
163
+
164
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'data-com-api/query_parameters'
3
+
4
+ describe DataComApi::QueryParameters do
5
+
6
+ subject(:query_parameters) { FactoryGirl.build(:query_parameters) }
7
+
8
+ it "has a valid factory" do
9
+ expect{query_parameters}.not_to raise_error
10
+ end
11
+
12
+ describe "#updated_since" do
13
+
14
+ it "returns time object" do
15
+ current_time = Time.now.utc.in_time_zone(DataComApi::Client::TIME_ZONE)
16
+ query_parameters.updated_since = current_time.to_s
17
+
18
+ expect(query_parameters.updatedSince).to be_an_instance_of ActiveSupport::TimeWithZone
19
+ end
20
+
21
+ it "returns same time as the one assigned" do
22
+ current_time = Time.now.utc.in_time_zone(DataComApi::Client::TIME_ZONE)
23
+ query_parameters.updated_since = current_time.to_s
24
+
25
+ # XXX: Be careful, comparison between the two ActiveSupport::TimeWithZone
26
+ # fails for unknown reasons to me, even if to_i and to_s are equal.
27
+ # Comparison through strings seems ok by the way because the class
28
+ # will just output data directly to the API, so string based
29
+ expect(query_parameters.updatedSince.to_s).to eq(current_time.to_s)
30
+ end
31
+
32
+ end
33
+
34
+ it "has only string values when converted to_hash" do
35
+ query_parameters.pageSize = 5
36
+
37
+ expect(
38
+ query_parameters.to_hash['pageSize']
39
+ ).to eq query_parameters.pageSize.to_s
40
+ end
41
+
42
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'data-com-api/client'
3
+ require 'data-com-api/contact'
4
+ require 'data-com-api/responses/base'
5
+ require 'data-com-api/responses/search_base'
6
+ require 'data-com-api/responses/search_contact'
7
+
8
+ describe DataComApi::Responses::SearchBase do
9
+
10
+ let!(:client) { FactoryGirl.build(:client) }
11
+ subject(:search_base_response) do
12
+ FactoryGirl.build(:responses_search_contact, client: client)
13
+ end
14
+
15
+ # Params: options, which are params passed to the request which will be
16
+ # converted into query params for url
17
+ describe "#perform_request" do
18
+
19
+ it { expect(search_base_response).to respond_to :perform_request }
20
+ it { expect{search_base_response.send(:perform_request)}.to raise_error ArgumentError }
21
+
22
+ it "has correct totalHits value when size called" do
23
+ total_size = 42
24
+ client.stub(:search_contact_raw_json).and_return({ 'totalHits' => total_size })
25
+
26
+ expect(search_base_response.size).to be total_size
27
+ end
28
+
29
+ end
30
+
31
+ # Params: request, which is the requested data in json format
32
+ describe "#transform_request" do
33
+
34
+ it { expect(search_base_response).to respond_to :transform_request }
35
+ it { expect{search_base_response.send(:transform_request)}.to raise_error ArgumentError }
36
+
37
+ it "is called when we try to access page data" do
38
+ client.stub(:search_contact_raw_json).and_return(
39
+ FactoryGirl.build(:data_com_search_contact_response, totalHits: 10)
40
+ )
41
+ expect(search_base_response).to receive(:transform_request).and_call_original
42
+
43
+ search_base_response.all
44
+ end
45
+
46
+ it "has correct types for all records after transformation" do
47
+ client.stub(:search_contact_raw_json).and_return(
48
+ FactoryGirl.build(:data_com_search_contact_response, totalHits: 10)
49
+ )
50
+
51
+ search_base_response.each do |contact|
52
+ expect(contact).to be_an_instance_of DataComApi::Contact
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ describe "#size" do
59
+
60
+ it "has 32 records for 32 records" do
61
+ client.page_size = 3
62
+ client.stub(:search_contact_raw_json).and_return(
63
+ FactoryGirl.build(:data_com_search_contact_response, totalHits: 32)
64
+ )
65
+
66
+ expect(client.search_contact.size).to be 32
67
+ end
68
+
69
+ it "has 0 records for 0 records" do
70
+ client.page_size = 1
71
+ client.stub(:search_contact_raw_json).and_return(
72
+ FactoryGirl.build(:data_com_search_contact_response, totalHits: 0)
73
+ )
74
+
75
+ expect(client.search_contact.size).to be 0
76
+ end
77
+
78
+ end
79
+
80
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,28 @@
1
- require 'factories'
1
+ require 'pry'
2
+ require 'pathname'
3
+ require 'webmock/rspec'
4
+ require 'factory_girl'
5
+
6
+ WebMock.disable_net_connect!(allow_localhost: true)
7
+
8
+ factories_path = Pathname.new(File.expand_path('..', __FILE__))
9
+ factories_path = factories_path.join('factories', '**', '*.rb')
10
+ Dir[factories_path].each { |f| require f }
11
+
12
+ support_path = Pathname.new(File.expand_path('..', __FILE__))
13
+ support_path = support_path.join('support', '**', '*.rb')
14
+ Dir[support_path].each { |f| require f }
2
15
 
3
16
  RSpec.configure do |config|
17
+ # Fix for faker deprecation message
18
+ I18n.enforce_available_locales = true
4
19
  config.treat_symbols_as_metadata_keys_with_true_values = true
5
20
  config.run_all_when_everything_filtered = true
6
- config.filter_run :focus
7
21
  config.order = 'random'
22
+ config.filter_run :focus
23
+ config.filter_run_excluding broken: true
24
+
25
+ config.expect_with :rspec do |c|
26
+ c.syntax = :expect
27
+ end
8
28
  end