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



10
11
12
13
14
15
16
17
18
19
20
# 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"
  @db = PG.connect(db_connect_params)
  User.db = @db
  User.initialize_table

  @timestamps = {}
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



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

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

#down_nameObject



22
23
24
# File 'lib/slacky/config.rb', line 22

def down_name
  @name.downcase
end

#log(msg, ex = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/slacky/config.rb', line 44

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



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

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

#slack_api_tokenObject



26
27
28
# File 'lib/slacky/config.rb', line 26

def slack_api_token
  ENV['SLACK_API_TOKEN']
end

#slack_reject_channelsObject



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

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