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
FANN_FILE_NAME =
"co2.net".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



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

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

.config_fileObject



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

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

.error_pathObject



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

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

.fann_pathObject



72
73
74
# File 'lib/co2-notify/config.rb', line 72

def self.fann_path
  File.join(config_dir, FANN_FILE_NAME)
end

.getObject



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

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

.output_pathObject



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

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

.setObject



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
54
# File 'lib/co2-notify/config.rb', line 21

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
    print "Cooldown (default: #{DEFAULT_COOLDOWN} mins): "
    h["cooldown"] = STDIN.gets.chomp.presence || DEFAULT_COOLDOWN
    print "Ping timeout (default: #{DEFAULT_PING_TIMEOUT} mins): "
    h["ping_timeout"] = STDIN.gets.chomp.presence || DEFAULT_PING_TIMEOUT
    print "High CO₂ level (default: #{DEFAULT_HIGH_LEVEL}): "
    h["high_level"] = STDIN.gets.chomp.presence || DEFAULT_HIGH_LEVEL
    print "Very High CO₂ level (default: #{DEFAULT_VERY_HIGH_LEVEL}): "
    h["very_high_level"] = STDIN.gets.chomp.presence || DEFAULT_VERY_HIGH_LEVEL
    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