Class: Co2Notify::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/co2-notify/config.rb

Constant Summary collapse

CONFIG_DIR =
".co2-notify".freeze
CONFIG_FILE_NAME =
"config.yml".freeze
OUTPUT_FILE_NAME =
"output.log".freeze
ERROR_FILE_NAME =
"error.log".freeze
DEFAULT_TIMEOUT =
5.freeze
DEFAULT_COOLDOWN =
15.freeze
DEFAULT_HIGH_LEVEL =
800.freeze
DEFAULT_VERY_HIGH_LEVEL =
1200.freeze
DEFAULT_START_TIME =
"12:00".freeze
DEFAULT_STOP_TIME =
"19:00".freeze
DEFAULT_PING_TIMEOUT =
60.freeze

Class Method Summary collapse

Class Method Details

.config_dirObject



55
56
57
# File 'lib/co2-notify/config.rb', line 55

def self.config_dir
  File.join(ENV['HOME'], CONFIG_DIR)
end

.config_fileObject



59
60
61
# File 'lib/co2-notify/config.rb', line 59

def self.config_file
  File.join(config_dir, CONFIG_FILE_NAME)
end

.error_pathObject



67
68
69
# File 'lib/co2-notify/config.rb', line 67

def self.error_path
  File.join(config_dir, ERROR_FILE_NAME)
end

.getObject



16
17
18
# File 'lib/co2-notify/config.rb', line 16

def self.get
  new YAML.load_file(config_file)
end

.output_pathObject



63
64
65
# File 'lib/co2-notify/config.rb', line 63

def self.output_path
  File.join(config_dir, OUTPUT_FILE_NAME)
end

.setObject



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
49
50
51
52
53
# File 'lib/co2-notify/config.rb', line 20

def self.set
  FileUtils.mkdir_p(config_dir) unless File.directory?(config_dir)

  data = {}.tap do |h|
    print "Monitor location (required): "
    h["location"] = STDIN.gets.chomp
    print "Monitor user (required): "
    h["user"] = STDIN.gets.chomp
    print "Hipchat API token (required): "
    h["api_token"] = STDIN.gets.chomp
    print "Hipchat room name (required): "
    h["room"] = STDIN.gets.chomp
    print "Timeout (default: #{DEFAULT_TIMEOUT} mins): "
    h["timeout"] = (STDIN.gets.chomp.presence || DEFAULT_TIMEOUT).to_i
    print "Cooldown (default: #{DEFAULT_COOLDOWN} mins): "
    h["cooldown"] = (STDIN.gets.chomp.presence || DEFAULT_COOLDOWN).to_i
    print "Ping timeout (default: #{DEFAULT_PING_TIMEOUT} mins): "
    h["ping_timeout"] = (STDIN.gets.chomp.presence || DEFAULT_PING_TIMEOUT).to_i
    print "High CO₂ level (default: #{DEFAULT_HIGH_LEVEL}): "
    h["high_level"] = (STDIN.gets.chomp.presence || DEFAULT_HIGH_LEVEL).to_i
    print "Very High CO₂ level (default: #{DEFAULT_VERY_HIGH_LEVEL}): "
    h["very_high_level"] = (STDIN.gets.chomp.presence || DEFAULT_VERY_HIGH_LEVEL).to_i
    print "Start time (default: #{DEFAULT_START_TIME}): "
    h["start_time"] = STDIN.gets.chomp.presence || DEFAULT_START_TIME
    print "Stop time (default: #{DEFAULT_STOP_TIME}): "
    h["stop_time"] = STDIN.gets.chomp.presence || DEFAULT_STOP_TIME
    print "Mention (optional): "
    h["mention"] = STDIN.gets.chomp
  end

  File.open(config_file, "w") do |f|
    YAML.dump(data, f)
  end
end