Module: Settings

Defined in:
lib/settings.rb

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)


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

def self.backup_directory
  @@backup_directory
end

.default_data_directoryString

Returns:

  • (String)


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

def self.default_data_directory
  @@default_data_directory
end

.get_config_hashHash

Returns:

  • (Hash)


61
62
63
# File 'lib/settings.rb', line 61

def self.get_config_hash
  @@config_hash
end

.load_action_mailer_smtp_settings(config, settings) ⇒ Hash

Parameters:

  • config (Hash)
  • settings (Hash)

Returns:

  • (Hash)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/settings.rb', line 213

def self.load_action_mailer_smtp_settings(config, settings)
  if settings
    auth = {}
    user = ENV['TW_ACTION_MAILER_SMTP_SETTINGS_USER_NAME']
    pass = ENV['TW_ACTION_MAILER_SMTP_SETTINGS_PASSWORD']
    auth[:user_name] = user unless user.blank?
    auth[:password] = pass unless pass.blank?

    config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      openssl_verify_mode: 'none',
    }.merge(auth).merge(settings)
  end
end

.load_action_mailer_url_host(config, url_host) ⇒ Boolean

Parameters:

  • config (Hash)
  • url_host (String)

Returns:

  • (Boolean)


231
232
233
234
235
# File 'lib/settings.rb', line 231

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

.load_backup_directory(path) ⇒ String

Parameters:

  • path (String)

Returns:

  • (String)


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

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)


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

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)


158
159
160
161
162
# File 'lib/settings.rb', line 158

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)


68
69
70
71
72
73
74
75
76
# File 'lib/settings.rb', line 68

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:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/settings.rb', line 43

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)


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

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)


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

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)


240
241
242
# File 'lib/settings.rb', line 240

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

.load_selenium_config(settings) ⇒ Hash

Parameters:

  • settings (Hash)

Returns:

  • (Hash)

Raises:



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/settings.rb', line 198

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.exist?(settings[:firefox_binary_path])
  raise Error, "Can not find chromedriver #{ settings[:chromedriver_path] }." if settings[:browser] == :chrome && !settings[:chromedriver_path].blank? && !File.exist?(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)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/settings.rb', line 246

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)


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

def self.mail_domain
  @@mail_domain
end

.process_exception_notification(settings) ⇒ Hash

Parameters:

  • settings (Hash)

Returns:

  • (Hash)

Raises:



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

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?

  settings[:sections] = %w{github_link request session environment backtrace full_backtrace}

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

  settings
end

.sandbox_commit_dateDate

Returns:

  • (Date)


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

def self.sandbox_commit_date
  @@sandbox_commit_date
end

.sandbox_commit_shaString

Returns:

  • (String)


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

def self.sandbox_commit_sha
  @@sandbox_commit_sha
end

.sandbox_mode?Boolean

Returns:

  • (Boolean)


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

def self.sandbox_mode?
  @@sandbox_mode
end

.sandbox_short_commit_shaString

Returns:

  • (String)


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

def self.sandbox_short_commit_sha
  @@sandbox_short_commit_sha
end

.selenium_settingsHash

Returns:

  • (Hash)


120
121
122
# File 'lib/settings.rb', line 120

def self.selenium_settings
  @@selenium_settings
end

.setup_directory(path) ⇒ Object

Parameters:

  • path (String)


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

def self.setup_directory(path)
  if !Dir.exist?(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.exist?(path)
  end
end