Class: Algolia::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/algolia/client.rb

Overview

A class which encapsulates the HTTPS communication with the Algolia API server. Uses the HTTPClient library for low-level HTTP communication.

Constant Summary collapse

DEFAULT_CONNECT_TIMEOUT =
2
DEFAULT_RECEIVE_TIMEOUT =
30
DEFAULT_SEND_TIMEOUT =
30
DEFAULT_BATCH_TIMEOUT =
120
DEFAULT_SEARCH_TIMEOUT =
5
DEFAULT_USER_AGENT =
["Algolia for Ruby (#{::Algolia::VERSION})", "Ruby (#{RUBY_VERSION})"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/algolia/client.rb', line 26

def initialize(data = {})
  raise ArgumentError.new('No APPLICATION_ID provided, please set :application_id') if data[:application_id].nil?

  @ssl             = data[:ssl].nil? ? true : data[:ssl]
  @ssl_version     = data[:ssl_version].nil? ? nil : data[:ssl_version]
  @gzip            = data[:gzip].nil? ? true : data[:gzip]
  @application_id  = data[:application_id]
  @api_key         = data[:api_key]
  @hosts           = data[:hosts] || (["#{@application_id}.algolia.net"] + 1.upto(3).map { |i| "#{@application_id}-#{i}.algolianet.com" }.shuffle)
  @search_hosts    = data[:search_hosts] || data[:hosts] || (["#{@application_id}-dsn.algolia.net"] + 1.upto(3).map { |i| "#{@application_id}-#{i}.algolianet.com" }.shuffle)
  @connect_timeout = data[:connect_timeout] || DEFAULT_CONNECT_TIMEOUT
  @send_timeout    = data[:send_timeout] || DEFAULT_SEND_TIMEOUT
  @batch_timeout   = data[:batch_timeout] || DEFAULT_BATCH_TIMEOUT
  @receive_timeout = data[:receive_timeout] || DEFAULT_RECEIVE_TIMEOUT
  @search_timeout  = data[:search_timeout] || DEFAULT_SEARCH_TIMEOUT
  @headers = {
    Protocol::HEADER_API_KEY => api_key,
    Protocol::HEADER_APP_ID  => application_id,
    'Content-Type'           => 'application/json; charset=utf-8',
    'User-Agent'             => DEFAULT_USER_AGENT.push(data[:user_agent]).compact.join('; ')
  }
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



17
18
19
# File 'lib/algolia/client.rb', line 17

def api_key
  @api_key
end

#application_idObject (readonly)

Returns the value of attribute application_id.



17
18
19
# File 'lib/algolia/client.rb', line 17

def application_id
  @application_id
end

#batch_timeoutObject (readonly)

Returns the value of attribute batch_timeout.



17
18
19
# File 'lib/algolia/client.rb', line 17

def batch_timeout
  @batch_timeout
end

#connect_timeoutObject (readonly)

Returns the value of attribute connect_timeout.



17
18
19
# File 'lib/algolia/client.rb', line 17

def connect_timeout
  @connect_timeout
end

#headersObject (readonly)

Returns the value of attribute headers.



17
18
19
# File 'lib/algolia/client.rb', line 17

def headers
  @headers
end

#hostsObject (readonly)

Returns the value of attribute hosts.



17
18
19
# File 'lib/algolia/client.rb', line 17

def hosts
  @hosts
end

#receive_timeoutObject (readonly)

Returns the value of attribute receive_timeout.



17
18
19
# File 'lib/algolia/client.rb', line 17

def receive_timeout
  @receive_timeout
end

#search_hostsObject (readonly)

Returns the value of attribute search_hosts.



17
18
19
# File 'lib/algolia/client.rb', line 17

def search_hosts
  @search_hosts
end

#search_timeoutObject (readonly)

Returns the value of attribute search_timeout.



17
18
19
# File 'lib/algolia/client.rb', line 17

def search_timeout
  @search_timeout
end

#send_timeoutObject (readonly)

Returns the value of attribute send_timeout.



17
18
19
# File 'lib/algolia/client.rb', line 17

def send_timeout
  @send_timeout
end

#sslObject (readonly)

Returns the value of attribute ssl.



17
18
19
# File 'lib/algolia/client.rb', line 17

def ssl
  @ssl
end

#ssl_versionObject (readonly)

Returns the value of attribute ssl_version.



17
18
19
# File 'lib/algolia/client.rb', line 17

def ssl_version
  @ssl_version
end

Instance Method Details

#add_api_key(object, request_options = {}, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, indexes = nil) ⇒ Object Also known as: add_user_key

Create a new user key

Deprecated call was add_api_key(acl, validity, maxQueriesPerIPPerHour, maxHitsPerQuery, indexes)

ACL can contain an array with those strings:
  - search: allow to search (https and http)
  - addObject: allows to add/update an object in the index (https only)
  - deleteObject : allows to delete an existing object (https only)
  - deleteIndex : allows to delete index content (https only)
  - settings : allows to get index settings (https only)
  - editSettings : allows to change index settings (https only)

@param object The list of parameters for this key.
       Defined by a Hash that can contain the following values:
        - acl: array of string
        - indexes: array of string
        - validity: int
        - referers: array of string
        - description: string
        - maxHitsPerQuery: integer
        - queryParameters: string
        - maxQueriesPerIPPerHour: integer
@param request_options contains extra parameters to send with your query - Default = {}


383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/algolia/client.rb', line 383

def add_api_key(object, request_options = {}, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, indexes = nil)
  if object.instance_of?(Array)
    params = { :acl => object }
  else
    params = object
  end

  validity = 0
  unless request_options.is_a?(Hash)
    validity = request_options
    request_options = {}
  end

  params[:indexes] = indexes if indexes
  params['validity'] = validity.to_i if validity != 0
  params['maxHitsPerQuery'] = max_hits_per_query.to_i if max_hits_per_query != 0
  params['maxQueriesPerIPPerHour'] = max_queries_per_IP_per_hour.to_i if max_queries_per_IP_per_hour != 0

  post(Protocol.keys_uri, params.to_json, :write, request_options)
end

#assign_user_id(user_id, cluster_name, request_options = {}) ⇒ Object



536
537
538
539
540
541
# File 'lib/algolia/client.rb', line 536

def assign_user_id(user_id, cluster_name, request_options = {})
  request_options = add_header_to_request_options(request_options, { :'X-Algolia-User-ID' => user_id})

  body = { :cluster => cluster_name }
  post(Protocol.cluster_mapping_uri, body.to_json, :write, request_options)
end

#batch(operations, request_options = {}) ⇒ Object

Send a batch request targeting multiple indices



468
469
470
# File 'lib/algolia/client.rb', line 468

def batch(operations, request_options = {})
  post(Protocol.batch_uri, { 'requests' => operations }.to_json, :batch, request_options)
end

#batch!(operations, request_options = {}) ⇒ Object

Send a batch request targeting multiple indices and wait the end of the indexing



475
476
477
478
479
480
# File 'lib/algolia/client.rb', line 475

def batch!(operations, request_options = {})
  res = batch(operations, request_options)
  res['taskID'].each do |index, taskID|
    wait_task(index, taskID, WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
  end
end

#copy_index(src_index, dst_index, scope = nil, request_options = {}) ⇒ Object

Copy an existing index.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName (destination will be overriten if it already exist).

  • scope (defaults to: nil)

    the optional list of scopes to copy (all if not specified).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



203
204
205
206
207
# File 'lib/algolia/client.rb', line 203

def copy_index(src_index, dst_index, scope = nil, request_options = {})
  request = { 'operation' => 'copy', 'destination' => dst_index }
  request['scope'] = scope unless scope.nil?
  post(Protocol.index_operation_uri(src_index), request.to_json, :write, request_options)
end

#copy_index!(src_index, dst_index, scope = nil, request_options = {}) ⇒ Object

Copy an existing index and wait until the copy has been processed.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName (destination will be overriten if it already exist).

  • scope (defaults to: nil)

    the optional list of scopes to copy (all if not specified).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



217
218
219
220
221
# File 'lib/algolia/client.rb', line 217

def copy_index!(src_index, dst_index, scope = nil, request_options = {})
  res = copy_index(src_index, dst_index, scope, request_options)
  wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
  res
end

#copy_rules(src_index, dst_index, request_options = {}) ⇒ Object

Copy an existing index rules.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName’s rules (destination’s rules will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



278
279
280
# File 'lib/algolia/client.rb', line 278

def copy_rules(src_index, dst_index, request_options = {})
  copy_index(src_index, dst_index, ['rules'], request_options)
end

#copy_rules!(src_index, dst_index, request_options = {}) ⇒ Object

Copy an existing index rules and wait until the copy has been processed.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName rules (destination rules will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



289
290
291
292
293
# File 'lib/algolia/client.rb', line 289

def copy_rules!(src_index, dst_index, request_options = {})
  res = copy_rules(src_index, dst_index, request_options)
  wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
  res
end

#copy_settings(src_index, dst_index, request_options = {}) ⇒ Object

Copy an existing index settings.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName’s settings (destination’s settings will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



230
231
232
# File 'lib/algolia/client.rb', line 230

def copy_settings(src_index, dst_index, request_options = {})
  copy_index(src_index, dst_index, ['settings'], request_options)
end

#copy_settings!(src_index, dst_index, request_options = {}) ⇒ Object

Copy an existing index settings and wait until the copy has been processed.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName settings (destination settings will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



241
242
243
244
245
# File 'lib/algolia/client.rb', line 241

def copy_settings!(src_index, dst_index, request_options = {})
  res = copy_settings(src_index, dst_index, request_options)
  wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
  res
end

#copy_synonyms(src_index, dst_index, request_options = {}) ⇒ Object

Copy an existing index synonyms.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName’s synonyms (destination’s synonyms will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



254
255
256
# File 'lib/algolia/client.rb', line 254

def copy_synonyms(src_index, dst_index, request_options = {})
  copy_index(src_index, dst_index, ['synonyms'], request_options)
end

#copy_synonyms!(src_index, dst_index, request_options = {}) ⇒ Object

Copy an existing index synonyms and wait until the copy has been processed.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName synonyms (destination synonyms will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



265
266
267
268
269
# File 'lib/algolia/client.rb', line 265

def copy_synonyms!(src_index, dst_index, request_options = {})
  res = copy_synonyms(src_index, dst_index, request_options)
  wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
  res
end

#delete(uri, type = :write, request_options = {}) ⇒ Object



617
618
619
# File 'lib/algolia/client.rb', line 617

def delete(uri, type = :write, request_options = {})
  request(uri, :DELETE, nil, type, request_options)
end

#delete_api_key(key, request_options = {}) ⇒ Object Also known as: delete_user_key

Delete an existing user key



454
455
456
# File 'lib/algolia/client.rb', line 454

def delete_api_key(key, request_options = {})
  delete(Protocol.key_uri(key), :write, request_options)
end

#delete_index(name, request_options = {}) ⇒ Object

Delete an index

Parameters:

  • name

    the name of the index to delete

  • request_options (defaults to: {})

    contains extra parameters to send with your query



300
301
302
# File 'lib/algolia/client.rb', line 300

def delete_index(name, request_options = {})
  init_index(name).delete(request_options)
end

#delete_index!(name, request_options = {}) ⇒ Object

Delete an index and wait until the deletion has been processed.

Parameters:

  • name

    the name of the index to delete

  • request_options (defaults to: {})

    contains extra parameters to send with your query



309
310
311
# File 'lib/algolia/client.rb', line 309

def delete_index!(name, request_options = {})
  init_index(name).delete!(request_options)
end

#destroyObject



49
50
51
52
53
54
# File 'lib/algolia/client.rb', line 49

def destroy
  Thread.current["algolia_search_hosts_#{application_id}"] = nil
  Thread.current["algolia_hosts_#{application_id}"] = nil
  Thread.current["algolia_host_index_#{application_id}"] = nil
  Thread.current["algolia_search_host_index_#{application_id}"] = nil
end

#disable_rate_limit_forwardObject

Disable IP rate limit enabled with enableRateLimitForward() function



94
95
96
97
98
# File 'lib/algolia/client.rb', line 94

def disable_rate_limit_forward
  headers[Protocol::HEADER_API_KEY] = api_key
  headers.delete(Protocol::HEADER_FORWARDED_IP)
  headers.delete(Protocol::HEADER_FORWARDED_API_KEY)
end

#enable_rate_limit_forward(admin_api_key, end_user_ip, rate_limit_api_key) ⇒ Object

Allow to use IP rate limit when you have a proxy between end-user and Algolia. This option will set the X-Forwarded-For HTTP header with the client IP and the X-Forwarded-API-Key with the API Key having rate limits.

Parameters:

  • admin_api_key

    the admin API Key you can find in your dashboard

  • end_user_ip

    the end user IP (you can use both IPV4 or IPV6 syntax)

  • rate_limit_api_key

    the API key on which you have a rate limit



85
86
87
88
89
# File 'lib/algolia/client.rb', line 85

def enable_rate_limit_forward(admin_api_key, end_user_ip, rate_limit_api_key)
  headers[Protocol::HEADER_API_KEY] = admin_api_key
  headers[Protocol::HEADER_FORWARDED_IP] = end_user_ip
  headers[Protocol::HEADER_FORWARDED_API_KEY] = rate_limit_api_key
end

#get(uri, type = :write, request_options = {}) ⇒ Object



605
606
607
# File 'lib/algolia/client.rb', line 605

def get(uri, type = :write, request_options = {})
  request(uri, :GET, nil, type, request_options)
end

#get_api_key(key, request_options = {}) ⇒ Object Also known as: get_user_key

Get ACL of a user key

Parameters:

  • request_options (defaults to: {})

    contains extra parameters to send with your query



354
355
356
# File 'lib/algolia/client.rb', line 354

def get_api_key(key, request_options = {})
  get(Protocol.key_uri(key), :read, request_options)
end

#get_logs(options = nil, length = nil, type = nil) ⇒ Object

Return last logs entries.

Parameters:

  • options (defaults to: nil)
    • accepts those keys:

    • offset Specify the first entry to retrieve (0-based, 0 is the most recent log entry) - Default = 0

    • length Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000 - Default = 10

    • type Type of log entries to retrieve (“all”, “query”, “build” or “error”) - Default = ‘all’

    • request_options contains extra parameters to send with your query



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/algolia/client.rb', line 322

def get_logs(options = nil, length = nil, type = nil)
  if options.is_a?(Hash)
    offset = options.delete('offset') || options.delete(:offset)
    length = options.delete('length') || options.delete(:length)
    type = options.delete('type') || options.delete(:type)
    request_options = options.delete('request_options') || options.delete(:request_options)
  else
    # Deprecated def get_logs(offset, length, type)
    offset = options
  end
  length ||= 10
  type = 'all' if type.nil?
  type = type ? 'error' : 'all' if type.is_a?(true.class)
  request_options ||= {}

  get(Protocol.logs(offset, length, type), :write, request_options)
end

#get_personalization_strategy(request_options = {}) ⇒ Object



513
514
515
# File 'lib/algolia/client.rb', line 513

def get_personalization_strategy(request_options = {})
  get(Protocol.personalization_strategy_uri, :read, request_options)
end

#get_task_status(index_name, taskID, request_options = {}) ⇒ Object

Check the status of a task on the server. All server task are asynchronous and you can check the status of a task with this method.

Parameters:

  • index_name

    the index name owning the taskID

  • taskID

    the id of the task returned by server

  • request_options (defaults to: {})

    contains extra parameters to send with your query



490
491
492
# File 'lib/algolia/client.rb', line 490

def get_task_status(index_name, taskID, request_options = {})
  get(Protocol.task_uri(index_name, taskID), :read, request_options)['status']
end

#get_top_user_ids(request_options = {}) ⇒ Object



532
533
534
# File 'lib/algolia/client.rb', line 532

def get_top_user_ids(request_options = {})
  get(Protocol.cluster_top_user_uri, :read, request_options)
end

#get_user_id(user_id, request_options = {}) ⇒ Object



543
544
545
# File 'lib/algolia/client.rb', line 543

def get_user_id(user_id, request_options = {})
  get(Protocol.cluster_mapping_uri(user_id), :read, request_options)
end

#init_analyticsObject

Initialize analytics helper



66
67
68
# File 'lib/algolia/client.rb', line 66

def init_analytics()
  Analytics.new(self, { :headers => @headers })
end

#init_index(name) ⇒ Object

Initialize a new index



59
60
61
# File 'lib/algolia/client.rb', line 59

def init_index(name)
  Index.new(name, self)
end

#list_api_keys(request_options = {}) ⇒ Object Also known as: list_user_keys

List all existing user keys with their associated ACLs

Parameters:

  • request_options (defaults to: {})

    contains extra parameters to send with your query



345
346
347
# File 'lib/algolia/client.rb', line 345

def list_api_keys(request_options = {})
  get(Protocol.keys_uri, :read, request_options)
end

#list_clusters(request_options = {}) ⇒ Object

Multicluster management



524
525
526
# File 'lib/algolia/client.rb', line 524

def list_clusters(request_options = {})
  get(Protocol.clusters_uri, :read, request_options)
end

#list_indexes(request_options = {}) ⇒ Object

List all existing indexes return an Answer object with answer in the form

{"items": [{ "name": "contacts", "createdAt": "2013-01-18T15:33:13.556Z"},
           {"name": "notes", "createdAt": "2013-01-18T15:33:13.556Z"}]}

Parameters:

  • request_options (defaults to: {})

    contains extra parameters to send with your query



166
167
168
# File 'lib/algolia/client.rb', line 166

def list_indexes(request_options = {})
  get(Protocol.indexes_uri, :read, request_options)
end

#list_user_ids(page = 0, hits_per_page = 20, request_options = {}) ⇒ Object



528
529
530
# File 'lib/algolia/client.rb', line 528

def list_user_ids(page = 0, hits_per_page = 20, request_options = {})
  get(Protocol.list_ids_uri(page, hits_per_page), :read, request_options)
end

#move_index(src_index, dst_index, request_options = {}) ⇒ Object

Move an existing index.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName (destination will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



177
178
179
180
# File 'lib/algolia/client.rb', line 177

def move_index(src_index, dst_index, request_options = {})
  request = { 'operation' => 'move', 'destination' => dst_index }
  post(Protocol.index_operation_uri(src_index), request.to_json, :write, request_options)
end

#move_index!(src_index, dst_index, request_options = {}) ⇒ Object

Move an existing index and wait until the move has been processed

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

    the new index name that will contains a copy of srcIndexName (destination will be overriten if it already exist).

  • request_options (defaults to: {})

    contains extra parameters to send with your query



189
190
191
192
193
# File 'lib/algolia/client.rb', line 189

def move_index!(src_index, dst_index, request_options = {})
  res = move_index(src_index, dst_index, request_options)
  wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options)
  res
end

#multiple_get_objects(requests, request_options = {}) ⇒ Object

Get objects by objectID across multiple indexes

]

Parameters:

  • requests ({ "indexName" => index_name_1, "objectID" => "obj1" }, { "indexName" => index_name_2, "objectID" => "obj2" })

    equests [ { “indexName” => index_name_1, “objectID” => “obj1” }, { “indexName” => index_name_2, “objectID” => “obj2” }



154
155
156
# File 'lib/algolia/client.rb', line 154

def multiple_get_objects(requests, request_options = {})
  post(Protocol.objects_uri, {:requests => requests}.to_json, :search, request_options)
end

#multiple_queries(queries, options = nil, strategy = nil) ⇒ Object

This method allows to query multiple indexes with one API call

Parameters:

  • queries

    the array of hash representing the query and associated index name

  • options (defaults to: nil)
    • accepts those keys:

    • index_name_key the name of the key used to fetch the index_name (:index_name by default)

    • strategy define the strategy applied on the sequential searches (none by default)

    • request_options contains extra parameters to send with your query



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/algolia/client.rb', line 121

def multiple_queries(queries, options = nil, strategy = nil)
  if options.is_a?(Hash)
    index_name_key = options.delete(:index_name_key) || options.delete('index_name_key')
    strategy = options.delete(:strategy) || options.delete('strategy')
    request_options = options.delete(:request_options) || options.delete('request_options')
  else
    # Deprecated def multiple_queries(queries, index_name_key, strategy)
    index_name_key = options
  end
  index_name_key ||= :index_name
  strategy ||= 'none'
  request_options ||= {}

  requests = {
    :requests => queries.map do |query|
      query = query.dup
      index_name = query.delete(index_name_key) || query.delete(index_name_key.to_s)
      raise ArgumentError.new("Missing '#{index_name_key}' option") if index_name.nil?
      encoded_params = Hash[query.map { |k, v| [k.to_s, v.is_a?(Array) ? v.to_json : v] }]
      { :indexName => index_name, :params => Protocol.to_query(encoded_params) }
    end
  }
  post(Protocol.multiple_queries_uri(strategy), requests.to_json, :search, request_options)
end

#post(uri, body = {}, type = :write, request_options = {}) ⇒ Object



609
610
611
# File 'lib/algolia/client.rb', line 609

def post(uri, body = {}, type = :write, request_options = {})
  request(uri, :POST, body, type, request_options)
end

#put(uri, body = {}, type = :write, request_options = {}) ⇒ Object



613
614
615
# File 'lib/algolia/client.rb', line 613

def put(uri, body = {}, type = :write, request_options = {})
  request(uri, :PUT, body, type, request_options)
end

#remove_user_id(user_id, request_options = {}) ⇒ Object



547
548
549
550
551
# File 'lib/algolia/client.rb', line 547

def remove_user_id(user_id, request_options = {})
  request_options = add_header_to_request_options(request_options, { :'X-Algolia-User-ID' => user_id})

  delete(Protocol.cluster_mapping_uri, :write, request_options)
end

#request(uri, method, data = nil, type = :write, request_options = {}) ⇒ Object

Perform an HTTP request for the given uri and method with common basic response handling. Will raise a AlgoliaProtocolError if the response has an error status code, and will return the parsed JSON body on success, if there is one.



566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/algolia/client.rb', line 566

def request(uri, method, data = nil, type = :write, request_options = {})
  exceptions = []

  connect_timeout = @connect_timeout
  send_timeout = if type == :search
    @search_timeout
  elsif type == :batch
    type = :write
    @batch_timeout
  else
    @send_timeout
  end
  receive_timeout = type == :search ? @search_timeout : @receive_timeout

  thread_local_hosts(type != :write).each_with_index do |host, i|
    connect_timeout += 2 if i == 2
    send_timeout += 10 if i == 2
    receive_timeout += 10 if i == 2

    thread_index_key = type != :write ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
    Thread.current[thread_index_key] = host[:index]
    host[:last_call] = Time.now.to_i

    host[:session].connect_timeout = connect_timeout
    host[:session].send_timeout = send_timeout
    host[:session].receive_timeout = receive_timeout
    begin
      return perform_request(host[:session], host[:base_url] + uri, method, data, request_options)
    rescue AlgoliaProtocolError => e
      raise if e.code / 100 == 4
      exceptions << e
    rescue => e
      exceptions << e
    end
    host[:session].reset_all
  end
  raise AlgoliaProtocolError.new(0, "Cannot reach any host: #{exceptions.map { |e| e.to_s }.join(', ')}")
end

#restore_api_key(key, request_options = {}) ⇒ Object

Restore a deleted api key



461
462
463
# File 'lib/algolia/client.rb', line 461

def restore_api_key(key, request_options = {})
  post(Protocol.restore_key_uri(key), :write, request_options)
end

#search_user_id(query, cluster_name = nil, page = nil, hits_per_page = nil, request_options = {}) ⇒ Object



553
554
555
556
557
558
559
# File 'lib/algolia/client.rb', line 553

def search_user_id(query, cluster_name = nil, page = nil, hits_per_page = nil, request_options = {})
  body = { :query => query }
  body[:cluster] = cluster_name unless cluster_name.nil?
  body[:page] = page unless page.nil?
  body[:hitsPerPage] = hits_per_page unless hits_per_page.nil?
  post(Protocol.search_user_id_uri, body.to_json, :read, request_options)
end

#set_extra_header(key, value) ⇒ Object

Allow to set custom headers



73
74
75
# File 'lib/algolia/client.rb', line 73

def set_extra_header(key, value)
  headers[key] = value
end

#set_personalization_strategy(strategy, request_options = {}) ⇒ Object



517
518
519
# File 'lib/algolia/client.rb', line 517

def set_personalization_strategy(strategy, request_options = {})
  post(Protocol.personalization_strategy_uri, strategy.to_json, :write, request_options)
end

#update_api_key(key, object, request_options = {}, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, indexes = nil) ⇒ Object Also known as: update_user_key

Update a user key

Deprecated call was update_api_key(key, acl, validity, max_queries_per_IP_per_hour, max_hits_per_query, indexes)

ACL can contain an array with those strings:
  - search: allow to search (https and http)
  - addObject: allows to add/update an object in the index (https only)
  - deleteObject : allows to delete an existing object (https only)
  - deleteIndex : allows to delete index content (https only)
  - settings : allows to get index settings (https only)
  - editSettings : allows to change index settings (https only)

@param key API Key to update
@param object The list of parameters for this key.
       Defined by a Hash that can contain the following values:
        - acl: array of string
        - indexes: array of string
        - validity: int
        - referers: array of string
        - description: string
        - maxHitsPerQuery: integer
        - queryParameters: string
        - maxQueriesPerIPPerHour: integer
@param request_options contains extra parameters to send with your query - Default = {}


430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/algolia/client.rb', line 430

def update_api_key(key, object, request_options = {}, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, indexes = nil)
  if object.instance_of?(Array)
    params = { :acl => object }
  else
    params = object
  end

  validity = 0
  unless request_options.is_a?(Hash)
    validity = request_options
    request_options = {}
  end

  params[:indexes] = indexes if indexes
  params['validity'] = validity.to_i if validity != 0
  params['maxQueriesPerIPPerHour'] = max_queries_per_IP_per_hour.to_i if max_queries_per_IP_per_hour != 0
  params['maxHitsPerQuery'] = max_hits_per_query.to_i if max_hits_per_query != 0

  put(Protocol.key_uri(key), params.to_json, :write, request_options)
end

#wait_task(index_name, taskID, time_before_retry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options = {}) ⇒ Object

Wait the publication of a task on the server. All server task are asynchronous and you can check with this method that the task is published.

Parameters:

  • index_name

    the index name owning the taskID

  • taskID

    the id of the task returned by server

  • time_before_retry (defaults to: WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY)

    the time in milliseconds before retry (default = 100ms)

  • request_options (defaults to: {})

    contains extra parameters to send with your query



503
504
505
506
507
508
509
510
511
# File 'lib/algolia/client.rb', line 503

def wait_task(index_name, taskID, time_before_retry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, request_options = {})
  loop do
    status = get_task_status(index_name, taskID, request_options)
    if status == 'published'
      return
    end
    sleep(time_before_retry.to_f / 1000)
  end
end

#with_rate_limits(end_user_ip, rate_limit_api_key, &block) ⇒ Object

Convenience method thats wraps enable_rate_limit_forward/disable_rate_limit_forward



103
104
105
106
107
108
109
110
# File 'lib/algolia/client.rb', line 103

def with_rate_limits(end_user_ip, rate_limit_api_key, &block)
  enable_rate_limit_forward(api_key, end_user_ip, rate_limit_api_key)
  begin
    yield
  ensure
    disable_rate_limit_forward
  end
end