Class: AlgoliaSearchCredentialChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/credential_checker.rb

Overview

Given an HTML file as input, will return an array of records to index

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AlgoliaSearchCredentialChecker

Returns a new instance of AlgoliaSearchCredentialChecker.



10
11
12
13
# File 'lib/credential_checker.rb', line 10

def initialize(config)
  @config = config
  @logger = AlgoliaSearchErrorHandler.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/credential_checker.rb', line 8

def config
  @config
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/credential_checker.rb', line 8

def logger
  @logger
end

Instance Method Details

#api_keyObject

Read the API key either from ENV or from an _algolia_api_key file in source folder



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/credential_checker.rb', line 17

def api_key
  # First read in ENV
  return ENV['ALGOLIA_API_KEY'] if ENV['ALGOLIA_API_KEY']

  # Otherwise from file in source directory
  key_file = File.join(@config['source'], '_algolia_api_key')
  if File.exist?(key_file) && File.size(key_file) > 0
    return File.open(key_file).read.strip
  end
  nil
end

#application_idObject

Read the application id either from the config file or from ENV



43
44
45
# File 'lib/credential_checker.rb', line 43

def application_id
  get_key('ALGOLIA_APPLICATION_ID', 'application_id')
end

#assert_validObject

Check that all credentials are present Stop with a helpful message if not



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/credential_checker.rb', line 75

def assert_valid
  check_api_key
  check_application_id
  check_index_name

  Algolia.init(
    application_id: application_id,
    api_key: api_key
  )

  nil
end

#check_api_keyObject

Check that the API key is available



53
54
55
56
57
# File 'lib/credential_checker.rb', line 53

def check_api_key
  return if api_key
  @logger.display('api_key_missing')
  exit 1
end

#check_application_idObject

Check that the application id is defined



60
61
62
63
64
# File 'lib/credential_checker.rb', line 60

def check_application_id
  return if application_id
  @logger.display('application_id_missing')
  exit 1
end

#check_index_nameObject

Check that the index name is defined



67
68
69
70
71
# File 'lib/credential_checker.rb', line 67

def check_index_name
  return if index_name
  @logger.display('index_name_missing')
  exit 1
end

#get_key(env_name, config_key) ⇒ Object

Read key either from ENV or _config.yml



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/credential_checker.rb', line 30

def get_key(env_name, config_key)
  # First read in ENV
  return ENV[env_name] if ENV[env_name]

  # Otherwise read from _config.yml
  if @config['algolia'] && @config['algolia'][config_key]
    return @config['algolia'][config_key]
  end

  nil
end

#index_nameObject

Read the index name either from the config file or from ENV



48
49
50
# File 'lib/credential_checker.rb', line 48

def index_name
  get_key('ALGOLIA_INDEX_NAME', 'index_name')
end