algoliasearch 1.18.5 → 1.19.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/algolia/index.rb CHANGED
@@ -14,17 +14,21 @@ module Algolia
14
14
  # Delete an index
15
15
  #
16
16
  # return an hash of the form { "deletedAt" => "2013-01-18T15:33:13.556Z", "taskID" => "42" }
17
- def delete
18
- client.delete(Protocol.index_uri(name))
17
+ #
18
+ # @param request_options contains extra parameters to send with your query
19
+ def delete(request_options = {})
20
+ client.delete(Protocol.index_uri(name), :write, request_options)
19
21
  end
20
22
  alias_method :delete_index, :delete
21
23
 
22
24
  # Delete an index and wait until the deletion has been processed
23
25
  #
24
26
  # return an hash of the form { "deletedAt" => "2013-01-18T15:33:13.556Z", "taskID" => "42" }
25
- def delete!
26
- res = delete
27
- wait_task(res['taskID'])
27
+ #
28
+ # @param request_options contains extra parameters to send with your query
29
+ def delete!(request_options = {})
30
+ res = delete(request_options)
31
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
28
32
  res
29
33
  end
30
34
  alias_method :delete_index!, :delete!
@@ -35,12 +39,13 @@ module Algolia
35
39
  # The object is represented by an associative array
36
40
  # @param objectID (optional) an objectID you want to attribute to this object
37
41
  # (if the attribute already exist the old object will be overridden)
38
- def add_object(obj, objectID = nil)
42
+ # @param request_options contains extra parameters to send with your query
43
+ def add_object(obj, objectID = nil, request_options = {})
39
44
  check_object obj
40
45
  if objectID.nil? || objectID.to_s.empty?
41
- client.post(Protocol.index_uri(name), obj.to_json)
46
+ client.post(Protocol.index_uri(name), obj.to_json, :write, request_options)
42
47
  else
43
- client.put(Protocol.object_uri(name, objectID), obj.to_json)
48
+ client.put(Protocol.object_uri(name, objectID), obj.to_json, :write, request_options)
44
49
  end
45
50
  end
46
51
 
@@ -50,28 +55,31 @@ module Algolia
50
55
  # The object is represented by an associative array
51
56
  # @param objectID (optional) an objectID you want to attribute to this object
52
57
  # (if the attribute already exist the old object will be overridden)
53
- def add_object!(obj, objectID = nil)
54
- res = add_object(obj, objectID)
55
- wait_task(res["taskID"])
56
- return res
58
+ # @param Request options object. Contains extra URL parameters or headers
59
+ def add_object!(obj, objectID = nil, request_options = {})
60
+ res = add_object(obj, objectID, request_options)
61
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
62
+ res
57
63
  end
58
64
 
59
65
  # Add several objects in this index
60
66
  #
61
67
  # @param objs the array of objects to add inside the index.
62
68
  # Each object is represented by an associative array
63
- def add_objects(objs)
64
- batch build_batch('addObject', objs, false)
69
+ # @param request_options contains extra parameters to send with your query
70
+ def add_objects(objs, request_options = {})
71
+ batch(build_batch('addObject', objs, false), request_options)
65
72
  end
66
73
 
67
74
  # Add several objects in this index and wait end of indexing
68
75
  #
69
76
  # @param objs the array of objects to add inside the index.
70
77
  # Each object is represented by an associative array
71
- def add_objects!(obj)
72
- res = add_objects(obj)
73
- wait_task(res["taskID"])
74
- return res
78
+ # @param request_options contains extra parameters to send with your query
79
+ def add_objects!(objs, request_options = {})
80
+ res = add_objects(objs, request_options)
81
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
82
+ res
75
83
  end
76
84
 
77
85
  # Search inside the index
@@ -135,10 +143,11 @@ module Algolia
135
143
  # all hits containing a duplicate value for the attributeForDistinct attribute are removed from results.
136
144
  # For example, if the chosen attribute is show_name and several hits have the same value for show_name, then only the best
137
145
  # one is kept and others are removed.
138
- def search(query, params = {})
146
+ # @param request_options contains extra parameters to send with your query
147
+ def search(query, params = {}, request_options = {})
139
148
  encoded_params = Hash[params.map { |k,v| [k.to_s, v.is_a?(Array) ? v.to_json : v] }]
140
149
  encoded_params[:query] = query
141
- client.post(Protocol.search_post_uri(name), { :params => Protocol.to_query(encoded_params) }.to_json, :search)
150
+ client.post(Protocol.search_post_uri(name), { :params => Protocol.to_query(encoded_params) }.to_json, :search, request_options)
142
151
  end
143
152
 
144
153
  class IndexBrowser
@@ -149,9 +158,9 @@ module Algolia
149
158
  @cursor = params[:cursor] || params['cursor'] || nil
150
159
  end
151
160
 
152
- def browse(&block)
161
+ def browse(request_options = {}, &block)
153
162
  loop do
154
- answer = @client.get(Protocol.browse_uri(@name, @params.merge({ :cursor => @cursor })), :read)
163
+ answer = @client.get(Protocol.browse_uri(@name, @params.merge({ :cursor => @cursor })), :read, request_options)
155
164
  answer['hits'].each do |hit|
156
165
  if block.arity == 2
157
166
  yield hit, @cursor
@@ -168,14 +177,16 @@ module Algolia
168
177
  #
169
178
  # Browse all index content
170
179
  #
171
- # @param pageOrQueryParameters The hash of query parameters to use to browse
172
- # To browse from a specific cursor, just add a ":cursor" parameters
180
+ # @param queryParameters The hash of query parameters to use to browse
181
+ # To browse from a specific cursor, just add a ":cursor" parameters
182
+ # @param queryParameters An optional second parameters hash here for backward-compatibility (which will be merged with the first)
183
+ # @param request_options contains extra parameters to send with your query
173
184
  #
