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.18"
@@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)


231
232
233
# File 'lib/algolia/client.rb', line 231

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



246
247
248
249
250
251
# File 'lib/algolia/client.rb', line 246

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:

  • src_index

    the name of index to copy.

  • dst_index

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



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

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



236
237
238
# File 'lib/algolia/client.rb', line 236

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.



241
242
243
244
# File 'lib/algolia/client.rb', line 241

def Algolia.destroy
  @@client = nil
  self
end

.disable_rate_limit_forwardObject

Disable IP rate limit enabled with enableRateLimitForward() function



144
145
146
147
148
149
150
151
# File 'lib/algolia/client.rb', line 144

def Algolia.disable_rate_limit_forward
  Algolia.client.thread_local_hosts.each do |host|
    session = host["session"]
    session.headers[Protocol::HEADER_API_KEY] = Algolia.client.api_key
    session.headers.delete(Protocol::HEADER_FORWARDED_IP)
    session.headers.delete(Protocol::HEADER_FORWARDED_API_KEY)
  end
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.

Parameters:

  • admin_api_key

    the admin API Key you can find in your dashboard

  • end_user_ip

    the end user IP (you can use both IPV4 or IPV6 syntax)

  • rate_limit_api_key

    the API key on which you have a rate limit



132
133
134
135
136
137
138
139
# File 'lib/algolia/client.rb', line 132

def Algolia.enable_rate_limit_forward(admin_api_key, end_user_ip, rate_limit_api_key)
  Algolia.client.thread_local_hosts.each do |host|
    session = host["session"]
    session.headers[Protocol::HEADER_API_KEY] = admin_api_key
    session.headers[Protocol::HEADER_FORWARDED_IP] = end_user_ip
    session.headers[Protocol::HEADER_FORWARDED_API_KEY] = rate_limit_api_key
  end
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.



201
202
203
# File 'lib/algolia/client.rb', line 201

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



211
212
213
# File 'lib/algolia/client.rb', line 211

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.



115
116
117
118
119
120
121
122
123
# File 'lib/algolia/client.rb', line 115

def Algolia.init(options = {})
  application_id = ENV["ALGOLIA_API_ID"] || ENV["ALGOLIA_APPLICATION_ID"]
  api_key = ENV["ALGOLIA_REST_API_KEY"] || ENV['ALGOLIA_API_KEY']

  defaulted = { :application_id => application_id, :api_key => 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"}]}


171
172
173
# File 'lib/algolia/client.rb', line 171

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

.list_user_keysObject

List all existing user keys with their associated ACLs



206
207
208
# File 'lib/algolia/client.rb', line 206

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

.move_index(src_index, dst_index) ⇒ Object

Move an existing index.

Parameters:

  • src_index

    the name of index to copy.

  • dst_index

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



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

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

.with_rate_limits(end_user_ip, rate_limit_api_key, &block) ⇒ Object

Convenience method thats wraps enable_rate_limit_forward/disable_rate_limit_forward



156
157
158
159
160
161
162
163
# File 'lib/algolia/client.rb', line 156

def Algolia.with_rate_limits(end_user_ip, rate_limit_api_key, &block)
  Algolia.enable_rate_limit_forward(Algolia.client.api_key, end_user_ip, rate_limit_api_key)
  begin
    yield
  ensure
    Algolia.disable_rate_limit_forward
  end
end