Module: Settings

Defined in:
lib/settings.rb

Overview

Server/application configuration settings

Defined Under Namespace

Classes: Error

Constant Summary collapse

EXCEPTION_NOTIFICATION_SETTINGS =
[
  :email_prefix,
  :sender_address,
  :exception_recipients
].freeze
VALID_SECTIONS =
[
  :backup_directory,
  :default_data_directory,
  :exception_notification,
  :action_mailer_smtp_settings,
  :action_mailer_url_host,
  :mail_domain,
  :capistrano,
  :interface,
  :selenium
].freeze
@@backup_directory =
nil
@@default_data_directory =
nil
@@mail_domain =
nil
@@config_hash =
nil
@@sandbox_mode =
false
@@sandbox_commit_sha =
nil
@@sandbox_short_commit_sha =
nil
@@sandbox_commit_date =
nil
@@selenium_settings =
{}

Class Method Summary collapse

Class Method Details

.backup_directoryString

Returns:

  • (String)


86
87
88
# File 'lib/settings.rb', line 86

def self.backup_directory
  @@backup_directory
end

.default_data_directoryString

Returns:

  • (String)


81
82
83
# File 'lib/settings.rb', line 81

def self.default_data_directory
  @@default_data_directory
end

.get_config_hashHash

Returns:

  • (Hash)


57
58
59
# File 'lib/settings.rb', line 57

def self.get_config_hash
  @@config_hash
end

.load_action_mailer_smtp_settings(config, settings) ⇒ Hash

Parameters:

  • config (Hash)
  • settings (Hash)

Returns:

  • (Hash)


207
208
209
210
211
212
# File 'lib/settings.rb', line 207

def self.load_action_mailer_smtp_settings(config, settings)
  if settings
    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = settings
  end
end

.load_action_mailer_url_host(config, url_host) ⇒ Boolean

Parameters:

  • config (Hash)
  • url_host (String)

Returns:

  • (Boolean)


217
218
219
220
221
# File 'lib/settings.rb', line 217

def self.load_action_mailer_url_host(config, url_host)
  if url_host
    config.action_mailer.default_url_options = { host: url_host }
  end
end

