Class: DataComApi::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/data-com-api/client.rb

Constant Summary collapse

ENV_NAME_TOKEN =
'DATA_COM_TOKEN'.freeze
TIME_ZONE =
'Pacific Time (US & Canada)'.freeze
BASE_OFFSET =
0
BASE_PAGE_SIZE =
50
SIZE_ONLY_PAGE_SIZE =
0
MIN_PAGE_SIZE =

We start at 1, 0 is a special case

1
MAX_PAGE_SIZE =
500
MAX_OFFSET =
100_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token = nil) ⇒ Client

Returns a new instance of Client.

Raises:



37
38
39
40
41
42
43
# File 'lib/data-com-api/client.rb', line 37

def initialize(api_token=nil)
  @token           = api_token || ENV[ENV_NAME_TOKEN]
  @page_size       = BASE_PAGE_SIZE
  @api_calls_count = 0

  raise TokenFailError, 'No token set!' unless @token
end

Instance Attribute Details

#api_calls_countObject (readonly)

Returns the value of attribute api_calls_count.



26
27
28
# File 'lib/data-com-api/client.rb', line 26

def api_calls_count
  @api_calls_count
end

#tokenObject (readonly)

Returns the value of attribute token.



27
28
29
# File 'lib/data-com-api/client.rb', line 27

def token
  @token
end

Instance Method Details

#company_contact_count(company_id, options = {}) ⇒ Object



71
72
73
# File 'lib/data-com-api/client.rb', line 71

def company_contact_count(company_id, options={})
  Responses::CompanyContactCount.new(self, company_id, options)
end

#company_contact_count_raw(company_id, include_graveyard) ⇒ Object

Raw calls



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/data-com-api/client.rb', line 87

def company_contact_count_raw(company_id, include_graveyard)
  params = QueryParameters.new(
    token:             token,
    company_id:        company_id,
    include_graveyard: include_graveyard
  )

  response = self.class.get(
    ApiURI.company_contact_count(company_id),
    { query: params }
  )
  increase_api_calls_count!

  response.body
end

#company_contact_count_raw_json(company_id, include_graveyard) ⇒ Object

JSON calls



190
191
192
# File 'lib/data-com-api/client.rb', line 190

def company_contact_count_raw_json(company_id, include_graveyard)
  json_or_raise company_contact_count_raw(company_id, include_graveyard)
end

#contacts(contact_ids, username, password, purchase_flag = false) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/data-com-api/client.rb', line 75

def contacts(contact_ids, username, password, purchase_flag=false)
  Responses::Contacts.new(
    self,
    contact_ids,
    username,
    password,
    purchase_flag
  )
end

#contacts_raw(contact_ids, username, password, purchase_flag = false) ⇒ Object

Raises:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/data-com-api/client.rb', line 123

def contacts_raw(contact_ids, username, password, purchase_flag=false)
  raise ParamError, 'One contact required at least' unless contact_ids.size > 0

  params = QueryParameters.new(
    token:         token,
    username:      username,
    password:      password,
    purchase_flag: purchase_flag
  )
  
  response = self.class.get(
    ApiURI.contacts(contact_ids),
    { query: params }
  )
  increase_api_calls_count!

  response.body
end

#contacts_raw_json(contact_ids, username, password, purchase_flag = false) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/data-com-api/client.rb', line 202

def contacts_raw_json(contact_ids, username, password, purchase_flag=false)
  json_or_raise contacts_raw(
    contact_ids,
    username,
    password,
    purchase_flag
  )
end

#max_offsetObject



29
30
31
# File 'lib/data-com-api/client.rb', line 29

def max_offset
  MAX_OFFSET
end

#page_sizeObject



45
46
47
# File 'lib/data-com-api/client.rb', line 45

def page_size
  @page_size
end

#page_size=(value) ⇒ Object

Page size = 0 returns objects count only (small request)



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/data-com-api/client.rb', line 50

def page_size=(value)
  real_value = value.to_i

  if real_value < MIN_PAGE_SIZE || real_value > MAX_PAGE_SIZE
    raise ParamError, <<-eos
      page_size must be between #{ MIN_PAGE_SIZE } and #{ MAX_PAGE_SIZE },
      received #{ real_value }"
    eos
  end

  @page_size = real_value
end

#partner_contacts_raw(contact_ids, end_org_id, end_user_id) ⇒ Object

Raises:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/data-com-api/client.rb', line 142

def partner_contacts_raw(contact_ids, end_org_id, end_user_id)
  raise ParamError, 'One contact required at least' unless contact_ids.size > 0

  params = QueryParameters.new(
    token:       token,
    end_org_id:  end_org_id,
    end_user_id: end_user_id
  )
  
  response = self.class.get(
    ApiURI.partner_contacts(contact_ids),
    { query: params }
  )
  increase_api_calls_count!

  response.body
end

#partner_contacts_raw_json(contact_ids, end_org_id, end_user_id) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/data-com-api/client.rb', line 211

def partner_contacts_raw_json(contact_ids, end_org_id, end_user_id)
  json_or_raise partner_contacts_raw(
    contact_ids,
    end_org_id,
    end_user_id
  )
end

#partner_rawObject



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/data-com-api/client.rb', line 160

def partner_raw
  params = QueryParameters.new(token: token)
  
  response = self.class.get(
    ApiURI.partner,
    { query: params }
  )
  increase_api_calls_count!

  response.body
end

#partner_raw_jsonObject



219
220
221
# File 'lib/data-com-api/client.rb', line 219

def partner_raw_json
  json_or_raise partner_raw
end

#search_company(options = {}) ⇒ Object



67
68
69
# File 'lib/data-com-api/client.rb', line 67

def search_company(options={})
  Responses::SearchCompany.new(self, options)
end

#search_company_raw(options = {}) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/data-com-api/client.rb', line 113

def search_company_raw(options={})
  response = self.class.get(
    ApiURI.search_company,
    generate_params(options)
  )
  increase_api_calls_count!

  response.body
end

#search_company_raw_json(options = {}) ⇒ Object



198
199
200
# File 'lib/data-com-api/client.rb', line 198

def search_company_raw_json(options={})
  json_or_raise search_company_raw(options)
end

#search_contact(options = {}) ⇒ Object



63
64
65
# File 'lib/data-com-api/client.rb', line 63

def search_contact(options={})
  Responses::SearchContact.new(self, options)
end

#search_contact_raw(options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/data-com-api/client.rb', line 103

def search_contact_raw(options={})
  response = self.class.get(
    ApiURI.search_contact,
    generate_params(options)
  )
  increase_api_calls_count!

  response.body
end

#search_contact_raw_json(options = {}) ⇒ Object



194
195
196
# File 'lib/data-com-api/client.rb', line 194

def search_contact_raw_json(options={})
  json_or_raise search_contact_raw(options)
end

#size_only_page_sizeObject



33
34
35
# File 'lib/data-com-api/client.rb', line 33

def size_only_page_size
  SIZE_ONLY_PAGE_SIZE
end

#user_raw(username, password) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/data-com-api/client.rb', line 172

def user_raw(username, password)
  params = QueryParameters.new(
    username: username,
    password: password,
    token:    token
  )
  
  response = self.class.get(
    ApiURI.user,
    { query: params }
  )
  increase_api_calls_count!

  response.body
end

#user_raw_json(username, password) ⇒ Object



223
224
225
# File 'lib/data-com-api/client.rb', line 223

def user_raw_json(username, password)
  json_or_raise user_raw(username, password)
end