174
185
  # @DEPRECATED:
175
- # @param pageOrQueryParameters Pagination parameter used to select the page to retrieve.
186
+ # @param page Pagination parameter used to select the page to retrieve.
176
187
  # @param hitsPerPage: Pagination parameter used to select the number of hits per page. Defaults to 1000.
177
188
  #
178
- def browse(pageOrQueryParameters = nil, hitsPerPage = nil, &block)
189
+ def browse(pageOrQueryParameters = nil, hitsPerPage = nil, request_options = {}, &block)
179
190
  params = {}
180
191
  if pageOrQueryParameters.is_a?(Hash)
181
192
  params.merge!(pageOrQueryParameters)
@@ -189,19 +200,20 @@ module Algolia
189
200
  end
190
201
 
191
202
  if block_given?
192
- IndexBrowser.new(client, name, params).browse(&block)
203
+ IndexBrowser.new(client, name, params).browse(request_options, &block)
193
204
  else
194
205
  params[:page] ||= 0
195
206
  params[:hitsPerPage] ||= 1000
196
- client.get(Protocol.browse_uri(name, params), :read)
207
+ client.get(Protocol.browse_uri(name, params), :read, request_options)
197
208
  end
198
209
  end
199
210
 
200
211
  #
201
212
  # Browse a single page from a specific cursor
202
213
  #
203
- def browse_from(cursor, hitsPerPage = 1000)
204
- client.get(Protocol.browse_uri(name, { :cursor => cursor, :hitsPerPage => hitsPerPage }), :read)
214
+ # @param request_options contains extra parameters to send with your query
215
+ def browse_from(cursor, hitsPerPage = 1000, request_options = {})
216
+ client.get(Protocol.browse_uri(name, { :cursor => cursor, :hitsPerPage => hitsPerPage }), :read, request_options)
205
217
  end
206
218
 
207
219
  #
@@ -209,13 +221,14 @@ module Algolia
209
221
  #
210
222
  # @param objectID the unique identifier of the object to retrieve
211
223
  # @param attributesToRetrieve (optional) if set, contains the list of attributes to retrieve as an array of strings of a string separated by ","
224
+ # @param request_options contains extra parameters to send with your query
212
225
  #
213
- def get_object(objectID, attributesToRetrieve = nil)
226
+ def get_object(objectID, attributesToRetrieve = nil, request_options = {})
214
227
  attributesToRetrieve = attributesToRetrieve.join(',') if attributesToRetrieve.is_a?(Array)
215
228
  if attributesToRetrieve.nil?
216
- client.get(Protocol.object_uri(name, objectID, nil), :read)
229
+ client.get(Protocol.object_uri(name, objectID, nil), :read, request_options)
217
230
  else
218
- client.get(Protocol.object_uri(name, objectID, {:attributes => attributesToRetrieve}), :read)
231
+ client.get(Protocol.object_uri(name, objectID, {:attributes => attributesToRetrieve}), :read, request_options)
219
232
  end
220
233
  end
221
234
 
@@ -224,24 +237,26 @@ module Algolia
224
237
  #
225
238
  # @param objectIDs the array of unique identifier of the objects to retrieve
226
239
  # @param attributesToRetrieve (optional) if set, contains the list of attributes to retrieve as an array of strings of a string separated by ","
240
+ # @param request_options contains extra parameters to send with your query
227
241
  #
228
- def get_objects(objectIDs, attributesToRetrieve = nil)
242
+ def get_objects(objectIDs, attributesToRetrieve = nil, request_options = {})
229
243
  attributesToRetrieve = attributesToRetrieve.join(',') if attributesToRetrieve.is_a?(Array)
230
244
  requests = objectIDs.map do |objectID|
231
245
  req = {:indexName => name, :objectID => objectID.to_s}
232
246
  req[:attributesToRetrieve] = attributesToRetrieve unless attributesToRetrieve.nil?
233
247
  req
234
248
  end
235
- client.post(Protocol.objects_uri, { :requests => requests }.to_json, :read)['results']
249
+ client.post(Protocol.objects_uri, { :requests => requests }.to_json, :read, request_options)['results']
236
250
  end
237
251
 
238
252
  # Check the status of a task on the server.
239
253
  # All server task are asynchronous and you can check the status of a task with this method.
240
254
  #
241
255
  # @param taskID the id of the task returned by server
256
+ # @param request_options contains extra parameters to send with your query
242
257
  #
243
- def get_task_status(taskID)
244
- client.get(Protocol.task_uri(name, taskID), :read)["status"]
258
+ def get_task_status(taskID, request_options = {})
259
+ client.get(Protocol.task_uri(name, taskID), :read, request_options)['status']
245
260
  end
246
261
 
247
262
  # Wait the publication of a task on the server.
@@ -249,11 +264,12 @@ module Algolia
249
264
  #
250
265
  # @param taskID the id of the task returned by server
251
266
  # @param timeBeforeRetry the time in milliseconds before retry (default = 100ms)
267
+ # @param request_options contains extra parameters to send with your query
252
268
  #
253
- def wait_task(taskID, timeBeforeRetry = 100)
269
+ def wait_task(taskID, timeBeforeRetry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options = {})
254
270
  loop do
255
- status = get_task_status(taskID)
256
- if status == "published"
271
+ status = get_task_status(taskID, request_options)
272
+ if status == 'published'
257
273
  return
258
274
  end
259
275
  sleep(timeBeforeRetry.to_f / 1000)
@@ -264,38 +280,42 @@ module Algolia
264
280
  #
265
281
  # @param obj the object to save
266
282
  # @param objectID the associated objectID, if nil 'obj' must contain an 'objectID' key
283
+ # @param request_options contains extra parameters to send with your query
267
284
  #
