contacts_client 0.0.28 → 0.0.29

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +8 -8
  2. data/app/models/padma_contact.rb +80 -17
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDAyNzU4ZGEyYjczMGY4NWMxYjQwYTJmNjJjYzliNWY1ZDNiMWQ2YQ==
4
+ MjM4YmI4ZGZjMTAyNWFjMGVhN2Q1ZjEyM2Y3NzFmNzc0Y2FmZmM1Mg==
5
5
  data.tar.gz: !binary |-
6
- ZTRiMmYzNDc5MzVhYzVmZjkyMGQwMjVmNjMxOTJjM2MzZTk0ODY2MQ==
6
+ NDk5MGQ5MGZmYWQ3NTFjMzZiYzdlMzViMDAzOGE4ZjU3NjlhMDU5MA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NWI2ZGUzZWNjYjViYTY1YmMyMzZhNmZhMTI4NzA3ZDU4ZTdhZWVmODIwZjUx
10
- ZGM5YjA0Yzc4OTMyOTI2MzVjNTI0NWRkNTBlZDA2MzAwNWZhN2QyNDEzNDMz
11
- MTJiODI2YmUxZDA2NzYxMmEzNGE0OTdmMGM3MWQ2NTkwNWJkMWM=
9
+ YWM5ZjlmZmM4NGU1ZmNlYmVlODA4NTk3MTZkZWQxMGRmMTQ1ZTdiYjhlZjY5
10
+ MWNmOGQ1MWRiMGU3NTlhNGEzYjA4ODBjNzVhZTNlZjY4ZWQzOGNmMmMyNDY4
11
+ MTRmODBiN2EyNWRkYzQ0NjA2YmFiNmVkOGJiYzA5ZmNhNDJiMWY=
12
12
  data.tar.gz: !binary |-
13
- N2VjOGM3ODk2YTJlODY1NTUwYmM0ZTY3ZDNiYmE2Zjg0NzE4Y2IyNWEwNTk5
14
- NzNmMDk3NzkwMGZiYWRjOWE4YTdkNzA0YTFiYmNlMWRlNjA3Y2U5ZmYwNmY3
15
- YzY3ODJlZTI4ZWQxMzBiZjA3ZDk0YmE3M2E5ODJkZDYzNjU5Nzk=
13
+ OTM2YWI5YTQ5MzA2ZTc0ZTJhNGQzNzAwNjMyMjMzZjQzZDVkY2VjNjQwZjQz
14
+ MThkZDNkN2ZhY2ZhMTI5MTg5NzZlNGZjOTQ2NDNiOWIyNWRiMzY1YWM3NWY2
15
+ MzJkMDhkMjlmY2M5MDY0YzY2ZDliNjRiMmQxYmQ4MDA3ZWFkYmI=
@@ -208,29 +208,92 @@ class PadmaContact < LogicalModel
208
208
  return nil
209
209
  end
210
210
 
211
+ # @see count
212
+ # Same as count by will make a POST request instead of GET request
213
+ def self.count_by_post(options)
214
+ options[:page] = 1
215
+ options[:per_page] = 1
216
+
217
+ options = self.merge_key(options)
218
+
219
+ response = Typhoeus.post(self.resource_uri+'/search', body: options)
220
+ if response.success?
221
+ log_ok(response)
222
+ result_set = self.from_json(response.body)
223
+ return result_set[:total]
224
+ else
225
+ log_failed(response)
226
+ return nil
227
+ end
228
+ end
229
+
211
230
  DEFAULT_BATCH_SIZE = 500
212
- MAX_FAILS = 5
213
231
  ##
214
- #
215
- # @return [Array] contacts
216
- # @return nil if connection failed
232
+ #
233
+ # It's slower than .search BUT if contacts-ws is timing out
234
+ # this will work because requests are smaller.
235
+ #
236
+ # @param search_options [Hash]
237
+ # @param batch_options [Hash]
238
+ #
239
+ # @option search_options any option valid for .search
240
+ # @option batch_options [Intger] batch_size - Size of batches
241
+ # @option batch_options [Integer] total - Total amount of contacts.
242
+ # To avoid making an extra call for counting
217
243
  def self.batch_search(search_options = {}, batch_options = {})
218
244
  batch_options[:batch_size] ||= DEFAULT_BATCH_SIZE
219
- ret = [];
220
-
221
- page_elements = nil; page = 1; fails = 0;
222
- until page_elements == [] || fails >= MAX_FAILS do
223
- page_elements = PadmaContact.search(search_options.merge(per_page: batch_options[:batch_size], page: page))
224
- if page_elements
225
- ret += page_elements
226
- page += 1
227
- else
228
- fails += 1
245
+ ret = nil
246
+
247
+ total = batch_options[:total] || self.count_by_post(search_options)
248
+ if total
249
+ ret = []
250
+ (total.to_f/batch_options[:batch_size]).ceil.times do |i|
251
+ self.async_search(search_options.merge(per_page: batch_options[:batch_size], page: i+1)) do |page_elements|
252
+ if page_elements
253
+ ret += page_elements
254
+ end
255
+ end
229
256
  end
230
- fails = 0
231
- end
257
+ end
258
+ self.hydra.run
259
+ ret.sort!{|a,b| a.first_name <=> b.first_name} if ret
260
+ return ret
261
+ end
232
262
 
233
- return (fails >= MAX_FAILS)? nil : ret
263
+ def self.async_search(options={})
264
+ options[:page] ||= 1
265
+ options[:per_page] ||= 9999
266
+
267
+ options = self.merge_key(options)
268
+
269
+ request = Typhoeus::Request.new(
270
+ resource_uri('search'),
271
+ method: :post,
272
+ body: options,
273
+ headers: default_headers
274
+ )
275
+ request.on_complete do |response|
276
+ if response.code >= 200 && response.code < 400
277
+ log_ok(response)
278
+
279
+ result_set = self.from_json(response.body)
280
+
281
+ # this paginate is will_paginate's Array pagination
282
+ collection = Kaminari.paginate_array(
283
+ result_set[:collection],
284
+ {
285
+ :total_count=>result_set[:total],
286
+ :limit => options[:per_page],
287
+ :offset => options[:per_page] * ([options[:page], 1].max - 1)
288
+ }
289
+ )
290
+
291
+ yield collection
292
+ else
293
+ log_failed(response)
294
+ end
295
+ end
296
+ self.hydra.queue(request)
234
297
  end
235
298
 
236
299
  ##
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contacts_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.28
4
+ version: 0.0.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dwayne Macgowan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-07 00:00:00.000000000 Z
11
+ date: 2014-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties