class Metrux::ConfigBuilders::Influx

Constants

DEFAULT_ASYNC
DEFAULT_TIME_PRECISION

Attributes

yaml[R]

Public Class Methods

new(yaml) click to toggle source
# File lib/metrux/config_builders/influx.rb, line 7
def initialize(yaml)
  @yaml = yaml
end

Public Instance Methods

build() click to toggle source
# File lib/metrux/config_builders/influx.rb, line 11
def build
  defaults.deep_merge(
    config_from_yaml.deep_merge(config_from_env_var)
  ).freeze
end

Private Instance Methods

boolean_value(value) click to toggle source
# File lib/metrux/config_builders/influx.rb, line 59
def boolean_value(value)
  return true if value == true || value =~ /^(true)$/i
  return false if value == false || value =~ /^(false)$/i

  nil
end
cast_value(value) click to toggle source
# File lib/metrux/config_builders/influx.rb, line 38
def cast_value(value)
  string_value = value.to_s

  unless (numeric_value = numeric_value(string_value)).nil?
    return numeric_value
  end

  unless (boolean_value = boolean_value(string_value)).nil?
    return boolean_value
  end

  value
end
config_from_env_var() click to toggle source
# File lib/metrux/config_builders/influx.rb, line 21
def config_from_env_var
  fetch_from(ENV, 'METRUX_INFLUX_'.freeze)
end
config_from_yaml() click to toggle source
# File lib/metrux/config_builders/influx.rb, line 25
def config_from_yaml
  fetch_from(yaml, 'influx_'.freeze)
end
defaults() click to toggle source
# File lib/metrux/config_builders/influx.rb, line 66
def defaults
  { time_precision: DEFAULT_TIME_PRECISION, async: DEFAULT_ASYNC }
end
fetch_from(object, prefix) click to toggle source
# File lib/metrux/config_builders/influx.rb, line 29
def fetch_from(object, prefix)
  object.each_with_object({}) do |(config_key, value), acc|
    if config_key.start_with?(prefix)
      acc[config_key.gsub(/^#{prefix}/, '').to_s.downcase.to_sym] =
        cast_value(value)
    end
  end
end
numeric_value(value) click to toggle source
# File lib/metrux/config_builders/influx.rb, line 52
def numeric_value(value)
  return value.to_f if value.to_f.to_s == value
  return value.to_i if value.to_i.to_s == value

  nil
end