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.
- checksums.yaml +8 -8
- data/README.md +81 -3
- data/data-com-api.gemspec +3 -1
- data/lib/data-com-api/api_uri.rb +40 -0
- data/lib/data-com-api/client.rb +247 -0
- data/lib/data-com-api/company.rb +17 -0
- data/lib/data-com-api/company_contact_count/department.rb +17 -0
- data/lib/data-com-api/company_contact_count/level.rb +17 -0
- data/lib/data-com-api/contact.rb +32 -0
- data/lib/data-com-api/errors.rb +96 -0
- data/lib/data-com-api/query_parameters.rb +66 -0
- data/lib/data-com-api/responses/base.rb +19 -0
- data/lib/data-com-api/responses/company_contact_count.rb +96 -0
- data/lib/data-com-api/responses/contacts.rb +97 -0
- data/lib/data-com-api/responses/search_base.rb +112 -0
- data/lib/data-com-api/responses/search_company.rb +23 -0
- data/lib/data-com-api/responses/search_contact.rb +23 -0
- data/lib/data-com-api/version.rb +1 -1
- data/lib/data-com-api.rb +14 -1
- data/spec/factories/client_factory.rb +12 -0
- data/spec/factories/data-com/company_contact_count/department_factory.rb +14 -0
- data/spec/factories/data-com/company_contact_count/level_factory.rb +14 -0
- data/spec/factories/data-com/company_contact_count_response_factory.rb +39 -0
- data/spec/factories/data-com/contact_factory.rb +35 -0
- data/spec/factories/data-com/contacts_response_factory.rb +30 -0
- data/spec/factories/data-com/search_contact_response_factory.rb +30 -0
- data/spec/factories/query_parameters_factory.rb +11 -0
- data/spec/factories/responses/search_contact_factory.rb +13 -0
- data/spec/integration/client_spec.rb +167 -0
- data/spec/integration/webmock_spec.rb +34 -0
- data/spec/models/client_spec.rb +164 -0
- data/spec/models/query_parameters_spec.rb +42 -0
- data/spec/models/search_base_spec.rb +80 -0
- data/spec/spec_helper.rb +22 -2
- data/spec/support/data_com_api_stub_requests.rb +229 -0
- metadata +75 -8
- data/spec/factories.rb +0 -4
- data/spec/requests/client_spec.rb +0 -6
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'data-com-api/responses/base'
|
2
|
+
require 'data-com-api/company_contact_count/level'
|
3
|
+
require 'data-com-api/company_contact_count/department'
|
4
|
+
|
5
|
+
module DataComApi
|
6
|
+
module Responses
|
7
|
+
class CompanyContactCount < Base
|
8
|
+
|
9
|
+
attr_reader :id
|
10
|
+
|
11
|
+
def initialize(api_client, company_id, received_options)
|
12
|
+
@options = received_options
|
13
|
+
@id = company_id
|
14
|
+
@levels = []
|
15
|
+
@departments = []
|
16
|
+
@url = nil
|
17
|
+
@size = nil
|
18
|
+
@requested = false
|
19
|
+
super(api_client)
|
20
|
+
end
|
21
|
+
|
22
|
+
def size
|
23
|
+
return @size if requested?
|
24
|
+
|
25
|
+
perform_request_if_not_requested!
|
26
|
+
@size
|
27
|
+
end
|
28
|
+
|
29
|
+
def url
|
30
|
+
return @url if requested?
|
31
|
+
|
32
|
+
perform_request_if_not_requested!
|
33
|
+
@url
|
34
|
+
end
|
35
|
+
|
36
|
+
def levels
|
37
|
+
return @levels if requested?
|
38
|
+
|
39
|
+
perform_request_if_not_requested!
|
40
|
+
@levels
|
41
|
+
end
|
42
|
+
|
43
|
+
def departments
|
44
|
+
return @departments if requested?
|
45
|
+
|
46
|
+
perform_request_if_not_requested!
|
47
|
+
@departments
|
48
|
+
end
|
49
|
+
|
50
|
+
protected
|
51
|
+
|
52
|
+
def options
|
53
|
+
@options
|
54
|
+
end
|
55
|
+
|
56
|
+
def transform_request(request)
|
57
|
+
@size = request['totalCount'].to_i
|
58
|
+
@url = request['url'] if request.include? 'url'
|
59
|
+
|
60
|
+
if request.include? 'levels'
|
61
|
+
request['levels'].each do |level|
|
62
|
+
@levels << DataComApi::CompanyContactCount::Level.new(level)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
if request.include? 'departments'
|
67
|
+
request['departments'].each do |department|
|
68
|
+
@departments << DataComApi::CompanyContactCount::Department.new(
|
69
|
+
department
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def perform_request(received_options)
|
76
|
+
include_graveyard = !!received_options[:includeGraveyard]
|
77
|
+
client.company_contact_count_raw_json(id, include_graveyard)
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def perform_request_if_not_requested!
|
83
|
+
return if requested?
|
84
|
+
|
85
|
+
transform_request perform_request(options)
|
86
|
+
|
87
|
+
@requested = true
|
88
|
+
end
|
89
|
+
|
90
|
+
def requested?
|
91
|
+
@requested
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'data-com-api/responses/base'
|
2
|
+
require 'data-com-api/contact'
|
3
|
+
|
4
|
+
module DataComApi
|
5
|
+
module Responses
|
6
|
+
class Contacts < Base
|
7
|
+
|
8
|
+
def initialize(api_client, contact_ids, username, password, purchase_flag)
|
9
|
+
@contact_ids = contact_ids
|
10
|
+
@username = username
|
11
|
+
@password = password
|
12
|
+
@purchase_flag = purchase_flag
|
13
|
+
|
14
|
+
@used_points = nil
|
15
|
+
@size = nil
|
16
|
+
@purchased_contacts = nil
|
17
|
+
@point_balance = nil
|
18
|
+
@contacts = []
|
19
|
+
super(api_client)
|
20
|
+
end
|
21
|
+
|
22
|
+
def size
|
23
|
+
return @size if requested?
|
24
|
+
|
25
|
+
perform_request_if_not_requested!
|
26
|
+
@size
|
27
|
+
end
|
28
|
+
|
29
|
+
def used_points
|
30
|
+
return @used_points if requested?
|
31
|
+
|
32
|
+
perform_request_if_not_requested!
|
33
|
+
@used_points
|
34
|
+
end
|
35
|
+
|
36
|
+
def purchased_contacts
|
37
|
+
return @purchased_contacts if requested?
|
38
|
+
|
39
|
+
perform_request_if_not_requested!
|
40
|
+
@purchased_contacts
|
41
|
+
end
|
42
|
+
|
43
|
+
def point_balance
|
44
|
+
return @point_balance if requested?
|
45
|
+
|
46
|
+
perform_request_if_not_requested!
|
47
|
+
@point_balance
|
48
|
+
end
|
49
|
+
|
50
|
+
def contacts
|
51
|
+
return @contacts if requested?
|
52
|
+
|
53
|
+
perform_request_if_not_requested!
|
54
|
+
@contacts
|
55
|
+
end
|
56
|
+
|
57
|
+
protected
|
58
|
+
|
59
|
+
def transform_request(request)
|
60
|
+
@used_points = request['pointsUsed'].to_i
|
61
|
+
@size = request['totalHits'].to_i
|
62
|
+
@purchased_contacts = request['numberOfContactsPurchased'].to_i
|
63
|
+
@point_balance = request['pointBalance'].to_i
|
64
|
+
|
65
|
+
if request.include? 'contacts'
|
66
|
+
request['contacts'].each do |contact|
|
67
|
+
@contacts << DataComApi::Contact.new(contact)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def perform_request
|
73
|
+
client.contacts_raw_json(
|
74
|
+
@contact_ids,
|
75
|
+
@username,
|
76
|
+
@password,
|
77
|
+
@purchase_flag
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def perform_request_if_not_requested!
|
84
|
+
return if requested?
|
85
|
+
|
86
|
+
transform_request perform_request
|
87
|
+
|
88
|
+
@requested = true
|
89
|
+
end
|
90
|
+
|
91
|
+
def requested?
|
92
|
+
@requested
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'data-com-api/responses/base'
|
2
|
+
|
3
|
+
module DataComApi
|
4
|
+
module Responses
|
5
|
+
# Abstract class
|
6
|
+
class SearchBase < Base
|
7
|
+
|
8
|
+
|
9
|
+
def initialize(api_client, received_options)
|
10
|
+
@options = received_options
|
11
|
+
super(api_client)
|
12
|
+
|
13
|
+
# Cache pagesize, MUST NOT change between requests
|
14
|
+
@page_size = client.page_size
|
15
|
+
end
|
16
|
+
|
17
|
+
def size
|
18
|
+
return @size if @size
|
19
|
+
|
20
|
+
size_options = options.merge(
|
21
|
+
offset: 0,
|
22
|
+
page_size: client.size_only_page_size
|
23
|
+
)
|
24
|
+
|
25
|
+
calculate_size_from_request! self.perform_request(size_options)
|
26
|
+
end
|
27
|
+
|
28
|
+
def max_size
|
29
|
+
self.real_max_offset + page_size
|
30
|
+
end
|
31
|
+
|
32
|
+
def real_size
|
33
|
+
self.size > self.real_max_offset ? self.max_size : self.size
|
34
|
+
end
|
35
|
+
|
36
|
+
def at_offset(offset)
|
37
|
+
page_options = options.merge(
|
38
|
+
offset: offset,
|
39
|
+
page_size: page_size
|
40
|
+
)
|
41
|
+
|
42
|
+
request = self.perform_request(page_options)
|
43
|
+
calculate_size_from_request! request
|
44
|
+
|
45
|
+
self.transform_request request
|
46
|
+
end
|
47
|
+
|
48
|
+
# Be careful, this will load all records in memory, check total_records
|
49
|
+
# before doing such a thing
|
50
|
+
def all
|
51
|
+
all_records = Array.new(self.real_size)
|
52
|
+
|
53
|
+
self.each_with_index { |record, index| all_records[index] = record }
|
54
|
+
|
55
|
+
all_records
|
56
|
+
end
|
57
|
+
|
58
|
+
def each_with_index
|
59
|
+
total_records = 0
|
60
|
+
records_per_previous_page = page_size
|
61
|
+
current_offset = 0
|
62
|
+
|
63
|
+
loop do
|
64
|
+
break if current_offset > self.real_max_offset
|
65
|
+
|
66
|
+
records = at_offset(current_offset)
|
67
|
+
|
68
|
+
records.each_with_index do |record, index|
|
69
|
+
yield(record, index + current_offset)
|
70
|
+
end
|
71
|
+
|
72
|
+
records_per_previous_page = records.size
|
73
|
+
current_offset += page_size
|
74
|
+
total_records += records_per_previous_page
|
75
|
+
|
76
|
+
if records_per_previous_page != page_size || total_records == self.size
|
77
|
+
break
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def real_max_offset
|
83
|
+
return @real_max_offset if @real_max_offset
|
84
|
+
|
85
|
+
@real_max_offset = client.max_offset
|
86
|
+
@real_max_offset = @real_max_offset - (@real_max_offset % page_size)
|
87
|
+
end
|
88
|
+
|
89
|
+
alias_method :to_a, :all
|
90
|
+
alias_method :each, :each_with_index
|
91
|
+
|
92
|
+
protected
|
93
|
+
|
94
|
+
def page_size
|
95
|
+
@page_size
|
96
|
+
end
|
97
|
+
|
98
|
+
def options
|
99
|
+
@options
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def calculate_size_from_request!(request)
|
105
|
+
return if @size
|
106
|
+
|
107
|
+
@size = request['totalHits'].to_i
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'data-com-api/company'
|
2
|
+
require 'data-com-api/responses/search_base'
|
3
|
+
require 'data-com-api/responses/base'
|
4
|
+
|
5
|
+
module DataComApi
|
6
|
+
module Responses
|
7
|
+
class SearchCompany < SearchBase
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def transform_request(request)
|
12
|
+
request['companies'].map do |contact_attributes|
|
13
|
+
DataComApi::Company.new(contact_attributes)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def perform_request(received_options)
|
18
|
+
client.search_company_raw_json(received_options)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'data-com-api/contact'
|
2
|
+
require 'data-com-api/responses/search_base'
|
3
|
+
require 'data-com-api/responses/base'
|
4
|
+
|
5
|
+
module DataComApi
|
6
|
+
module Responses
|
7
|
+
class SearchContact < SearchBase
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def transform_request(request)
|
12
|
+
request['contacts'].map do |contact_attributes|
|
13
|
+
DataComApi::Contact.new(contact_attributes)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def perform_request(received_options)
|
18
|
+
client.search_contact_raw_json(received_options)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/data-com-api/version.rb
CHANGED
data/lib/data-com-api.rb
CHANGED
@@ -1,4 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require 'data-com-api/version'
|
2
|
+
require 'data-com-api/errors'
|
3
|
+
require 'data-com-api/query_parameters'
|
4
|
+
require 'data-com-api/company'
|
5
|
+
require 'data-com-api/contact'
|
6
|
+
require 'data-com-api/company_contact_count/level'
|
7
|
+
require 'data-com-api/company_contact_count/department'
|
8
|
+
require 'data-com-api/responses/base'
|
9
|
+
require 'data-com-api/responses/search_base'
|
10
|
+
require 'data-com-api/responses/search_contact'
|
11
|
+
require 'data-com-api/responses/search_company'
|
12
|
+
require 'data-com-api/responses/company_contact_count'
|
13
|
+
require 'data-com-api/responses/contacts'
|
14
|
+
require 'data-com-api/client'
|
2
15
|
|
3
16
|
module DataComApi
|
4
17
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'faker'
|
3
|
+
|
4
|
+
FactoryGirl.define do
|
5
|
+
|
6
|
+
factory :data_com_company_contact_count_department, class: Hashie::Mash do
|
7
|
+
count { Faker::Number.number(2).to_i }
|
8
|
+
name { Faker::Lorem.word }
|
9
|
+
url { Faker::Internet.url }
|
10
|
+
|
11
|
+
initialize_with { new(attributes) }
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'faker'
|
3
|
+
|
4
|
+
FactoryGirl.define do
|
5
|
+
|
6
|
+
factory :data_com_company_contact_count_level, class: Hashie::Mash do
|
7
|
+
count { Faker::Number.number(2).to_i }
|
8
|
+
name { Faker::Lorem.word }
|
9
|
+
url { Faker::Internet.url }
|
10
|
+
|
11
|
+
initialize_with { new(attributes) }
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'faker'
|
3
|
+
|
4
|
+
FactoryGirl.define do
|
5
|
+
|
6
|
+
# Notice that in factory I didn't care about total contacts in levels +
|
7
|
+
# departments = totalCount
|
8
|
+
factory :data_com_company_contact_count_response, class: Hashie::Mash do
|
9
|
+
id { Faker::Number.number(6).to_i }
|
10
|
+
totalCount { Faker::Number.number(2).to_i }
|
11
|
+
url { Faker::Internet.url }
|
12
|
+
levels nil
|
13
|
+
departments nil
|
14
|
+
|
15
|
+
ignore do
|
16
|
+
levels_size 5
|
17
|
+
departments_size 9
|
18
|
+
end
|
19
|
+
|
20
|
+
initialize_with { new(attributes) }
|
21
|
+
|
22
|
+
after(:build) do |data_com_company_contact_count_response, evaluator|
|
23
|
+
unless data_com_company_contact_count_response[:levels]
|
24
|
+
data_com_company_contact_count_response[:levels] = FactoryGirl.build_list(
|
25
|
+
:data_com_company_contact_count_level,
|
26
|
+
evaluator.levels_size
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
unless data_com_company_contact_count_response[:departments]
|
31
|
+
data_com_company_contact_count_response[:departments] = FactoryGirl.build_list(
|
32
|
+
:data_com_company_contact_count_department,
|
33
|
+
evaluator.departments_size
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'faker'
|
3
|
+
require 'active_support/time'
|
4
|
+
require 'active_support/hash_with_indifferent_access'
|
5
|
+
require 'data-com-api/client'
|
6
|
+
|
7
|
+
FactoryGirl.define do
|
8
|
+
|
9
|
+
factory :data_com_contact, class: Hashie::Mash do
|
10
|
+
zip { Faker::Address.zip.to_s }
|
11
|
+
phone { Faker::PhoneNumber.phone_number }
|
12
|
+
areaCode { Faker::Number.number(3) }
|
13
|
+
updatedDate { Time.now.utc.in_time_zone(DataComApi::Client::TIME_ZONE) }
|
14
|
+
seoContactURL { Faker::Internet.url }
|
15
|
+
state { Faker::Address.state_abbr }
|
16
|
+
lastname { Faker::Name.last_name }
|
17
|
+
firstname { Faker::Name.first_name }
|
18
|
+
companyName { Faker::Company.name }
|
19
|
+
contactURL { Faker::Internet.url }
|
20
|
+
contactSales { Faker::Number.number(2).to_i }
|
21
|
+
country { Faker::Address.country }
|
22
|
+
owned false
|
23
|
+
city { Faker::Address.city }
|
24
|
+
title { Faker::Lorem.sentence }
|
25
|
+
contactId { Faker::Number.number(6).to_i }
|
26
|
+
email { Faker::Internet.email }
|
27
|
+
address { Faker::Address.street_address }
|
28
|
+
graveyardStatus false
|
29
|
+
ownedType ""
|
30
|
+
companyId { Faker::Number.number(4) }
|
31
|
+
|
32
|
+
initialize_with { new(attributes) }
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'faker'
|
3
|
+
|
4
|
+
FactoryGirl.define do
|
5
|
+
|
6
|
+
factory :data_com_contacts_response, class: Hashie::Mash do
|
7
|
+
unrecognized { Hashie::Mash.new('contactId' => []) }
|
8
|
+
pointsUsed { Faker::Number.number(2).to_i }
|
9
|
+
totalHits { Faker::Number.number(2).to_i }
|
10
|
+
numberOfContactsPurchased { Faker::Number.number(2).to_i }
|
11
|
+
pointBalance { Faker::Number.number(4).to_i }
|
12
|
+
contacts nil
|
13
|
+
|
14
|
+
ignore do
|
15
|
+
contacts_size 2
|
16
|
+
end
|
17
|
+
|
18
|
+
initialize_with { new(attributes) }
|
19
|
+
|
20
|
+
after(:build) do |data_com_contacts_response, evaluator|
|
21
|
+
unless data_com_contacts_response[:contacts]
|
22
|
+
data_com_contacts_response[:contacts] = FactoryGirl.build_list(
|
23
|
+
:data_com_contact,
|
24
|
+
evaluator.contacts_size
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'faker'
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
4
|
+
require 'data-com-api/client'
|
5
|
+
|
6
|
+
FactoryGirl.define do
|
7
|
+
|
8
|
+
factory :data_com_search_contact_response, class: Hashie::Mash do
|
9
|
+
totalHits 0
|
10
|
+
contacts nil
|
11
|
+
|
12
|
+
ignore do
|
13
|
+
page_size 3
|
14
|
+
end
|
15
|
+
|
16
|
+
initialize_with { new(attributes) }
|
17
|
+
|
18
|
+
after(:build) do |data_com_search_contact_response, evaluator|
|
19
|
+
if evaluator.page_size > 0 && data_com_search_contact_response[:contacts].nil?
|
20
|
+
data_com_search_contact_response[:contacts] = FactoryGirl.build_list(
|
21
|
+
:data_com_contact,
|
22
|
+
evaluator.page_size
|
23
|
+
)
|
24
|
+
elsif evaluator.page_size == 0 && data_com_search_contact_response[:contacts].nil?
|
25
|
+
data_com_search_contact_response[:contacts] = []
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'data-com-api/responses/search_contact'
|
2
|
+
|
3
|
+
FactoryGirl.define do
|
4
|
+
|
5
|
+
factory :responses_search_contact, class: DataComApi::Responses::SearchContact do
|
6
|
+
ignore do
|
7
|
+
client { FactoryGirl.build(:client) }
|
8
|
+
end
|
9
|
+
|
10
|
+
initialize_with { new(client, attributes) }
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|