.load_backup_directory(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


143
144
145
146
147
148
149
150
# File 'lib/settings.rb', line 143

def self.load_backup_directory(path)
  @@backup_directory = nil
  if !path.nil?
    full_path = File.absolute_path(path)
    setup_directory(full_path)
    @@backup_directory = full_path
  end
end

.load_default_data_directory(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


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

def self.load_default_data_directory(path)
  @@default_data_directory = nil
  if !path.nil?
    full_path = File.absolute_path(path)
    setup_directory(full_path)
    @@default_data_directory = full_path
  end
end

.load_exception_notification(config, settings) ⇒ Object

Parameters:

  • config (Hash)
  • settings (Hash)


154
155
156
157
158
# File 'lib/settings.rb', line 154

def self.load_exception_notification(config, settings)
  if settings
    config.middleware.use ExceptionNotification::Rack, email: process_exception_notification(settings)
  end
end

.load_from_file(config, path, set_name) ⇒ Object

Parameters:

  • config (Hash)
  • path (String)
  • set_name (Symbol)


64
65
66
67
68
69
70
71
72
# File 'lib/settings.rb', line 64

def self.load_from_file(config, path, set_name)
  hash = YAML.load_file(path)
  if hash.keys.include?(set_name.to_s)
    self.load_from_hash(config, Utilities::Hashes.symbolize_keys(hash[set_name.to_s] || { }))
  else
    # require settings for production, but technically not test/development
    raise Error, "#{set_name} settings set not found" unless %w{production test development}.include?(set_name.to_s)
  end
end

.load_from_hash(config, hash) ⇒ Boolean

Parameters:

  • config (Hash)
  • hash (Hash)

Returns:

  • (Boolean)

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/settings.rb', line 39

def self.load_from_hash(config, hash)
  invalid_sections = hash.keys - VALID_SECTIONS
  raise Error, "#{invalid_sections} are not valid sections" unless invalid_sections.empty?

  @@config_hash = hash.deep_dup

  load_exception_notification(config, hash[:exception_notification])
  load_default_data_directory(hash[:default_data_directory])
  load_backup_directory(hash[:backup_directory])
  load_action_mailer_smtp_settings(config, hash[:action_mailer_smtp_settings])
  load_action_mailer_url_host(config, hash[:action_mailer_url_host])
  load_mail_domain(config, hash[:mail_domain])
  load_interface(hash[:interface])
  load_selenium_config(hash[:selenium]) if hash[:selenium]
  true
end

.load_from_settings_file(config, set_name) ⇒ Object

Parameters:

  • config (Hash)
  • set_name (Symbol)


76
77
78
# File 'lib/settings.rb', line 76

def self.load_from_settings_file(config, set_name)
  self.load_from_file(config, 'config/application_settings.yml', set_name) if File.exist?('config/application_settings.yml')
end

.load_interface(settings) ⇒ Object

Parameters:

  • settings (Hash)


177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/settings.rb', line 177

def self.load_interface(settings)
  if settings
    invalid = settings.keys - [:sandbox_mode]
    raise Error, "#{invalid} are not valid settings for interface" unless invalid.empty?
    if settings[:sandbox_mode] == true
      @@sandbox_mode = true
      @@sandbox_commit_sha = TaxonworksNet.commit_sha
      @@sandbox_short_commit_sha = TaxonworksNet.commit_sha.try(:slice!, 0, 12)
      @@sandbox_commit_date = TaxonworksNet.commit_date
    end
  end
end

.load_mail_domain(config, mail_domain) ⇒ String

Parameters:

  • config (Hash)
  • mail_domain (String)

Returns:

  • (String)


226
227
228
# File 'lib/settings.rb', line 226

def self.load_mail_domain(config, mail_domain)
  @@mail_domain = mail_domain
end

.load_selenium_config(settings) ⇒ Hash

Parameters:

  • settings (Hash)

Returns:

  • (Hash)

Raises:



192
193
194
195
196
197
198
199
200
201
202
# File 'lib/settings.rb', line 192

def self.load_selenium_config(settings)
  invalid = settings.keys - [:browser, :marionette, :firefox_binary_path, :chromedriver_path, :headless]

  raise Error, "#{invalid} are not valid settings for test:selenium." unless invalid.empty?
  raise Error, "Can not find Firefox browser binary #{settings[:firefox_binary_path]}." if settings[:browser] == :firefox && !settings[:firefox_binary_path].blank? && !File.exists?(settings[:firefox_binary_path])
  raise Error, "Can not find chromedriver #{ settings[:chromedriver_path] }." if settings[:browser] == :chrome && !settings[:chromedriver_path].blank? && !File.exists?(settings[:chromedriver_path])

  settings.each do |k,v|
    @@selenium_settings[k] = v if !v.blank?
  end
end

.load_test_defaults(config) ⇒ Boolean

Parameters:

  • config (Hash)

Returns:

  • (Boolean)


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/settings.rb', line 232

def self.load_test_defaults(config)
  load_from_hash(config, {
    exception_notification: {
      email_prefix: '[TW-Error] ',
      sender_address: %{"notifier" <notifier@example.com>},
      exception_recipients: %w{exceptions@example.com},
    },
    mail_domain: 'example.com',
    selenium: {
      browser: 'firefox',
      marionette: false,
      firefox_binary_path: nil,
      chromedriver_path: nil
    }
  })
end

.mail_domainString

Returns:

  • (String)


91
92
93
# File 'lib/settings.rb', line 91

def self.mail_domain
  @@mail_domain
end

.process_exception_notification(settings) ⇒ Hash

Parameters:

  • settings (Hash)

Returns:

  • (Hash)

Raises:



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/settings.rb', line 162

def self.process_exception_notification(settings)
  missing = EXCEPTION_NOTIFICATION_SETTINGS - settings.keys
  raise Error, "Missing #{missing} settings in exception_notification" unless missing.empty?

  invalid = settings.keys - EXCEPTION_NOTIFICATION_SETTINGS
  raise Error, "#{invalid} are not valid settings for exception_notification" unless invalid.empty?

  settings[:exception_recipients] =  settings[:exception_recipients].split(',') unless settings[:exception_recipients].class == Array || settings[:exception_recipients].blank?

  raise Error, ':exception_recipients must be an Array' unless settings[:exception_recipients].class == Array

  settings
end

.sandbox_commit_dateDate

Returns:

  • (Date)


111
112
113
# File 'lib/settings.rb', line 111

def self.sandbox_commit_date
  @@sandbox_commit_date
end

.sandbox_commit_shaString

Returns:

  • (String)


101
102
103
# File 'lib/settings.rb', line 101

def self.sandbox_commit_sha
  @@sandbox_commit_sha
end

.sandbox_mode?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/settings.rb', line 96

def self.sandbox_mode?
  @@sandbox_mode
end

.sandbox_short_commit_shaString

Returns:

  • (String)


106
107
108
# File 'lib/settings.rb', line 106

def self.sandbox_short_commit_sha
  @@sandbox_short_commit_sha
end

.selenium_settingsHash

Returns:

  • (Hash)


116
117
118
# File 'lib/settings.rb', line 116

def self.selenium_settings
  @@selenium_settings
end

.setup_directory(path) ⇒ Object

Parameters:

  • path (String)


121
122
123
124
125
126
127
128
# File 'lib/settings.rb', line 121

def self.setup_directory(path)
  if !Dir.exists?(path)
    # TODO: use/open a logger
    Rainbow("Directory #{path} does not exist, creating").purple
    FileUtils.mkdir_p(path)
    raise Error, "Directory #{path} could not be made, check permissions" unless Dir.exists?(path)
  end
end