268
- def save_object(obj, objectID = nil)
269
- client.put(Protocol.object_uri(name, get_objectID(obj, objectID)), obj.to_json)
285
+ def save_object(obj, objectID = nil, request_options = {})
286
+ client.put(Protocol.object_uri(name, get_objectID(obj, objectID)), obj.to_json, :write, request_options)
270
287
  end
271
288
 
272
289
  # Override the content of object and wait end of indexing
273
290
  #
274
291
  # @param obj the object to save
275
292
  # @param objectID the associated objectID, if nil 'obj' must contain an 'objectID' key
293
+ # @param request_options contains extra parameters to send with your query
276
294
  #
277
- def save_object!(obj, objectID = nil)
278
- res = save_object(obj, objectID)
279
- wait_task(res["taskID"])
280
- return res
295
+ def save_object!(obj, objectID = nil, request_options = {})
296
+ res = save_object(obj, objectID, request_options)
297
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
298
+ res
281
299
  end
282
300
 
283
301
  # Override the content of several objects
284
302
  #
285
303
  # @param objs the array of objects to save, each object must contain an 'objectID' key
304
+ # @param request_options contains extra parameters to send with your query
286
305
  #
287
- def save_objects(objs)
288
- batch build_batch('updateObject', objs, true)
306
+ def save_objects(objs, request_options = {})
307
+ batch(build_batch('updateObject', objs, true), request_options)
289
308
  end
290
309
 
291
310
  # Override the content of several objects and wait end of indexing
292
311
  #
293
312
  # @param objs the array of objects to save, each object must contain an objectID attribute
313
+ # @param request_options contains extra parameters to send with your query
294
314
  #
295
- def save_objects!(objs)
296
- res = save_objects(objs)
297
- wait_task(res["taskID"])
298
- return res
315
+ def save_objects!(objs, request_options = {})
316
+ res = save_objects(objs, request_options)
317
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
318
+ res
299
319
  end
300
320
 
301
321
  #
@@ -304,9 +324,10 @@ module Algolia
304
324
  # @param obj the object attributes to override
305
325
  # @param objectID the associated objectID, if nil 'obj' must contain an 'objectID' key
306
326
  # @param create_if_not_exits a boolean, if true creates the object if this one doesn't exist
327
+ # @param request_options contains extra parameters to send with your query
307
328
  #
308
- def partial_update_object(obj, objectID = nil, create_if_not_exits = true)
309
- client.post(Protocol.partial_object_uri(name, get_objectID(obj, objectID), create_if_not_exits), obj.to_json)
329
+ def partial_update_object(obj, objectID = nil, create_if_not_exits = true, request_options = {})
330
+ client.post(Protocol.partial_object_uri(name, get_objectID(obj, objectID), create_if_not_exits), obj.to_json, :write, request_options)
310
331
  end
311
332
 
312
333
  #
@@ -314,12 +335,13 @@ module Algolia
314
335
  #
315
336
  # @param objs an array of objects to update (each object must contains a objectID attribute)
316
337
  # @param create_if_not_exits a boolean, if true create the objects if they don't exist
338
+ # @param request_options contains extra parameters to send with your query
317
339
  #
318
- def partial_update_objects(objs, create_if_not_exits = true)
340
+ def partial_update_objects(objs, create_if_not_exits = true, request_options = {})
319
341
  if create_if_not_exits
320
- batch build_batch('partialUpdateObject', objs, true)
342
+ batch(build_batch('partialUpdateObject', objs, true), request_options)
321
343
  else
322
- batch build_batch('partialUpdateObjectNoCreate', objs, true)
344
+ batch(build_batch('partialUpdateObjectNoCreate', objs, true), request_options)
323
345
  end
324
346
  end
325
347
 
@@ -328,11 +350,12 @@ module Algolia
328
350
  #
329
351
  # @param objs an array of objects to update (each object must contains a objectID attribute)
330
352
  # @param create_if_not_exits a boolean, if true create the objects if they don't exist
353
+ # @param request_options contains extra parameters to send with your query
331
354
  #
332
- def partial_update_objects!(objs, create_if_not_exits = true)
333
- res = partial_update_objects(objs, create_if_not_exits)
334
- wait_task(res["taskID"])
335
- return res
355
+ def partial_update_objects!(objs, create_if_not_exits = true, request_options = {})
356
+ res = partial_update_objects(objs, create_if_not_exits, request_options)
357
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
358
+ res
336
359
  end
337
360
 
338
361
  #
@@ -341,53 +364,58 @@ module Algolia
341
364
  # @param obj the attributes to override
342
365
  # @param objectID the associated objectID, if nil 'obj' must contain an 'objectID' key
343
366
  # @param create_if_not_exits a boolean, if true creates the object if this one doesn't exist
367
+ # @param request_options contains extra parameters to send with your query
344
368
  #
345
- def partial_update_object!(obj, objectID = nil, create_if_not_exits = true)
346
- res = partial_update_object(obj, objectID, create_if_not_exits)
347
- wait_task(res["taskID"])
348
- return res
369
+ def partial_update_object!(obj, objectID = nil, create_if_not_exits = true, request_options = {})
370
+ res = partial_update_object(obj, objectID, create_if_not_exits, request_options)
371
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
372
+ res
349
373
  end
350
374
 
351
375
  #
352
376
  # Delete an object from the index
353
377
  #
354
378
  # @param objectID the unique identifier of object to delete
379
+ # @param request_options contains extra parameters to send with your query
355
380
  #
356
- def delete_object(objectID)
381
+ def delete_object(objectID, request_options = {})
357
382
  raise ArgumentError.new('objectID must not be blank') if objectID.nil? || objectID == ''
358
- client.delete(Protocol.object_uri(name, objectID))
383
+ client.delete(Protocol.object_uri(name, objectID), :write, request_options)
359
384
  end
