Class: Algolia::Client
- Inherits:
-
Object
- Object
- Algolia::Client
- 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
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#application_id ⇒ Object
readonly
Returns the value of attribute application_id.
-
#batch_timeout ⇒ Object
readonly
Returns the value of attribute batch_timeout.
-
#connect_timeout ⇒ Object
readonly
Returns the value of attribute connect_timeout.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#receive_timeout ⇒ Object
readonly
Returns the value of attribute receive_timeout.
-
#search_hosts ⇒ Object
readonly
Returns the value of attribute search_hosts.
-
#search_timeout ⇒ Object
readonly
Returns the value of attribute search_timeout.
-
#send_timeout ⇒ Object
readonly
Returns the value of attribute send_timeout.
-
#ssl ⇒ Object
readonly
Returns the value of attribute ssl.
-
#ssl_version ⇒ Object
readonly
Returns the value of attribute ssl_version.
Instance Method Summary collapse
-
#add_api_key(object, request_options = {}, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, indexes = nil) ⇒ Object
(also: #add_user_key)
Create a new user key.
- #assign_user_id(user_id, cluster_name, request_options = {}) ⇒ Object
-
#batch(operations, request_options = {}) ⇒ Object
Send a batch request targeting multiple indices.
-
#batch!(operations, request_options = {}) ⇒ Object
Send a batch request targeting multiple indices and wait the end of the indexing.
-
#copy_index(src_index, dst_index, scope = nil, request_options = {}) ⇒ Object
Copy an existing index.
-
#copy_index!(src_index, dst_index, scope = nil, request_options = {}) ⇒ Object
Copy an existing index and wait until the copy has been processed.
-
#copy_rules(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index rules.
-
#copy_rules!(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index rules and wait until the copy has been processed.
-
#copy_settings(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index settings.
-
#copy_settings!(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index settings and wait until the copy has been processed.
-
#copy_synonyms(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index synonyms.
-
#copy_synonyms!(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index synonyms and wait until the copy has been processed.
- #delete(uri, type = :write, request_options = {}) ⇒ Object
-
#delete_api_key(key, request_options = {}) ⇒ Object
(also: #delete_user_key)
Delete an existing user key.
-
#delete_index(name, request_options = {}) ⇒ Object
Delete an index.
-
#delete_index!(name, request_options = {}) ⇒ Object
Delete an index and wait until the deletion has been processed.
- #destroy ⇒ Object
-
#disable_rate_limit_forward ⇒ Object
Disable IP rate limit enabled with enableRateLimitForward() function.
-
#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.
- #get(uri, type = :write, request_options = {}) ⇒ Object
-
#get_api_key(key, request_options = {}) ⇒ Object
(also: #get_user_key)
Get ACL of a user key.
-
#get_logs(options = nil, length = nil, type = nil) ⇒ Object
Return last logs entries.
- #get_personalization_strategy(request_options = {}) ⇒ Object
-
#get_task_status(index_name, taskID, request_options = {}) ⇒ Object
Check the status of a task on the server.
- #get_top_user_ids(request_options = {}) ⇒ Object
- #get_user_id(user_id, request_options = {}) ⇒ Object
-
#init_analytics ⇒ Object
Initialize analytics helper.
-
#init_index(name) ⇒ Object
Initialize a new index.
-
#initialize(data = {}) ⇒ Client
constructor
A new instance of Client.
-
#list_api_keys(request_options = {}) ⇒ Object
(also: #list_user_keys)
List all existing user keys with their associated ACLs.
-
#list_clusters(request_options = {}) ⇒ Object
Multicluster management.
-
#list_indexes(request_options = {}) ⇒ Object
List all existing indexes return an Answer object with answer in the form [{ “name”: “contacts”, “createdAt”: “2013-01-18T15:33:13.556Z”, “notes”, “createdAt”: “2013-01-18T15:33:13.556Z”]}.
- #list_user_ids(page = 0, hits_per_page = 20, request_options = {}) ⇒ Object
-
#move_index(src_index, dst_index, request_options = {}) ⇒ Object
Move an existing index.
-
#move_index!(src_index, dst_index, request_options = {}) ⇒ Object
Move an existing index and wait until the move has been processed.
-
#multiple_get_objects(requests, request_options = {}) ⇒ Object
Get objects by objectID across multiple indexes.
-
#multiple_queries(queries, options = nil, strategy = nil) ⇒ Object
This method allows to query multiple indexes with one API call.
- #post(uri, body = {}, type = :write, request_options = {}) ⇒ Object
- #put(uri, body = {}, type = :write, request_options = {}) ⇒ Object
- #remove_user_id(user_id, request_options = {}) ⇒ Object
-
#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.
-
#restore_api_key(key, request_options = {}) ⇒ Object
Restore a deleted api key.
- #search_user_id(query, cluster_name = nil, page = nil, hits_per_page = nil, request_options = {}) ⇒ Object
-
#set_extra_header(key, value) ⇒ Object
Allow to set custom headers.
- #set_personalization_strategy(strategy, request_options = {}) ⇒ Object
-
#update_api_key(key, object, request_options = {}, max_queries_per_IP_per_hour = 0, max_hits_per_query = 0, indexes = nil) ⇒ Object
(also: #update_user_key)
Update a user key.
-
#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.
-
#with_rate_limits(end_user_ip, rate_limit_api_key, &block) ⇒ Object
Convenience method thats wraps enable_rate_limit_forward/disable_rate_limit_forward.
Constructor Details
#initialize(data = {}) ⇒ Client
Returns a new instance of Client.
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_key ⇒ Object (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_id ⇒ Object (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_timeout ⇒ Object (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_timeout ⇒ Object (readonly)
Returns the value of attribute connect_timeout.
17 18 19 |
# File 'lib/algolia/client.rb', line 17 def connect_timeout @connect_timeout end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
17 18 19 |
# File 'lib/algolia/client.rb', line 17 def headers @headers end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
17 18 19 |
# File 'lib/algolia/client.rb', line 17 def hosts @hosts end |
#receive_timeout ⇒ Object (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_hosts ⇒ Object (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_timeout ⇒ Object (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_timeout ⇒ Object (readonly)
Returns the value of attribute send_timeout.
17 18 19 |
# File 'lib/algolia/client.rb', line 17 def send_timeout @send_timeout end |
#ssl ⇒ Object (readonly)
Returns the value of attribute ssl.
17 18 19 |
# File 'lib/algolia/client.rb', line 17 def ssl @ssl end |
#ssl_version ⇒ Object (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, = {}, 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 .is_a?(Hash) validity = = {} 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, ) 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, = {}) = (, { :'X-Algolia-User-ID' => user_id}) body = { :cluster => cluster_name } post(Protocol.cluster_mapping_uri, body.to_json, :write, ) 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, = {}) post(Protocol.batch_uri, { 'requests' => operations }.to_json, :batch, ) 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, = {}) res = batch(operations, ) res['taskID'].each do |index, taskID| wait_task(index, taskID, WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, ) end end |
#copy_index(src_index, dst_index, scope = nil, request_options = {}) ⇒ Object
Copy an existing index.
203 204 205 206 207 |
# File 'lib/algolia/client.rb', line 203 def copy_index(src_index, dst_index, scope = nil, = {}) request = { 'operation' => 'copy', 'destination' => dst_index } request['scope'] = scope unless scope.nil? post(Protocol.index_operation_uri(src_index), request.to_json, :write, ) end |
#copy_index!(src_index, dst_index, scope = nil, request_options = {}) ⇒ Object
Copy an existing index and wait until the copy has been processed.
217 218 219 220 221 |
# File 'lib/algolia/client.rb', line 217 def copy_index!(src_index, dst_index, scope = nil, = {}) res = copy_index(src_index, dst_index, scope, ) wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, ) res end |
#copy_rules(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index rules.
278 279 280 |
# File 'lib/algolia/client.rb', line 278 def copy_rules(src_index, dst_index, = {}) copy_index(src_index, dst_index, ['rules'], ) end |
#copy_rules!(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index rules and wait until the copy has been processed.
289 290 291 292 293 |
# File 'lib/algolia/client.rb', line 289 def copy_rules!(src_index, dst_index, = {}) res = copy_rules(src_index, dst_index, ) wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, ) res end |
#copy_settings(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index settings.
230 231 232 |
# File 'lib/algolia/client.rb', line 230 def copy_settings(src_index, dst_index, = {}) copy_index(src_index, dst_index, ['settings'], ) end |
#copy_settings!(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index settings and wait until the copy has been processed.
241 242 243 244 245 |
# File 'lib/algolia/client.rb', line 241 def copy_settings!(src_index, dst_index, = {}) res = copy_settings(src_index, dst_index, ) wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, ) res end |
#copy_synonyms(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index synonyms.
254 255 256 |
# File 'lib/algolia/client.rb', line 254 def copy_synonyms(src_index, dst_index, = {}) copy_index(src_index, dst_index, ['synonyms'], ) end |
#copy_synonyms!(src_index, dst_index, request_options = {}) ⇒ Object
Copy an existing index synonyms and wait until the copy has been processed.
265 266 267 268 269 |
# File 'lib/algolia/client.rb', line 265 def copy_synonyms!(src_index, dst_index, = {}) res = copy_synonyms(src_index, dst_index, ) wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, ) 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(uri, :DELETE, nil, type, ) 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, = {}) delete(Protocol.key_uri(key), :write, ) end |
#delete_index(name, request_options = {}) ⇒ Object
Delete an index
300 301 302 |
# File 'lib/algolia/client.rb', line 300 def delete_index(name, = {}) init_index(name).delete() end |
#delete_index!(name, request_options = {}) ⇒ Object
Delete an index and wait until the deletion has been processed.
309 310 311 |
# File 'lib/algolia/client.rb', line 309 def delete_index!(name, = {}) init_index(name).delete!() end |
#destroy ⇒ Object
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_forward ⇒ Object
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.
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(uri, :GET, nil, type, ) end |
#get_api_key(key, request_options = {}) ⇒ Object Also known as: get_user_key
Get ACL of a user key
354 355 356 |
# File 'lib/algolia/client.rb', line 354 def get_api_key(key, = {}) get(Protocol.key_uri(key), :read, ) end |
#get_logs(options = nil, length = nil, type = nil) ⇒ Object
Return last logs entries.
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( = nil, length = nil, type = nil) if .is_a?(Hash) offset = .delete('offset') || .delete(:offset) length = .delete('length') || .delete(:length) type = .delete('type') || .delete(:type) = .delete('request_options') || .delete(:request_options) else # Deprecated def get_logs(offset, length, type) offset = end length ||= 10 type = 'all' if type.nil? type = type ? 'error' : 'all' if type.is_a?(true.class) ||= {} get(Protocol.logs(offset, length, type), :write, ) end |
#get_personalization_strategy(request_options = {}) ⇒ Object
513 514 515 |
# File 'lib/algolia/client.rb', line 513 def get_personalization_strategy( = {}) get(Protocol.personalization_strategy_uri, :read, ) 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.
490 491 492 |
# File 'lib/algolia/client.rb', line 490 def get_task_status(index_name, taskID, = {}) get(Protocol.task_uri(index_name, taskID), :read, )['status'] end |
#get_top_user_ids(request_options = {}) ⇒ Object
532 533 534 |
# File 'lib/algolia/client.rb', line 532 def get_top_user_ids( = {}) get(Protocol.cluster_top_user_uri, :read, ) 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, = {}) get(Protocol.cluster_mapping_uri(user_id), :read, ) end |
#init_analytics ⇒ Object
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
345 346 347 |
# File 'lib/algolia/client.rb', line 345 def list_api_keys( = {}) get(Protocol.keys_uri, :read, ) end |
#list_clusters(request_options = {}) ⇒ Object
Multicluster management
524 525 526 |
# File 'lib/algolia/client.rb', line 524 def list_clusters( = {}) get(Protocol.clusters_uri, :read, ) 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"}]}
166 167 168 |
# File 'lib/algolia/client.rb', line 166 def list_indexes( = {}) get(Protocol.indexes_uri, :read, ) 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, = {}) get(Protocol.list_ids_uri(page, hits_per_page), :read, ) end |
#move_index(src_index, dst_index, request_options = {}) ⇒ Object
Move an existing index.
177 178 179 180 |
# File 'lib/algolia/client.rb', line 177 def move_index(src_index, dst_index, = {}) request = { 'operation' => 'move', 'destination' => dst_index } post(Protocol.index_operation_uri(src_index), request.to_json, :write, ) end |
#move_index!(src_index, dst_index, request_options = {}) ⇒ Object
Move an existing index and wait until the move has been processed
189 190 191 192 193 |
# File 'lib/algolia/client.rb', line 189 def move_index!(src_index, dst_index, = {}) res = move_index(src_index, dst_index, ) wait_task(dst_index, res['taskID'], WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, ) res end |
#multiple_get_objects(requests, request_options = {}) ⇒ Object
Get objects by objectID across multiple indexes
]
154 155 156 |
# File 'lib/algolia/client.rb', line 154 def multiple_get_objects(requests, = {}) post(Protocol.objects_uri, {:requests => requests}.to_json, :search, ) end |
#multiple_queries(queries, options = nil, strategy = nil) ⇒ Object
This method allows to query multiple indexes with one API call
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, = nil, strategy = nil) if .is_a?(Hash) index_name_key = .delete(:index_name_key) || .delete('index_name_key') strategy = .delete(:strategy) || .delete('strategy') = .delete(:request_options) || .delete('request_options') else # Deprecated def multiple_queries(queries, index_name_key, strategy) index_name_key = end index_name_key ||= :index_name strategy ||= 'none' ||= {} 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, ) 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(uri, :POST, body, type, ) 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(uri, :PUT, body, type, ) 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, = {}) = (, { :'X-Algolia-User-ID' => user_id}) delete(Protocol.cluster_mapping_uri, :write, ) 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, = {}) 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, ) 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, = {}) post(Protocol.restore_key_uri(key), :write, ) 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, = {}) 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, ) 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, = {}) post(Protocol.personalization_strategy_uri, strategy.to_json, :write, ) 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, = {}, 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 .is_a?(Hash) validity = = {} 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, ) 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.
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, = {}) loop do status = get_task_status(index_name, taskID, ) 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 |