lib/algolia/client.rb in algoliasearch-1.2.5 vs lib/algolia/client.rb in algoliasearch-1.2.7

- old
+ new

@@ -1,10 +1,11 @@ require 'algolia/protocol' require 'algolia/error' require 'algolia/version' require 'json' require 'zlib' +require 'openssl' module Algolia # A class which encapsulates the HTTPS communication with the Algolia # API server. Uses the HTTPClient library for low-level HTTP communication. @@ -109,10 +110,12 @@ api_key = ENV["ALGOLIA_REST_API_KEY"] || ENV['ALGOLIA_API_KEY'] defaulted = { :application_id => application_id, :api_key => api_key } defaulted.merge!(options) + raise ArgumentError.new("No APPLICATION_ID provided, please set :application_id") if defaulted[:application_id].nil? + @@client = Client.new(defaulted) end # # Allow to use IP rate limit when you have a proxy between end-user and Algolia. @@ -159,14 +162,30 @@ def Algolia.generate_secured_api_key(private_api_key, tag_filters, user_token = nil) if tag_filters.is_a?(Array) tag_filters = tag_filters.map { |t| t.is_a?(Array) ? "(#{t.join(',')})" : t }.join(',') end raise ArgumentError.new('Attribute "tag_filters" must be a list of tags') if !tag_filters.is_a?(String) - Digest::SHA256.hexdigest "#{private_api_key}#{tag_filters}#{user_token.to_s}" + OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), private_api_key, "#{tag_filters}#{user_token.to_s}") end # + # This method allows to query multiple indexes with one API call + # + # @param queries the array of hash representing the query and associated index name + # @param index_name_key the name of the key used to fetch the index_name (:index_name by default) + # + 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) } + end + } + Algolia.client.post(Protocol.multiple_queries_uri, requests.to_json) + end + + # # 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"}]} # @@ -198,11 +217,11 @@ # Return last logs entries. # # @param offset Specify the first entry to retrieve (0-based, 0 is the most recent log entry). # @param length Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000. # - def Algolia.get_logs(offset = 0, length = 10) - Algolia.client.get(Protocol.logs(offset, length)) + def Algolia.get_logs(offset = 0, length = 10, only_errors = false) + Algolia.client.get(Protocol.logs(offset, length, only_errors)) end # List all existing user keys with their associated ACLs def Algolia.list_user_keys Algolia.client.get(Protocol.keys_uri)