Class: Alerty::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/alerty/config.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optsObject (readonly)

Returns the value of attribute opts.



8
9
10
# File 'lib/alerty/config.rb', line 8

def opts
  @opts
end

Class Method Details

.configObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/alerty/config.rb', line 18

def config
  return @config if @config
  if dotenv?
    require 'dotenv'
    Dotenv.load
  end
  content = File.read(config_path)
  erb = ERB.new(content, nil, '-')
  erb_content = erb.result
  if debug?
    puts '=== Erb evaluated config ==='
    puts erb_content
  end
  yaml = YAML.load(erb_content)
  @config = Hashie::Mash.new(yaml)
  if debug?
    puts '=== Recognized config ==='
    puts({
      'log_path' => log_path,
      'log_level' => log_level,
      'log_shift_age' => log_shift_age,
      'log_shift_size' => log_shift_size,
      'timeout' => timeout,
      'lock_path' => lock_path,
      'retry_limit' => retry_limit,
      'retry_wait' => retry_wait,
      'plugin' => yaml['plugins'],
    }.to_yaml)
  end
  @config
end

.config_pathObject



14
15
16
# File 'lib/alerty/config.rb', line 14

def config_path
  @config_path ||= opts[:config_path] || ENV['ALERTY_CONFIG_PATH'] || '/etc/alerty/alerty.yml'
end

.configure(opts) ⇒ Object



10
11
12
# File 'lib/alerty/config.rb', line 10

def configure(opts)
  @opts = opts
end

.debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  !!opts[:debug]
end

.dotenv?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/alerty/config.rb', line 88

def dotenv?
  !!opts[:dotenv]
end

.lock_pathObject



72
73
74
# File 'lib/alerty/config.rb', line 72

def lock_path
  opts[:lock_path] || config.lock_path
end

.log_levelObject



54
55
56
# File 'lib/alerty/config.rb', line 54

def log_level
  opts[:log_level] || config.log_level || 'warn'
end

.log_pathObject



50
51
52
# File 'lib/alerty/config.rb', line 50

def log_path
  opts[:log_path] || config.log_path || 'STDOUT'
end

.log_shift_ageObject



58
59
60
61
# File 'lib/alerty/config.rb', line 58

def log_shift_age
  # config.shift_age is for old version compatibility
  opts[:log_shift_age] || config.shift_age || config.log_shift_age || 0
end

.log_shift_sizeObject



63
64
65
66
# File 'lib/alerty/config.rb', line 63

def log_shift_size
  # config.log_shift_age is for old version compatibility
  opts[:log_shift_size] || config.shift_size || config.log_shift_size || 1048576
end

.pluginsObject



98
99
100
# File 'lib/alerty/config.rb', line 98

def plugins
  @plugins ||= config.fetch('plugins')
end

.resetObject

for debug



103
104
105
106
107
# File 'lib/alerty/config.rb', line 103

def reset
  @config_path = nil
  @config = nil
  @plugins = nil
end

.retry_intervalObject



92
93
94
95
96
# File 'lib/alerty/config.rb', line 92

def retry_interval
  @random ||= Random.new
  randomness = retry_wait * 0.125
  retry_wait + @random.rand(-randomness .. randomness)
end

.retry_limitObject



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

def retry_limit
  opts[:retry_limit] || config.retry_limit || 0
end

.retry_waitObject



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

def retry_wait
  opts[:retry_wait] || config.retry_wait || 1.0
end

.timeoutObject



68
69
70
# File 'lib/alerty/config.rb', line 68

def timeout
  opts[:timeout] || config.timeout
end