Module: Pechkin::ConfigurationLoader

Included in:
ConfigurationLoaderBots, ConfigurationLoaderChannels, ConfigurationLoaderViews
Defined in:
lib/pechkin/configuration/configuration_loader.rb

Overview

Common code for all configuration loaders. To use this code just include module in user class.

Instance Method Summary collapse

Instance Method Details

#create_connector(bot) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/pechkin/configuration/configuration_loader.rb', line 25

def create_connector(bot)
  case bot.connector
  when 'tg', 'telegram'
    Connector::Telegram.new(bot.token, bot.name)
  when 'slack'
    Connector::Slack.new(bot.token, bot.name)
  else
    raise "Unknown connector #{bot.connector} for #{bot.name}"
  end
end

#fetch_field(object, field, file) ⇒ Object

Raises:



5
6
7
8
9
10
11
# File 'lib/pechkin/configuration/configuration_loader.rb', line 5

def fetch_field(object, field, file)
  contains = object.key?(field)

  raise ConfigurationError, "#{file}: '#{field}' is missing" unless contains

  object[field]
end

#fetch_value_from_env(object, token_field, file) ⇒ Object

Fetch token from environment variable defined in configuration file.

Raises:



14
15
16
17
18
19
20
21
22
23
# File 'lib/pechkin/configuration/configuration_loader.rb', line 14

def fetch_value_from_env(object, token_field, file)
  raise ConfigurationError, "#{file}: '#{token_field}' is missing in configuration" unless object.key?(token_field)

  env_var = object[token_field]
  token = ENV.fetch(env_var, nil)

  raise ConfigurationError, "#{file}: environment var '#{token_field}' is missing" if token.to_s.strip.empty?

  token
end

#yaml_load(file) ⇒ Object



36
37
38
# File 'lib/pechkin/configuration/configuration_loader.rb', line 36

def yaml_load(file)
  YAML.safe_load(IO.read(file))
end