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.6.1"
@@client =

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

nil

Class Method Summary collapse

Class Method Details

.add_user_key(obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0, indexes = nil) ⇒ Object

Create a new user key

@param obj can be two different parameters:
      The list of parameters for this key. Defined by a NSDictionary that
      can contains the following values:
        - acl: array of string
        - indices: array of string
        - validity: int
        - referers: array of string
        - description: string
        - maxHitsPerQuery: integer
        - queryParameters: string
        - maxQueriesPerIPPerHour: integer
      Or the list of ACL for this key. Defined by an array of NSString that
      can contains the following values:
        - search: allow to search (https and http)
        - addObject: allows to add/update an object in the index (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)
@param indexes the optional list of targeted indexes


596
597
598
# File 'lib/algolia/client.rb', line 596

def Algolia.add_user_key(obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0, indexes = nil)
  Algolia.client.add_user_key(obj, validity, maxQueriesPerIPPerHour, maxHitsPerQuery, indexes)
end

.batch(requests) ⇒ Object

Send a batch request targeting multiple indices



637
638
639
# File 'lib/algolia/client.rb', line 637

def Algolia.batch(requests)
  Algolia.client.batch(requests)
end

.batch!(requests) ⇒ Object

Send a batch request targeting multiple indices and wait the end of the indexing



642
643
644
# File 'lib/algolia/client.rb', line 642

def Algolia.batch!(requests)
  Algolia.client.batch!(requests)
end

.clientObject



652
653
654
655
656
657
# File 'lib/algolia/client.rb', line 652

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



524
525
526
# File 'lib/algolia/client.rb', line 524

def Algolia.copy_index(src_index, dst_index)
  Algolia.client.copy_index(src_index, dst_index)
end

.copy_index!(src_index, dst_index) ⇒ Object

Copy an existing index and wait until the copy has been processed.

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



533
534
535
# File 'lib/algolia/client.rb', line 533

def Algolia.copy_index!(src_index, dst_index)
  Algolia.client.copy_index!(src_index, dst_index)
end

.delete_index(name) ⇒ Object

Delete an index



539
540
541
# File 'lib/algolia/client.rb', line 539

def Algolia.delete_index(name)
  Algolia.client.delete_index(name)
end

.delete_index!(name) ⇒ Object

Delete an index and wait until the deletion has been processed.



545
546
547
# File 'lib/algolia/client.rb', line 545

def Algolia.delete_index!(name)
  Algolia.client.delete_index!(name)
end

.delete_user_key(key) ⇒ Object

Delete an existing user key



632
633
634
# File 'lib/algolia/client.rb', line 632

def Algolia.delete_user_key(key)
    Algolia.client.delete_user_key(key)
end

.destroyObject

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



647
648
649
650
# File 'lib/algolia/client.rb', line 647

def Algolia.destroy
  @@client = Thread.current[:algolia_hosts] = Thread.current[:algolia_search_hosts] = nil
  self
end

.disable_rate_limit_forwardObject

Disable IP rate limit enabled with enableRateLimitForward() function



453
454
455
# File 'lib/algolia/client.rb', line 453

def Algolia.disable_rate_limit_forward
  Algolia.client.disable_rate_limit_forward
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



446
447
448
# File 'lib/algolia/client.rb', line 446

def Algolia.enable_rate_limit_forward(admin_api_key, end_user_ip, rate_limit_api_key)
  Algolia.client.enable_rate_limit_forward(admin_api_key, end_user_ip, rate_limit_api_key)
end

.generate_secured_api_key(private_api_key, tag_filters, user_token = nil) ⇒ Object

Generate a secured and public API Key from a list of tagFilters and an optional user token identifying the current user

Parameters:

  • private_api_key

    your private API Key

  • tag_filters

    the list of tags applied to the query (used as security)

  • user_token (defaults to: nil)

    an optional token identifying the current user

Raises:

  • (ArgumentError)


472
473
474
475
476
477
478
# File 'lib/algolia/client.rb', line 472

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)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), private_api_key, "#{tag_filters}#{user_token.to_s}")
end

.get_logs(offset = 0, length = 10, type = "all") ⇒ 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.



555
556
557
# File 'lib/algolia/client.rb', line 555

def Algolia.get_logs(offset = 0, length = 10, type = "all")
  Algolia.client.get_logs(offset, length, type)
end

.get_user_key(key) ⇒ Object

Get ACL of a user key



565
566
567
# File 'lib/algolia/client.rb', line 565

def Algolia.get_user_key(key)
  Algolia.client.get_user_key(key)
end

.init(options = {}) ⇒ Object

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



422
423
424
425
426
427
428
429
430
# File 'lib/algolia/client.rb', line 422

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


497
498
499
# File 'lib/algolia/client.rb', line 497

def Algolia.list_indexes
  Algolia.client.list_indexes
end

.list_user_keysObject

List all existing user keys with their associated ACLs



560
561
562
# File 'lib/algolia/client.rb', line 560

def Algolia.list_user_keys
  Algolia.client.list_user_keys
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).



506
507
508
# File 'lib/algolia/client.rb', line 506

def Algolia.move_index(src_index, dst_index)
  Algolia.client.move_index(src_index, dst_index)
end

.move_index!(src_index, dst_index) ⇒ Object

Move an existing index and wait until the move has been processed

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



515
516
517
# File 'lib/algolia/client.rb', line 515

def Algolia.move_index!(src_index, dst_index)
  Algolia.client.move_index!(src_index, dst_index)
end

.multiple_queries(queries, index_name_key = :index_name, strategy = "none") ⇒ Object

This method allows to query multiple indexes with one API call

Parameters:

  • queries

    the array of hash representing the query and associated index name

  • index_name_key (defaults to: :index_name)

    the name of the key used to fetch the index_name (:index_name by default)

  • strategy (defaults to: "none")

    define the strategy applied on the sequential searches (none by default)



487
488
489
# File 'lib/algolia/client.rb', line 487

def Algolia.multiple_queries(queries, index_name_key = :index_name, strategy = "none")
  Algolia.client.multiple_queries(queries, index_name_key, strategy)
end

.set_extra_header(key, value) ⇒ Object

Allow to set custom headers



435
436
437
# File 'lib/algolia/client.rb', line 435

def Algolia.set_extra_header(key, value)
  Algolia.client.set_extra_header(key, value)
end

.update_user_key(key, obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0, indexes = nil) ⇒ Object

Update a user key

@param obj can be two different parameters:
      The list of parameters for this key. Defined by a NSDictionary that
      can contains the following values:
        - acl: array of string
        - indices: array of string
        - validity: int
        - referers: array of string
        - description: string
        - maxHitsPerQuery: integer
        - queryParameters: string
        - maxQueriesPerIPPerHour: integer
      Or the list of ACL for this key. Defined by an array of NSString that
      can contains the following values:
        - search: allow to search (https and http)
        - addObject: allows to add/update an object in the index (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)
@param indexes the optional list of targeted indexes


627
628
629
# File 'lib/algolia/client.rb', line 627

def Algolia.update_user_key(key, obj, validity = 0, maxQueriesPerIPPerHour = 0, maxHitsPerQuery = 0, indexes = nil)
  Algolia.client.update_user_key(key, obj, validity, maxQueriesPerIPPerHour, maxHitsPerQuery, indexes)
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



460
461
462
# File 'lib/algolia/client.rb', line 460

def Algolia.with_rate_limits(end_user_ip, rate_limit_api_key, &block)
  Algolia.client.with_rate_limits(end_user_ip, rate_limit_api_key, &block)
end