Class: Briefbag::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/briefbag/configuration.rb

Constant Summary collapse

MESSAGES =
{
  notice_yml: 'NOTICE! Your app is using configs from yml file',
  notice_consul: 'NOTICE! Your app is using configs from consul now
    If you want to use local configs. you need to create config file. Just run `rake settings:consul2yml`',
  error_consul: 'ALARM! You are trying to get consul config, but you have no consul connection.
    Please! connect to VPN or check consul configuration',
  error_yml: "ALARM! You are trying to use local schema connection! But file configuration doesn't exist!
    Just run `rake settings:consul2yml`"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Configuration

Returns a new instance of Configuration.



22
23
24
25
# File 'lib/briefbag/configuration.rb', line 22

def initialize(config)
  @config_name = config[:config_name] || 'application'
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/briefbag/configuration.rb', line 10

def config
  @config
end

#config_nameObject (readonly)

Returns the value of attribute config_name.



10
11
12
# File 'lib/briefbag/configuration.rb', line 10

def config_name
  @config_name
end

Instance Method Details

#callObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/briefbag/configuration.rb', line 27

def call
  return file_config if file_exist?

  diplomat = Briefbag::Diplomat.new(config).call
  return Briefbag.aborting_message(MESSAGES[:error_consul]) unless diplomat.success?

  diplomat_config(diplomat[:consul_data])
rescue StandardError
  return Briefbag.aborting_message(MESSAGES[:error_yml]) unless file_exist?

  file_config
end

#diplomat_config(data) ⇒ Object



40
41
42
43
# File 'lib/briefbag/configuration.rb', line 40

def diplomat_config(data)
  Briefbag.warning_message(MESSAGES[:notice_consul])
  HashToStruct.struct(data)
end

#file_configObject



45
46
47
48
49
50
51
52
53
# File 'lib/briefbag/configuration.rb', line 45

def file_config
  Briefbag.warning_message(MESSAGES[:notice_yml])
  Anyway::Settings.default_config_path = 'config'
  data = Anyway::Config.for(config_name.to_sym, env_prefix: config[:environment])

  return HashToStruct.struct(data.deep_symbolize_keys) if defined?(Rails)

  HashToStruct.struct(symbolize_all_keys(data))
end

#file_exist?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/briefbag/configuration.rb', line 55

def file_exist?
  @file_exist ||= File.exist?(yaml_file)
end

#local_keysObject



63
64
65
# File 'lib/briefbag/configuration.rb', line 63

def local_keys
  @local_keys ||= YAML.safe_load(File.read(yaml_file))[environment].keys
end

#symbolize_all_keys(h) ⇒ Object

rubocop:disable Naming/MethodParameterName



67
68
69
70
71
72
73
74
75
76
# File 'lib/briefbag/configuration.rb', line 67

def symbolize_all_keys(h) # rubocop:disable  Naming/MethodParameterName
  if h.is_a? Hash
    h.transform_keys!(&:to_sym)
    h.each_value do |val|
      val.each { |v| symbolize_all_keys(v) } if val.is_a? Array
      symbolize_all_keys(val)
    end
  end
  h
end

#yaml_fileObject



59
60
61
# File 'lib/briefbag/configuration.rb', line 59

def yaml_file
  @yaml_file ||= "./config/#{config_name}.yml"
end