Class: Antaeus::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/antaeus-sdk/config.rb

Overview

A generic way of constructing a mergeable configuration

Instance Method Summary collapse

Instance Method Details

#loadObject

Construct a base config using the following order of precedence:

* environment variables
* YAML file
* defaults


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/antaeus-sdk/config.rb', line 7

def load
  # First, apply the defaults
  defaults = {
    group_name_attribute: :cn,
    user_login_attribute: :uid,
    user_firstname_attribute: :givenName,
    user_lastname_attribute: :sn,
    user_mail_attribute: :mail,
    base_url: 'http://localhost:8080',
    login: 'username',
    password: 'p@assedWard!'
  }
  merge defaults

  # Then apply the config file, if one exists
  begin
    apprc_dir = File.expand_path(File.join('~', '.antaeus'))
    config_file = File.expand_path(File.join(apprc_dir, 'client.yml'))
    merge YAML.load_file(config_file) if File.readable?(config_file)
  rescue => e
    puts "Unable to read from ~/.antaeus/client.yml"
  end

  # Finally, apply any environment variables specified
  env_conf = {}
  defaults.keys.each do |key|
    antaeus_key = "ANTAEUS_#{key}".upcase
    env_conf[key] = ENV[antaeus_key] if ENV.key?(antaeus_key)
  end
  merge env_conf unless env_conf.empty?
end

#merge(data) ⇒ Object



39
40
41
42
43
44
# File 'lib/antaeus-sdk/config.rb', line 39

def merge(data)
  raise Exceptions::InvalidConfigData unless data.is_a?(Hash)
  data.each do |k, v|
    self[k.to_sym] = v
  end
end