360
385
 
361
386
  #
362
387
  # Delete an object from the index and wait end of indexing
363
388
  #
364
389
  # @param objectID the unique identifier of object to delete
390
+ # @param request_options contains extra parameters to send with your query
365
391
  #
366
- def delete_object!(objectID)
367
- res = delete_object(objectID)
368
- wait_task(res["taskID"])
369
- return res
392
+ def delete_object!(objectID, request_options = {})
393
+ res = delete_object(objectID, request_options)
394
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
395
+ res
370
396
  end
371
397
 
372
398
  #
373
399
  # Delete several objects
374
400
  #
375
401
  # @param objs an array of objectIDs
402
+ # @param request_options contains extra parameters to send with your query
376
403
  #
377
- def delete_objects(objs)
404
+ def delete_objects(objs, request_options = {})
378
405
  check_array objs
379
- batch build_batch('deleteObject', objs.map { |objectID| { :objectID => objectID } }, false)
406
+ batch(build_batch('deleteObject', objs.map { |objectID| { :objectID => objectID } }, false), request_options)
380
407
  end
381
408
 
382
409
  #
383
410
  # Delete several objects and wait end of indexing
384
411
  #
385
412
  # @param objs an array of objectIDs
413
+ # @param request_options contains extra parameters to send with your query
386
414
  #
387
- def delete_objects!(objs)
388
- res = delete_objects(objs)
389
- wait_task(res["taskID"])
390
- return res
415
+ def delete_objects!(objs, request_options = {})
416
+ res = delete_objects(objs, request_options)
417
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
418
+ res
391
419
  end
392
420
 
393
421
  #
@@ -396,8 +424,9 @@ module Algolia
396
424
  # asynchronously
397
425
  # @param query the query string
398
426
  # @param params the optional query parameters
427
+ # @param request_options contains extra parameters to send with your query
399
428
  #
400
- def delete_by_query(query, params = nil)
429
+ def delete_by_query(query, params = nil, request_options = {})
401
430
  raise ArgumentError.new('query cannot be nil, use the `clear` method to wipe the entire index') if query.nil? && params.nil?
402
431
  params = sanitized_delete_by_query_params(params)
403
432
 
@@ -409,7 +438,7 @@ module Algolia
409
438
  ids = []
410
439
 
411
440
  while params[:cursor] != nil
412
- result = browse(params)
441
+ result = browse(params, nil, request_options)
413
442
 
414
443
  params[:cursor] = result['cursor']
415
444
 
@@ -419,7 +448,7 @@ module Algolia
419
448
  ids += hits.map { |h| h['objectID'] }
420
449
  end
421
450
 
422
- delete_objects(ids)
451
+ delete_objects(ids, request_options)
423
452
  end
424
453
 
425
454
  #
@@ -427,10 +456,11 @@ module Algolia
427
456
  #
428
457
  # @param query the query string
429
458
  # @param params the optional query parameters
459
+ # @param request_options contains extra parameters to send with your query
430
460
  #
431
- def delete_by_query!(query, params = nil)
432
- res = delete_by_query(query, params)
433
- wait_task(res['taskID']) if res
461
+ def delete_by_query!(query, params = nil, request_options = {})
462
+ res = delete_by_query(query, params, request_options)
463
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) if res
434
464
  res
435
465
  end
436
466
 
@@ -439,72 +469,75 @@ module Algolia
439
469
  # This method deletes every record matching the filters provided
440
470
  #
441
471
  # @param params query parameters
472
+ # @param request_options contains extra parameters to send with your query
442
473
  #
443
- def delete_by(params)
474
+ def delete_by(params, request_options = {})
444
475
  raise ArgumentError.new('params cannot be nil, use the `clear` method to wipe the entire index') if params.nil?
445
476
  params = sanitized_delete_by_query_params(params)
446
- client.post(Protocol.delete_by_uri(name), params.to_json)
477
+ client.post(Protocol.delete_by_uri(name), params.to_json, :write, request_options)
447
478
  end
448
479
 
449
480
  #
450
481
  # Delete all objects matching a query (doesn't work with actual text queries)
451
482
  # This method deletes every record matching the filters provided and waits for the end of indexing
452
483
  # @param params query parameters
484
+ # @param request_options contains extra parameters to send with your query
453
485
  #
454
- def delete_by!(params)
455
- res = delete_by(params)
456
- wait_task(res['taskID']) if res
486
+ def delete_by!(params, request_options = {})
487
+ res = delete_by(params, request_options)
488
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options) if res
457
489
  res
458
490
  end
459
491
 
460
492
  #
461
493
  # Delete the index content
462
494
  #
495
+ # @param request_options contains extra parameters to send with your query
463
496
  #
464
- def clear
465
- client.post(Protocol.clear_uri(name))
497
+ def clear(request_options = {})
498
+ client.post(Protocol.clear_uri(name), {}, :write, request_options)
466
499
  end
467
500
  alias_method :clear_index, :clear
468
501
 
469
502
  #
470
503
  # Delete the index content and wait end of indexing
471
504
  #
472
- def clear!
473
- res = clear
474
- wait_task(res["taskID"])
475
- return res
505
+ def clear!(request_options = {})
506
+ res = clear(request_options)
507
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
508
+ res
476
509
  end
477
510
  alias_method :clear_index!, :clear!
478
511
 
479
512
  #
480
513
  # Set settings for this index
481
514
  #
482
- def set_settings(new_settings, options = {})
483
- client.put(Protocol.settings_uri(name, options), new_settings.to_json)
515
+ def set_settings(new_settings, options = {}, request_options = {})
516
+ client.put(Protocol.settings_uri(name, options), new_settings.to_json, :write, request_options)
484
517
  end
485
518
 
486
519
  # Set settings for this index and wait end of indexing
