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.12.4"
@@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


622
623
624
# File 'lib/algolia/client.rb', line 622

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



663
664
665
# File 'lib/algolia/client.rb', line 663

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



668
669
670
# File 'lib/algolia/client.rb', line 668

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

.clientObject



679
680
681
682
683
684
# File 'lib/algolia/client.rb', line 679

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



550
551
552
# File 'lib/algolia/client.rb', line 550

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



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

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

.delete_index(name) ⇒ Object

Delete an index



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

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.



571
572
573
# File 'lib/algolia/client.rb', line 571

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

.delete_user_key(key) ⇒ Object

Delete an existing user key



658
659
660
# File 'lib/algolia/client.rb', line 658

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.



673
674
675
676
677
# File 'lib/algolia/client.rb', line 673

def Algolia.destroy
  @@client.destroy unless @@client.nil?
  @@client = nil
  self
end

.disable_rate_limit_forwardObject

Disable IP rate limit enabled with enableRateLimitForward() function



470
471
472
# File 'lib/algolia/client.rb', line 470

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



463
464
465
# File 'lib/algolia/client.rb', line 463

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_or_params, 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



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/algolia/client.rb', line 489

def Algolia.generate_secured_api_key(private_api_key, tag_filters_or_params, user_token = nil)
  if tag_filters_or_params.is_a?(Hash) && user_token.nil?
    encoded_params = Hash[tag_filters_or_params.map { |k,v| [k.to_s, v.is_a?(Array) ? v.to_json : v] }]
    query_str = Protocol.to_query(encoded_params)
    hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), private_api_key, query_str)
    Base64.encode64("#{hmac}#{query_str}").gsub("\n", '')
  else
    tag_filters = if tag_filters_or_params.is_a?(Array)
      tag_filters = tag_filters_or_params.map { |t| t.is_a?(Array) ? "(#{t.join(',')})" : t }.join(',')
    else
      tag_filters_or_params
    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
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.



581
582
583
# File 'lib/algolia/client.rb', line 581

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



591
592
593
# File 'lib/algolia/client.rb', line 591

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.



439
440
441
442
443
444
445
446
447
# File 'lib/algolia/client.rb', line 439

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


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

def Algolia.list_indexes
  Algolia.client.list_indexes
end

.list_user_keysObject

List all existing user keys with their associated ACLs



586
587
588
# File 'lib/algolia/client.rb', line 586

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



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

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



541
542
543
# File 'lib/algolia/client.rb', line 541

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)



513
514
515
# File 'lib/algolia/client.rb', line 513

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



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

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


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

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



477
478
479
# File 'lib/algolia/client.rb', line 477

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