Module: Algolia

Defined in:
lib/algolia/error.rb,
lib/algolia/index.rb,
lib/algolia/client.rb,
lib/algolia/version.rb,
lib/algolia/protocol.rb

Defined Under Namespace

Modules: Protocol Classes: AlgoliaError, AlgoliaProtocolError, Client, Index

Constant Summary collapse

VERSION =
"1.1.6"
@@client =

A singleton client Always use Algolia.client to retrieve the client object.

nil

Class Method Summary collapse

Class Method Details

.add_user_key(acls, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0) ⇒ Object

Create a new user key

@param acls the list of ACL for this key. Defined by an array of strings that 
       can contains the following values:
         - search: allow to search (https and http)
         - addObject: allows to add a new object in the index (https only)
         - updateObject : allows to change content of an existing object (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 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)


181
182
183
# File 'lib/algolia/client.rb', line 181

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)
end

.clientObject



196
197
198
199
200
201
# File 'lib/algolia/client.rb', line 196

def Algolia.client
  if !@@client
    raise AlgoliaError, "API not initialized"
  end
  @@client
end

.copy_index(src_index, dst_index) ⇒ Object

Copy an existing index.

Parameters:

  • srcIndexName

    the name of index to copy.

  • dstIndexName

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



140
141
142
143
# File 'lib/algolia/client.rb', line 140

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

.delete_user_key(key) ⇒ Object

Delete an existing user key



186
187
188
# File 'lib/algolia/client.rb', line 186

def Algolia.delete_user_key(key)
    Algolia.client.delete(Protocol.key_uri(key))
end

.destroyObject

Used mostly for testing. Lets you delete the api key global vars.



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

def Algolia.destroy
  @@client = nil
  self
end

.get_logs(offset = 0, length = 10) ⇒ Object

Return last logs entries.

Parameters:

  • offset (defaults to: 0)

    Specify the first entry to retrieve (0-based, 0 is the most recent log entry).

  • length (defaults to: 10)

    Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000.



151
152
153
# File 'lib/algolia/client.rb', line 151

def Algolia.get_logs(offset = 0, length = 10)
    Algolia.client.get(Protocol.logs(offset, length))
end

.get_user_key(key) ⇒ Object

Get ACL of a user key



161
162
163
# File 'lib/algolia/client.rb', line 161

def Algolia.get_user_key(key)
    Algolia.client.get(Protocol.key_uri(key))
end

.init(options = {}) ⇒ Object

Initialize the singleton instance of Client which is used by all API methods.



108
109
110
111
112
113
# File 'lib/algolia/client.rb', line 108

def Algolia.init(options = {})
  defaulted = { :api_id => ENV["ALGOLIA_API_ID"], :api_key => ENV["ALGOLIA_REST_API_KEY"] }
  defaulted.merge!(options)

  @@client = Client.new(defaulted)
end

.list_indexesObject

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"}]}


121
122
123
# File 'lib/algolia/client.rb', line 121

def Algolia.list_indexes
    Algolia.client.get(Protocol.indexes_uri)
end

.list_user_keysObject

List all existing user keys with their associated ACLs



156
157
158
# File 'lib/algolia/client.rb', line 156

def Algolia.list_user_keys
    Algolia.client.get(Protocol.keys_uri)
end

.move_index(src_index, dst_index) ⇒ Object

Move an existing index.

Parameters:

  • srcIndexName

    the name of index to copy.

  • dstIndexName

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



130
131
132
133
# File 'lib/algolia/client.rb', line 130

def Algolia.move_index(src_index, dst_index)
    request = {"operation" => "move", "destination" => dst_index};
    Algolia.client.post(Protocol.index_operation_uri(src_index), request.to_json)
end