487
520
  #
488
- def set_settings!(new_settings, options = {})
489
- res = set_settings(new_settings, options)
490
- wait_task(res["taskID"])
491
- return res
521
+ def set_settings!(new_settings, options = {}, request_options = {})
522
+ res = set_settings(new_settings, options, request_options)
523
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
524
+ res
492
525
  end
493
526
 
494
527
  # Get settings of this index
495
- def get_settings(options = {})
528
+ def get_settings(options = {}, request_options = {})
496
529
  options['getVersion'] = 2 if !options[:getVersion] && !options['getVersion']
497
- client.get("#{Protocol.settings_uri(name, options)}", :read)
530
+ client.get(Protocol.settings_uri(name, options).to_s, :read, request_options)
498
531
  end
499
532
 
500
533
  # List all existing user keys with their associated ACLs
501
- def list_api_keys
502
- client.get(Protocol.index_keys_uri(name), :read)
534
+ def list_api_keys(request_options = {})
535
+ client.get(Protocol.index_keys_uri(name), :read, request_options)
503
536
  end
504
537
 
505
538
  # Get ACL of a user key
506
- def get_api_key(key)
507
- client.get(Protocol.index_key_uri(name, key), :read)
539
+ def get_api_key(key, request_options = {})
540
+ client.get(Protocol.index_key_uri(name, key), :read, request_options)
508
541
  end
509
542
 
510
543
  #
@@ -531,8 +564,9 @@ module Algolia
531
564
  # @param validity the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
532
565
  # @param maxQueriesPerIPPerHour the maximum number of API calls allowed from an IP address per hour (0 means unlimited)
533
566
  # @param maxHitsPerQuery the maximum number of hits this API key can retrieve in one call (0 means unlimited)
567
+ # @param request_options contains extra parameters to send with your query
534
568
  #
535
- def add_api_key(obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0)
569
+ def add_api_key(obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0, request_options = {})
536
570
  if obj.instance_of? Array
537
571
  params = {
538
572
  :acl => obj
@@ -549,7 +583,7 @@ module Algolia
549
583
  if maxHitsPerQuery != 0
550
584
  params["maxHitsPerQuery"] = maxHitsPerQuery.to_i
551
585
  end
552
- client.post(Protocol.index_keys_uri(name), params.to_json)
586
+ client.post(Protocol.index_keys_uri(name), params.to_json, :write, request_options)
553
587
  end
554
588
 
555
589
  #
@@ -576,8 +610,9 @@ module Algolia
576
610
  # @param validity the number of seconds after which the key will be automatically removed (0 means no time limit for this key)
577
611
  # @param maxQueriesPerIPPerHour the maximum number of API calls allowed from an IP address per hour (0 means unlimited)
578
612
  # @param maxHitsPerQuery the maximum number of hits this API key can retrieve in one call (0 means unlimited)
613
+ # @param request_options contains extra parameters to send with your query
579
614
  #
580
- def update_api_key(key, obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0)
615
+ def update_api_key(key, obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0, request_options = {})
581
616
  if obj.instance_of? Array
582
617
  params = {
583
618
  :acl => obj
@@ -594,24 +629,24 @@ module Algolia
594
629
  if maxHitsPerQuery != 0
595
630
  params["maxHitsPerQuery"] = maxHitsPerQuery.to_i
596
631
  end
597
- client.put(Protocol.index_key_uri(name, key), params.to_json)
632
+ client.put(Protocol.index_key_uri(name, key), params.to_json, :write, request_options)
598
633
  end
599
634
 
600
635
 
601
636
  # Delete an existing user key
602
- def delete_api_key(key)
603
- client.delete(Protocol.index_key_uri(name, key))
637
+ def delete_api_key(key, request_options = {})
638
+ client.delete(Protocol.index_key_uri(name, key), :write, request_options)
604
639
  end
605
640
 
606
641
  # Send a batch request
607
- def batch(request)
608
- client.post(Protocol.batch_uri(name), request.to_json, :batch)
642
+ def batch(request, request_options = {})
643
+ client.post(Protocol.batch_uri(name), request.to_json, :batch, request_options)
609
644
  end
610
645
 
611
646
  # Send a batch request and wait the end of the indexing
612
- def batch!(request)
613
- res = batch(request)
614
- wait_task(res['taskID'])
647
+ def batch!(request, request_options = {})
648
+ res = batch(request, request_options)
649
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
615
650
  res
616
651
  end
617
652
 
@@ -623,10 +658,11 @@ module Algolia
623
658
  # @param query An optional query to take extra search parameters into account.
624
659
  # These parameters apply to index objects like in a regular search query.
625
660
  # Only facet values contained in the matched objects will be returned.
626
- def search_for_facet_values(facet, text, query = {})
661
+ # @param request_options contains extra parameters to send with your query
662
+ def search_for_facet_values(facet, text, query = {}, request_options = {})
627
663
  params = query.clone
628
664
  params['facetQuery'] = text
629
- client.post(Protocol.search_facet_uri(name, facet), params.to_json)
665
+ client.post(Protocol.search_facet_uri(name, facet), params.to_json, :read, request_options)
630
666
  end
631
667
 
632
668
  # deprecated
@@ -639,7 +675,8 @@ module Algolia
639
675
  # @param params a hash representing the regular query parameters
640
676
  # @param refinements a hash ("string" -> ["array", "of", "refined", "values"]) representing the current refinements
641
677
  # ex: { "my_facet1" => ["my_value1", ["my_value2"], "my_disjunctive_facet1" => ["my_value1", "my_value2"] }
642
- def search_disjunctive_faceting(query, disjunctive_facets, params = {}, refinements = {})
678
+ # @param request_options contains extra parameters to send with your query
679
+ def search_disjunctive_faceting(query, disjunctive_facets, params = {}, refinements = {}, request_options = {})
643
680
  raise ArgumentError.new('Argument "disjunctive_facets" must be a String or an Array') unless disjunctive_facets.is_a?(String) || disjunctive_facets.is_a?(Array)
644
681
  raise ArgumentError.new('Argument "refinements" must be a Hash of Arrays') if !refinements.is_a?(Hash) || !refinements.select { |k, v| !v.is_a?(Array) }.empty?
645
682
 
@@ -693,7 +730,7 @@ module Algolia
693
730
  :analytics => false
694
731
  })
