Class: Slacky::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
16
17
# File 'lib/slacky/config.rb', line 10

def initialize(name, opts = {})
  @name = name
  Dotenv.load ".env", "#{config_dir}/.env"
  FileUtils.mkdir config_dir unless File.directory? config_dir
  @pid_file = "#{config_dir}/pid"
  @timestamps = {}
  User.config = self
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pid_fileObject (readonly)

Returns the value of attribute pid_file.



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

def pid_file
  @pid_file
end

Instance Method Details

#config_dirObject



39
40
41
# File 'lib/slacky/config.rb', line 39

def config_dir
  ENV['CONFIG_DIR'] || "#{ENV['HOME']}/.#{down_name}"
end

#down_nameObject



31
32
33
# File 'lib/slacky/config.rb', line 31

def down_name
  @name.downcase
end

#log(msg, ex = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/slacky/config.rb', line 58

def log(msg, ex = nil)
  log = File.new(log_file, 'a')
  timestamp = Time.now.strftime('%Y-%m-%d %H:%M:%S')
  type = ex ? 'ERROR' : ' INFO'
  log.puts "#{type}  #{timestamp}  #{msg}"
  if ex
    log.puts ex.message
    log.puts("Stacktrace:\n" + ex.backtrace.join("\n"))
  end
  log.flush
end

#slack_accept_channelsObject



48
49
50
51
# File 'lib/slacky/config.rb', line 48

def slack_accept_channels
  return nil unless ENV['ACCEPT_CHANNELS']
  ENV['ACCEPT_CHANNELS'].split(',').map {|c| c.strip}
end

#slack_api_tokenObject



35
36
37
# File 'lib/slacky/config.rb', line 35

def slack_api_token
  ENV['SLACK_API_TOKEN']
end

#slack_reject_channelsObject



43
44
45
46
# File 'lib/slacky/config.rb', line 43

def slack_reject_channels
  return nil unless ENV['REJECT_CHANNELS']
  ENV['REJECT_CHANNELS'].split(',').map {|c| c.strip}
end

#whitelist_usersObject



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

def whitelist_users
  return nil unless ENV['WHITELIST_USERS']
  ENV['WHITELIST_USERS'].split(',').map {|u| u.strip}
end