Class: AlgoliaSearch::SafeIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/algoliasearch-rails.rb

Overview

this class wraps an Algolia::Index object ensuring all raised exceptions are correctly logged or thrown depending on the ‘raise_on_failure` option

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, raise_on_failure) ⇒ SafeIndex

Returns a new instance of SafeIndex.



293
294
295
296
# File 'lib/algoliasearch-rails.rb', line 293

def initialize(name, raise_on_failure)
  @index = AlgoliaSearch.client.init_index(name)
  @raise_on_failure = raise_on_failure.nil? || raise_on_failure
end

Class Method Details

.move_index(old_name, new_name) ⇒ Object

expose move as well



327
328
329
330
331
# File 'lib/algoliasearch-rails.rb', line 327

def self.move_index(old_name, new_name)
  SafeIndex.log_or_throw(:move_index, true) do
    AlgoliaSearch.client.move_index(old_name, new_name)
  end
end

Instance Method Details

#get_settings(*args) ⇒ Object

special handling of get_settings to avoid raising errors on 404



315
316
317
318
319
320
321
322
323
324
# File 'lib/algoliasearch-rails.rb', line 315

def get_settings(*args)
  SafeIndex.log_or_throw(:get_settings, @raise_on_failure) do
    begin
      @index.get_settings(*args)
    rescue Algolia::AlgoliaHttpError => e
      return {} if e.code == 404 # not fatal
      raise e
    end
  end
end

#wait_task(task_id) ⇒ Object

special handling of wait_task to handle null task_id



307
308
309
310
311
312
# File 'lib/algoliasearch-rails.rb', line 307

def wait_task(task_id)
  return if task_id.nil? && !@raise_on_failure # ok
  SafeIndex.log_or_throw(:wait_task, @raise_on_failure) do
    @index.wait_task(task_id)
  end
end