class GhostAdapter::EnvParser

Attributes

config[R]

Public Class Methods

new(env = {}) click to toggle source
# File lib/ghost_adapter/env_parser.rb, line 5
def initialize(env = {})
  @config = env.map do |key, value|
    next unless ghost_key?(key)

    config_key = convert_env_key(key)

    next unless GhostAdapter::CONFIG_KEYS.include?(config_key)

    config_value = convert_env_value(value)

    [config_key, config_value]
  end.compact.to_h
end

Private Instance Methods

convert_env_key(key) click to toggle source
# File lib/ghost_adapter/env_parser.rb, line 25
def convert_env_key(key)
  key.gsub('GHOST_', '').downcase.to_sym
end
convert_env_value(value) click to toggle source
# File lib/ghost_adapter/env_parser.rb, line 29
def convert_env_value(value)
  num_val = try_to_i_env(value) || try_to_f_env(value)
  return num_val unless num_val.nil?

  bool_val = try_to_bool_env(value)
  return bool_val unless bool_val.nil?

  value
end
ghost_key?(key) click to toggle source
# File lib/ghost_adapter/env_parser.rb, line 21
def ghost_key?(key)
  key.start_with?('GHOST_') && (key != 'GHOST_MIGRATE')
end
try_to_bool_env(value) click to toggle source
# File lib/ghost_adapter/env_parser.rb, line 51
def try_to_bool_env(value)
  lowered = value.downcase
  return true if %w[yes y true t].include? lowered

  return false if %w[no n false f].include? lowered

  nil
end
try_to_f_env(value) click to toggle source
# File lib/ghost_adapter/env_parser.rb, line 45
def try_to_f_env(value)
  return unless /\A[0-9]*\.[0-9]+$/ =~ value

  value.to_f
end
try_to_i_env(value) click to toggle source
# File lib/ghost_adapter/env_parser.rb, line 39
def try_to_i_env(value)
  return unless /\A[0-9]+$/ =~ value

  value.to_i
end