695
732
  end
696
- answers = client.multiple_queries(queries)
733
+ answers = client.multiple_queries(queries, { :request_options => request_options })
697
734
 
698
735
  # aggregate answers
699
736
  ## first answer stores the hits + regular facets
@@ -720,15 +757,18 @@ module Algolia
720
757
  #
721
758
  # Alias of Algolia.list_indexes
722
759
  #
723
- def Index.all
724
- Algolia.list_indexes
760
+ # @param request_options contains extra parameters to send with your query
761
+ #
762
+ def Index.all(request_options = {})
763
+ Algolia.list_indexes(request_options)
725
764
  end
726
765
 
727
766
  # Search synonyms
728
767
  #
729
768
  # @param query the query
730
769
  # @param params an optional hash of :type, :page, :hitsPerPage
731
- def search_synonyms(query, params = {})
770
+ # @param request_options contains extra parameters to send with your query
771
+ def search_synonyms(query, params = {}, request_options = {})
732
772
  type = params[:type] || params['type']
733
773
  type = type.join(',') if type.is_a?(Array)
734
774
  page = params[:page] || params['page'] || 0
@@ -739,32 +779,35 @@ module Algolia
739
779
  :page => page,
740
780
  :hitsPerPage => hits_per_page
741
781
  }
742
- client.post(Protocol.search_synonyms_uri(name), params.to_json, :read)
782
+ client.post(Protocol.search_synonyms_uri(name), params.to_json, :read, request_options)
743
783
  end
744
784
 
745
785
  # Get a synonym
746
786
  #
747
787
  # @param objectID the synonym objectID
748
- def get_synonym(objectID)
749
- client.get(Protocol.synonym_uri(name, objectID), :read)
788
+ # @param request_options contains extra parameters to send with your query
789
+ def get_synonym(objectID, request_options = {})
790
+ client.get(Protocol.synonym_uri(name, objectID), :read, request_options)
750
791
  end
751
792
 
752
793
  # Delete a synonym
753
794
  #
754
795
  # @param objectID the synonym objectID
755
796
  # @param forward_to_replicas should we forward the delete to replica indices
756
- def delete_synonym(objectID, forward_to_replicas = false)
757
- client.delete("#{Protocol.synonym_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", :write)
797
+ # @param request_options contains extra parameters to send with your query
798
+ def delete_synonym(objectID, forward_to_replicas = false, request_options = {})
799
+ client.delete("#{Protocol.synonym_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", :write, request_options)
758
800
  end
759
801
 
760
802
  # Delete a synonym and wait the end of indexing
761
803
  #
762
804
  # @param objectID the synonym objectID
763
805
  # @param forward_to_replicas should we forward the delete to replica indices
764
- def delete_synonym!(objectID, forward_to_replicas = false)
765
- res = delete_synonym(objectID, forward_to_replicas)
766
- wait_task(res["taskID"])
767
- return res
806
+ # @param request_options contains extra parameters to send with your query
807
+ def delete_synonym!(objectID, forward_to_replicas = false, request_options = {})
808
+ res = delete_synonym(objectID, forward_to_replicas, request_options)
809
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
810
+ res
768
811
  end
769
812
 
770
813
  # Save a synonym
@@ -772,8 +815,9 @@ module Algolia
772
815
  # @param objectID the synonym objectID
773
816
  # @param synonym the synonym
774
817
  # @param forward_to_replicas should we forward the delete to replica indices
775
- def save_synonym(objectID, synonym, forward_to_replicas = false)
776
- client.put("#{Protocol.synonym_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", synonym.to_json, :write)
818
+ # @param request_options contains extra parameters to send with your query
819
+ def save_synonym(objectID, synonym, forward_to_replicas = false, request_options = {})
820
+ client.put("#{Protocol.synonym_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", synonym.to_json, :write, request_options)
777
821
  end
778
822
 
779
823
  # Save a synonym and wait the end of indexing
@@ -781,26 +825,29 @@ module Algolia
781
825
  # @param objectID the synonym objectID
782
826
  # @param synonym the synonym
783
827
  # @param forward_to_replicas should we forward the delete to replica indices
784
- def save_synonym!(objectID, synonym, forward_to_replicas = false)
785
- res = save_synonym(objectID, synonym, forward_to_replicas)
786
- wait_task(res["taskID"])
787
- return res
828
+ # @param request_options contains extra parameters to send with your query
829
+ def save_synonym!(objectID, synonym, forward_to_replicas = false, request_options = {})
830
+ res = save_synonym(objectID, synonym, forward_to_replicas, request_options)
831
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
832
+ res
788
833
  end
789
834
 
790
835
  # Clear all synonyms
791
836
  #
792
837
  # @param forward_to_replicas should we forward the delete to replica indices
793
- def clear_synonyms(forward_to_replicas = false)
794
- client.post("#{Protocol.clear_synonyms_uri(name)}?forwardToReplicas=#{forward_to_replicas}", :write)
838
+ # @param request_options contains extra parameters to send with your query
839
+ def clear_synonyms(forward_to_replicas = false, request_options = {})
840
+ client.post("#{Protocol.clear_synonyms_uri(name)}?forwardToReplicas=#{forward_to_replicas}", {}, :write, request_options)
795
841
  end
