Sha256: a07804dc428483dec9c08a6ec9242b194d7978d35b9be49c4c4c665a12a6e84a

Contents?: true

Size: 1.2 KB

Versions: 13

Compression:

Stored size: 1.2 KB

Contents

module GhostAdapter
  class EnvParser
    attr_reader :config

    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

    def ghost_key?(key)
      key.start_with?('GHOST_') && (key != 'GHOST_MIGRATE')
    end

    def convert_env_key(key)
      key.gsub('GHOST_', '').downcase.to_sym
    end

    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

    def try_to_i_env(value)
      return unless /\A[0-9]+$/ =~ value

      value.to_i
    end

    def try_to_f_env(value)
      return unless /\A[0-9]*\.[0-9]+$/ =~ value

      value.to_f
    end

    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
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ghost_adapter-0.7.0 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.6.0 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.5.0 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.4.2 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.4.1 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.4.0 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.3.0 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.2.3 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.2.2 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.2.1 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.2.0 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.1.4 lib/ghost_adapter/env_parser.rb
ghost_adapter-0.1.3 lib/ghost_adapter/env_parser.rb