lib/algolia/client.rb in algoliasearch-1.2.8 vs lib/algolia/client.rb in algoliasearch-1.2.9
- old
+ new
@@ -8,18 +8,21 @@
module Algolia
# A class which encapsulates the HTTPS communication with the Algolia
# API server. Uses the HTTPClient library for low-level HTTP communication.
class Client
- attr_reader :hosts, :application_id, :api_key, :headers
+ attr_reader :ssl, :hosts, :application_id, :api_key, :headers, :connect_timeout, :send_timeout, :receive_timeout
def initialize(data = {})
- @ssl = data[:ssl].nil? ? true : data[:ssl]
- @application_id = data[:application_id]
- @api_key = data[:api_key]
- @hosts = (data[:hosts] || 1.upto(3).map { |i| "#{@application_id}-#{i}.algolia.io" }).shuffle
+ @ssl = data[:ssl].nil? ? true : data[:ssl]
+ @application_id = data[:application_id]
+ @api_key = data[:api_key]
+ @hosts = (data[:hosts] || 1.upto(3).map { |i| "#{@application_id}-#{i}.algolia.io" }).shuffle
+ @connect_timeout = data[:connect_timeout]
+ @send_timeout = data[:send_timeout]
+ @receive_timeout = data[:receive_timeout]
@headers = {
Protocol::HEADER_API_KEY => api_key,
Protocol::HEADER_APP_ID => application_id,
'Content-Type' => 'application/json; charset=utf-8',
'User-Agent' => "Algolia for Ruby #{::Algolia::VERSION}"
@@ -69,10 +72,13 @@
hinfo = {
:base_url => "http#{@ssl ? 's' : ''}://#{host}",
:session => HTTPClient.new
}
hinfo[:session].transparent_gzip_decompression = true
+ hinfo[:session].connect_timeout = @connect_timeout if @connect_timeout
+ hinfo[:session].send_timeout = @send_timeout if @send_timeout
+ hinfo[:session].receive_timeout = @receive_timeout if @receive_timeout
hinfo[:session].ssl_config.add_trust_ca File.join(File.dirname(__FILE__), '..', '..', 'resources', 'ca-bundle.crt')
hinfo
end
end
@@ -175,11 +181,12 @@
#
def Algolia.multiple_queries(queries, index_name_key = :index_name)
requests = {
:requests => queries.map do |query|
indexName = query.delete(index_name_key) || query.delete(index_name_key.to_s)
- { :indexName => indexName, :params => Protocol.to_query(query) }
+ encoded_params = Hash[query.map { |k,v| [k.to_s, v.is_a?(Array) ? v.to_json : v] }]
+ { :indexName => indexName, :params => Protocol.to_query(encoded_params) }
end
}
Algolia.client.post(Protocol.multiple_queries_uri, requests.to_json)
end
@@ -202,16 +209,50 @@
request = {"operation" => "move", "destination" => dst_index};
Algolia.client.post(Protocol.index_operation_uri(src_index), request.to_json)
end
#
+ # Move an existing index and wait until the move has been processed
+ # @param src_index the name of index to copy.
+ # @param dst_index the new index name that will contains a copy of srcIndexName (destination will be overriten if it already exist).
+ #
+ def Algolia.move_index!(src_index, dst_index)
+ res = Algolia.move_index(src_index, dst_index)
+ Index.new(dst_index).wait_task(res['taskID'])
+ res
+ end
+
+ #
# Copy an existing index.
# @param src_index the name of index to copy.
# @param dst_index the new index name that will contains a copy of srcIndexName (destination will be overriten if it already exist).
#
def Algolia.copy_index(src_index, dst_index)
request = {"operation" => "copy", "destination" => dst_index};
Algolia.client.post(Protocol.index_operation_uri(src_index), request.to_json)
+ end
+
+ #
+ # Copy an existing index and wait until the copy has been processed.
+ # @param src_index the name of index to copy.
+ # @param dst_index the new index name that will contains a copy of srcIndexName (destination will be overriten if it already exist).
+ #
+ def Algolia.copy_index!(src_index, dst_index)
+ res = Algolia.copy_index(src_index, dst_index)
+ Index.new(dst_index).wait_task(res['taskID'])
+ res
+ end
+
+ # Delete an index
+ #
+ def delete_index(name)
+ Index.new(name).delete
+ end
+
+ # Delete an index and wait until the deletion has been processed.
+ #
+ def delete_index!(name)
+ Index.new(name).delete!
end
#
# Return last logs entries.
#