Module: Settings

Defined in:
lib/settings.rb

Overview

Server/application configuration settings

Defined Under Namespace

Classes: Error

Constant Summary

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_commit_date =
nil
@@selenium_settings =
{}

Class Method Summary (collapse)

Class Method Details

+ (String) backup_directory

Returns:

  • (String)


84
85
86
# File 'lib/settings.rb', line 84

def self.backup_directory
  @@backup_directory
end

+ (String) default_data_directory

Returns:

  • (String)


79
80
81
# File 'lib/settings.rb', line 79

def self.default_data_directory
  @@default_data_directory
end

+ (Hash) get_config_hash

Returns:

  • (Hash)


55
56
57
# File 'lib/settings.rb', line 55

def self.get_config_hash
  @@config_hash
end

+ (Hash) load_action_mailer_smtp_settings(config, settings)

Parameters:

  • config (Hash)
  • settings (Hash)

Returns:

  • (Hash)


199
200
201
202
203
204
# File 'lib/settings.rb', line 199

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

+ (Boolean) load_action_mailer_url_host(config, url_host)

Parameters:

  • config (Hash)
  • url_host (String)

Returns:

  • (Boolean)


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

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

+ (String) load_backup_directory(path)

Parameters:

  • path (String)

Returns:

  • (String)


136
137
138
139
140
141
142
143
# File 'lib/settings.rb', line 136

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

+ (String) load_default_data_directory(path)

Parameters:

  • path (String)

Returns:

  • (String)


125
126
127
128
129
130
131
132
# File 'lib/settings.rb', line 125

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

+ (Object) load_exception_notification(config, settings)

Parameters:

  • config (Hash)
  • settings (Hash)


147
148
149
150
151
# File 'lib/settings.rb', line 147

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

+ (Object) load_from_file(config, path, set_name)

Parameters:

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


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

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

+ (Boolean) load_from_hash(config, hash)

Parameters:

  • config (Hash)
  • hash (Hash)

Returns:

  • (Boolean)

Raises:



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

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

+ (Object) load_from_settings_file(config, set_name)

Parameters:

  • config (Hash)
  • set_name (Symbol)


74
75
76
# File 'lib/settings.rb', line 74

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

+ (Object) load_interface(settings)

Parameters:

  • settings (Hash)


170
171
172
173
174
175
176
177
178
179
180
# File 'lib/settings.rb', line 170

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]
      @@sandbox_mode = true
      @@sandbox_commit_sha = TaxonworksNet.commit_sha
      @@sandbox_commit_date = TaxonworksNet.commit_date
    end
  end
end

+ (String) load_mail_domain(config, mail_domain)

Parameters:

  • config (Hash)
  • mail_domain (String)

Returns:

  • (String)


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

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

+ (Hash) load_selenium_config(settings)

Parameters:

  • settings (Hash)

Returns:

  • (Hash)

Raises:



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/settings.rb', line 184

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

  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

+ (Boolean) load_test_defaults(config)

Parameters:

  • config (Hash)

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/settings.rb', line 224

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

+ (String) mail_domain

Returns:

  • (String)


89
90
91
# File 'lib/settings.rb', line 89

def self.mail_domain
  @@mail_domain
end

+ (Hash) process_exception_notification(settings)

Parameters:

  • settings (Hash)

Returns:

  • (Hash)

Raises:



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/settings.rb', line 155

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

+ (Date) sandbox_commit_date

Returns:

  • (Date)


104
105
106
# File 'lib/settings.rb', line 104

def self.sandbox_commit_date
  @@sandbox_commit_date
end

+ (String) sandbox_commit_sha

Returns:

  • (String)


99
100
101
# File 'lib/settings.rb', line 99

def self.sandbox_commit_sha
  @@sandbox_commit_sha
end

+ (Boolean) sandbox_mode?

Returns:

  • (Boolean)


94
95
96
# File 'lib/settings.rb', line 94

def self.sandbox_mode?
  @@sandbox_mode
end

+ (Hash) selenium_settings

Returns:

  • (Hash)


109
110
111
# File 'lib/settings.rb', line 109

def self.selenium_settings
  @@selenium_settings
end

+ (Object) setup_directory(path)

Parameters:

  • path (String)


114
115
116
117
118
119
120
121
# File 'lib/settings.rb', line 114

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