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

- old
+ new

@@ -147,10 +147,26 @@ Algolia.disable_rate_limit_forward end end # + # Generate a secured and public API Key from a list of tagFilters and an + # optional user token identifying the current user + # + # @param private_api_key your private API Key + # @param tag_filters the list of tags applied to the query (used as security) + # @param user_token an optional token identifying the current user + # + 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}" + 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"}]} # @@ -211,12 +227,20 @@ # - settings : allows to get index settings (https only) # - editSettings : allows to change index settings (https only) # @param validity the number of seconds after which the key will be automatically removed (0 means no time limit for this key) # @param maxQueriesPerIPPerHour the maximum number of API calls allowed from an IP address per hour (0 means unlimited) # @param maxHitsPerQuery the maximum number of hits this API key can retrieve in one call (0 means unlimited) + # @param indexes the optional list of targeted indexes # - def Algolia.add_user_key(acls, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0) - Algolia.client.post(Protocol.keys_uri, {"acl" => acls, "validity" => validity.to_i, "maxQueriesPerIPPerHour" => maxQueriesPerIPPerHour.to_i, "maxHitsPerQuery" => maxHitsPerQuery.to_i}.to_json) + def Algolia.add_user_key(acls, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0, indexes = nil) + params = { + :acl => acls, + :validity => validity.to_i, + :maxQueriesPerIPPerHour => maxQueriesPerIPPerHour.to_i, + :maxHitsPerQuery => maxHitsPerQuery.to_i + } + params[:indexes] = indexes if indexes + Algolia.client.post(Protocol.keys_uri, params.to_json) end # Delete an existing user key def Algolia.delete_user_key(key) Algolia.client.delete(Protocol.key_uri(key))