796
842
 
797
843
  # Clear all synonyms and wait the end of indexing
798
844
  #
799
845
  # @param forward_to_replicas should we forward the delete to replica indices
800
- def clear_synonyms!(forward_to_replicas = false)
801
- res = clear_synonyms(forward_to_replicas)
802
- wait_task(res["taskID"])
803
- return res
846
+ # @param request_options contains extra parameters to send with your query
847
+ def clear_synonyms!(forward_to_replicas = false, request_options = {})
848
+ res = clear_synonyms(forward_to_replicas, request_options)
849
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
850
+ res
804
851
  end
805
852
 
806
853
  # Add/Update an array of synonyms
@@ -808,8 +855,9 @@ module Algolia
808
855
  # @param synonyms the array of synonyms to add/update
809
856
  # @param forward_to_replicas should we forward the delete to replica indices
810
857
  # @param replace_existing_synonyms should we replace the existing synonyms before adding the new ones
811
- def batch_synonyms(synonyms, forward_to_replicas = false, replace_existing_synonyms = false)
812
- client.post("#{Protocol.batch_synonyms_uri(name)}?forwardToReplicas=#{forward_to_replicas}&replaceExistingSynonyms=#{replace_existing_synonyms}", synonyms.to_json, :batch)
858
+ # @param request_options contains extra parameters to send with your query
859
+ def batch_synonyms(synonyms, forward_to_replicas = false, replace_existing_synonyms = false, request_options = {})
860
+ client.post("#{Protocol.batch_synonyms_uri(name)}?forwardToReplicas=#{forward_to_replicas}&replaceExistingSynonyms=#{replace_existing_synonyms}", synonyms.to_json, :batch, request_options)
813
861
  end
814
862
 
815
863
  # Add/Update an array of synonyms and wait the end of indexing
@@ -817,17 +865,40 @@ module Algolia
817
865
  # @param synonyms the array of synonyms to add/update
818
866
  # @param forward_to_replicas should we forward the delete to replica indices
819
867
  # @param replace_existing_synonyms should we replace the existing synonyms before adding the new ones
820
- def batch_synonyms!(synonyms, forward_to_replicas = false, replace_existing_synonyms = false)
821
- res = batch_synonyms(synonyms, forward_to_replicas, replace_existing_synonyms)
822
- wait_task(res["taskID"])
823
- return res
868
+ # @param request_options contains extra parameters to send with your query
869
+ def batch_synonyms!(synonyms, forward_to_replicas = false, replace_existing_synonyms = false, request_options = {})
870
+ res = batch_synonyms(synonyms, forward_to_replicas, replace_existing_synonyms, request_options)
871
+ wait_task(res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
872
+ res
873
+ end
874
+
875
+ # Export the full list of synonyms
876
+ # Accepts an optional block to which it will pass each synonym
877
+ # Also returns an array with all the synonyms
878
+ #
879
+ # @param hits_per_page Amount of synonyms to retrieve on each internal request - Optional - Default: 100
880
+ # @param request_options contains extra parameters to send with your query - Optional
881
+ def export_synonyms(hits_per_page = 100, request_options = {}, &_block)
882
+ res = []
883
+ page = 0
884
+ loop do
885
+ curr = search_synonyms('', { :hits_per_page => hits_per_page, :page => page }, request_options)['hits']
886
+ curr.each do |synonym|
887
+ res << synonym
888
+ yield synonym if block_given?
889
+ end
890
+ break if curr.size < hits_per_page
891
+ page += 1
892
+ end
893
+ res
824
894
  end
825
895
 
826
896
  # Search rules
827
897
  #
828
898
  # @param query the query
829
899
  # @param params an optional hash of :anchoring, :context, :page, :hitsPerPage
830
- def search_rules(query, params = {})
900
+ # @param request_options contains extra parameters to send with your query
901
+ def search_rules(query, params = {}, request_options = {})
831
902
  anchoring = params[:anchoring]
832
903
  context = params[:context]
833
904
  page = params[:page] || params['page'] || 0
@@ -839,31 +910,34 @@ module Algolia
839
910
  }
840
911
  params[:anchoring] = anchoring unless anchoring.nil?
841
912
  params[:context] = context unless context.nil?
842
- client.post(Protocol.search_rules_uri(name), params.to_json, :read)
913
+ client.post(Protocol.search_rules_uri(name), params.to_json, :read, request_options)
843
914
  end
844
915
 
845
916
  # Get a rule
846
917
  #
847
918
  # @param objectID the rule objectID
848
- def get_rule(objectID)
849
- client.get(Protocol.rule_uri(name, objectID), :read)
919
+ # @param request_options contains extra parameters to send with your query
920
+ def get_rule(objectID, request_options = {})
921
+ client.get(Protocol.rule_uri(name, objectID), :read, request_options)
850
922
  end
851
923
 
852
924
  # Delete a rule
853
925
  #
854
926
  # @param objectID the rule objectID
855
927
  # @param forward_to_replicas should we forward the delete to replica indices
856
- def delete_rule(objectID, forward_to_replicas = false)
857
- client.delete("#{Protocol.rule_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", :write)
928
+ # @param request_options contains extra parameters to send with your query
929
+ def delete_rule(objectID, forward_to_replicas = false, request_options = {})
930
+ client.delete("#{Protocol.rule_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", :write, request_options)
858
931
  end
859
932
 
860
933
  # Delete a rule and wait the end of indexing
861
934
  #
862
935
  # @param objectID the rule objectID
863
936
  # @param forward_to_replicas should we forward the delete to replica indices
