Class: TingYun::Configuration::EnvironmentSource

Inherits:
DottedHash
  • Object
show all
Defined in:
lib/ting_yun/configuration/environment_source.rb

Constant Summary collapse

SUPPORTED_PREFIXES =
/^ting_yun_|^tingyun_/i
SPECIAL_CASE_KEYS =
[
    'TING_YUN_ENV',
    'TING_YUN_LOG' # read by set_log_file
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DottedHash

#inspect, symbolize, #to_hash

Constructor Details

#initializeEnvironmentSource

Returns a new instance of EnvironmentSource.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ting_yun/configuration/environment_source.rb', line 18

def initialize
  set_config_file
  set_log_file

  @alias_map = {}
  @type_map = {}

  DEFAULTS.each do |config_setting, value|
    self.type_map[config_setting] = value[:type]
    set_aliases(config_setting, value)
  end

  set_values_from_ting_yun_environment_variables
end

Instance Attribute Details

#alias_mapObject

Returns the value of attribute alias_map.



16
17
18
# File 'lib/ting_yun/configuration/environment_source.rb', line 16

def alias_map
  @alias_map
end

#type_mapObject

Returns the value of attribute type_map.



16
17
18
# File 'lib/ting_yun/configuration/environment_source.rb', line 16

def type_map
  @type_map
end

Instance Method Details

#collect_ting_yun_environment_variable_keysObject



111
112
113
# File 'lib/ting_yun/configuration/environment_source.rb', line 111

def collect_ting_yun_environment_variable_keys
  ENV.keys.select { |key| key.match(SUPPORTED_PREFIXES) }
end

#convert_environment_key_to_config_key(key) ⇒ Object



106
107
108
109
# File 'lib/ting_yun/configuration/environment_source.rb', line 106

def convert_environment_key_to_config_key(key)
  stripped_key = key.gsub(SUPPORTED_PREFIXES, '').downcase.to_sym
  self.alias_map[stripped_key] || stripped_key
end

#set_aliases(config_setting, value) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ting_yun/configuration/environment_source.rb', line 33

def set_aliases(config_setting, value)
  set_dotted_alias(config_setting)

  return unless value[:aliases]
  value[:aliases].each do |alise|
    self.alias_map[alise] = config_setting
  end
end

#set_config_fileObject



52
53
54
# File 'lib/ting_yun/configuration/environment_source.rb', line 52

def set_config_file
  self[:config_path] = ENV['NRCONFIG'] if ENV['NRCONFIG']
end

#set_dotted_alias(original_config_setting) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/ting_yun/configuration/environment_source.rb', line 42

def set_dotted_alias(original_config_setting)
  config_setting = original_config_setting.to_s


  if config_setting.include? '.'
    config_alias = config_setting.gsub(/\./, '_').to_sym
    self.alias_map[config_alias] = original_config_setting
  end
end

#set_key_by_type(config_key, environment_key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ting_yun/configuration/environment_source.rb', line 81

def set_key_by_type(config_key, environment_key)
  value = ENV[environment_key]
  type = self.type_map[config_key]

  if type == String
    self[config_key] = value
  elsif type == Fixnum
    self[config_key] = value.to_i
  elsif type == Float
    self[config_key] = value.to_f
  elsif type == Symbol
    self[config_key] = value.to_sym
  elsif type == TingYun::Configuration::Boolean
    if value =~ /false|off|no/i
      self[config_key] = false
    elsif value != nil
      self[config_key] = true
    end
  else
    # TingYun::Agent.logger.info("#{environment_key} does not have a corresponding configuration setting (#{config_key} does not exist).")
    self[config_key] = value
  end

end

#set_log_fileObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/ting_yun/configuration/environment_source.rb', line 57

def set_log_file
  if ENV['TING_YUN_LOG']
    if ENV['TING_YUN_LOG'].upcase == 'STDOUT'
      self[:log_file_path] = self[:log_file_name] = 'STDOUT'
    else
      self[:log_file_path] = File.dirname(ENV['TING_YUN_LOG'])
      self[:log_file_name] = File.basename(ENV['TING_YUN_LOG'])
    end
  end
end

#set_value_from_environment_variable(key) ⇒ Object



76
77
78
79
# File 'lib/ting_yun/configuration/environment_source.rb', line 76

def set_value_from_environment_variable(key)
  config_key = convert_environment_key_to_config_key(key)
  set_key_by_type(config_key, key)
end

#set_values_from_ting_yun_environment_variablesObject



68
69
70
71
72
73
74
# File 'lib/ting_yun/configuration/environment_source.rb', line 68

def set_values_from_ting_yun_environment_variables
  env_var_keys = collect_ting_yun_environment_variable_keys
  env_var_keys.each do |key|
    next if SPECIAL_CASE_KEYS.include?(key.upcase)
    set_value_from_environment_variable(key)
  end
end