Sha256: 7b47a59ca746cc2708cafd749e9fdf8bcb2e5eb5400b748be0c3dee9d8424a71

Contents?: true

Size: 1.01 KB

Versions: 69

Compression:

Stored size: 1.01 KB

Contents

require 'set'

module Honeybadger
  class Config
    module Env
      CONFIG_KEY = /\AHONEYBADGER_(.+)\Z/.freeze
      CONFIG_MAPPING = Hash[DEFAULTS.keys.map {|k| [k.to_s.upcase.gsub(KEY_REPLACEMENT, '_'), k] }].freeze
      ARRAY_VALUES = Regexp.new('\s*,\s*').freeze
      IGNORED_TYPES = Set[Hash]

      def self.new(env = ENV)
        hash = {}

        env.each_pair do |k,v|
          next unless k.match(CONFIG_KEY)
          next unless config_key = CONFIG_MAPPING[$1]
          type = OPTIONS[config_key][:type]
          next if IGNORED_TYPES.include?(type)
          hash[config_key] = cast_value(v, type)
        end

        hash
      end

      def self.cast_value(value, type = String)
        v = value.to_s

        if type == Boolean
          !!(v =~ /\A(true|t|1)\z/i)
        elsif type == Array
          v.split(ARRAY_VALUES).map(&method(:cast_value))
        elsif type == Integer
          v.to_i
        elsif type == Float
          v.to_f
        else
          v
        end
      end
    end
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
honeybadger-5.26.2 lib/honeybadger/config/env.rb
honeybadger-5.26.1 lib/honeybadger/config/env.rb
honeybadger-5.26.0 lib/honeybadger/config/env.rb
honeybadger-5.25.0 lib/honeybadger/config/env.rb
honeybadger-5.24.0 lib/honeybadger/config/env.rb
honeybadger-5.23.0 lib/honeybadger/config/env.rb
honeybadger-5.22.0 lib/honeybadger/config/env.rb
honeybadger-5.21.0 lib/honeybadger/config/env.rb
honeybadger-5.20.1 lib/honeybadger/config/env.rb
honeybadger-5.20.0 lib/honeybadger/config/env.rb
honeybadger-5.19.2 lib/honeybadger/config/env.rb
honeybadger-5.19.1 lib/honeybadger/config/env.rb
honeybadger-5.19.0 lib/honeybadger/config/env.rb
honeybadger-5.18.0 lib/honeybadger/config/env.rb
honeybadger-5.17.0 lib/honeybadger/config/env.rb
honeybadger-5.16.0 lib/honeybadger/config/env.rb
honeybadger-5.15.6 lib/honeybadger/config/env.rb
honeybadger-5.15.5 lib/honeybadger/config/env.rb
honeybadger-5.15.4 lib/honeybadger/config/env.rb
honeybadger-5.15.3 lib/honeybadger/config/env.rb