864
- def delete_rule!(objectID, forward_to_replicas = false)
865
- res = delete_rule(objectID, forward_to_replicas)
866
- wait_task(res["taskID"])
937
+ # @param request_options contains extra parameters to send with your query
938
+ def delete_rule!(objectID, forward_to_replicas = false, request_options = {})
939
+ res = delete_rule(objectID, forward_to_replicas, request_options)
940
+ wait_task(res["taskID"], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
867
941
  return res
868
942
  end
869
943
 
@@ -872,8 +946,9 @@ module Algolia
872
946
  # @param objectID the rule objectID
873
947
  # @param rule the rule
874
948
  # @param forward_to_replicas should we forward the delete to replica indices
875
- def save_rule(objectID, rule, forward_to_replicas = false)
876
- client.put("#{Protocol.rule_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", rule.to_json, :write)
949
+ # @param request_options contains extra parameters to send with your query
950
+ def save_rule(objectID, rule, forward_to_replicas = false, request_options = {})
951
+ client.put("#{Protocol.rule_uri(name, objectID)}?forwardToReplicas=#{forward_to_replicas}", rule.to_json, :write, request_options)
877
952
  end
878
953
 
879
954
  # Save a rule and wait the end of indexing
@@ -881,25 +956,28 @@ module Algolia
881
956
  # @param objectID the rule objectID
882
957
  # @param rule the rule
883
958
  # @param forward_to_replicas should we forward the delete to replica indices
884
- def save_rule!(objectID, rule, forward_to_replicas = false)
885
- res = save_rule(objectID, rule, forward_to_replicas)
886
- wait_task(res["taskID"])
959
+ # @param request_options contains extra parameters to send with your query
960
+ def save_rule!(objectID, rule, forward_to_replicas = false, request_options = {})
961
+ res = save_rule(objectID, rule, forward_to_replicas, request_options)
962
+ wait_task(res["taskID"], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
887
963
  return res
888
964
  end
889
965
 
890
966
  # Clear all rules
891
967
  #
892
968
  # @param forward_to_replicas should we forward the delete to replica indices
893
- def clear_rules(forward_to_replicas = false)
894
- client.post("#{Protocol.clear_rules_uri(name)}?forwardToReplicas=#{forward_to_replicas}")
969
+ # @param request_options contains extra parameters to send with your query
970
+ def clear_rules(forward_to_replicas = false, request_options = {})
971
+ client.post("#{Protocol.clear_rules_uri(name)}?forwardToReplicas=#{forward_to_replicas}", {}, :write, request_options)
895
972
  end
896
973
 
897
974
  # Clear all rules and wait the end of indexing
898
975
  #
899
976
  # @param forward_to_replicas should we forward the delete to replica indices
900
- def clear_rules!(forward_to_replicas = false)
901
- res = clear_rules(forward_to_replicas)
902
- wait_task(res["taskID"])
977
+ # @param request_options contains extra parameters to send with your query
978
+ def clear_rules!(forward_to_replicas = false, request_options = {})
979
+ res = clear_rules(forward_to_replicas, request_options)
980
+ wait_task(res["taskID"], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
903
981
  return res
904
982
  end
905
983
 
@@ -908,8 +986,9 @@ module Algolia
908
986
  # @param rules the array of rules to add/update
909
987
  # @param forward_to_replicas should we forward the delete to replica indices
910
988
  # @param clear_existing_rules should we clear the existing rules before adding the new ones
911
- def batch_rules(rules, forward_to_replicas = false, clear_existing_rules = false)
912
- client.post("#{Protocol.batch_rules_uri(name)}?forwardToReplicas=#{forward_to_replicas}&clearExistingRules=#{clear_existing_rules}", rules.to_json, :batch)
989
+ # @param request_options contains extra parameters to send with your query
990
+ def batch_rules(rules, forward_to_replicas = false, clear_existing_rules = false, request_options = {})
991
+ client.post("#{Protocol.batch_rules_uri(name)}?forwardToReplicas=#{forward_to_replicas}&clearExistingRules=#{clear_existing_rules}", rules.to_json, :batch, request_options)
913
992
  end
914
993
 
915
994
  # Add/Update an array of rules and wait the end of indexing
@@ -917,12 +996,34 @@ module Algolia
917
996
  # @param rules the array of rules to add/update
918
997
  # @param forward_to_replicas should we forward the delete to replica indices
919
998
  # @param clear_existing_rules should we clear the existing rules before adding the new ones
920
- def batch_rules!(rules, forward_to_replicas = false, clear_existing_rules = false)
921
- res = batch_rules(rules, forward_to_replicas, clear_existing_rules)
922
- wait_task(res["taskID"])
999
+ # @param request_options contains extra parameters to send with your query
1000
+ def batch_rules!(rules, forward_to_replicas = false, clear_existing_rules = false, request_options = {})
1001
+ res = batch_rules(rules, forward_to_replicas, clear_existing_rules, request_options)
1002
+ wait_task(res["taskID"], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
923
1003
  return res
924
1004
  end
925
1005
 
1006
+ # Export the full list of rules
1007
+ # Accepts an optional block to which it will pass each rule
1008
+ # Also returns an array with all the rules
1009
+ #
1010
+ # @param hits_per_page Amount of rules to retrieve on each internal request - Optional - Default: 100
1011
+ # @param request_options contains extra parameters to send with your query - Optional
1012
+ def export_rules(hits_per_page = 100, request_options = {}, &_block)
1013
+ res = []
1014
+ page = 0
1015
+ loop do
1016
+ curr = search_rules('', { :hits_per_page => hits_per_page, :page => page }, request_options)['hits']
1017
+ curr.each do |rule|
1018
+ res << rule
1019
+ yield rule if block_given?
1020
+ end
1021
+ break if curr.size < hits_per_page
1022
+ page += 1
1023
+ end
1024
+ res
1025
+ end
1026
+
926
1027
  # Deprecated
927
1028
  alias_method :get_user_key, :get_api_key
928
1029
  alias_method :list_user_keys, :list_api_keys