Sha256: 15545648baf1e18b5ec586bbefb5db28ed3bbf29efa33a5b5b688006cd615df4

Contents?: true

Size: 1.43 KB

Versions: 26

Compression:

Stored size: 1.43 KB

Contents

require 'pathname'
require 'yaml'
require 'erb'

module Honeybadger
  class Config
    class Yaml < ::Hash
      DISALLOWED_KEYS = [:'config.path'].freeze

      def initialize(path, env = 'production')
        @path = path.kind_of?(Pathname) ? path : Pathname.new(path)

        if !@path.exist?
          raise ConfigError, "The configuration file #{@path} was not found."
        elsif !@path.file?
          raise ConfigError, "The configuration file #{@path} is not a file."
        elsif !@path.readable?
          raise ConfigError, "The configuration file #{@path} is not readable."
        else
          yaml = load_yaml
          yaml.merge!(yaml[env]) if yaml[env].kind_of?(Hash)
          update(dotify_keys(yaml))
        end
      end

      private

      def load_yaml
        yaml = YAML.load(ERB.new(@path.read).result)
        case yaml
        when Hash
          yaml
        when NilClass, FalseClass
          {}
        else
          raise ConfigError, "The configuration file #{@path} is invalid."
        end
      end

      def dotify_keys(hash, key_prefix = nil)
        {}.tap do |new_hash|
          hash.each_pair do |k,v|
            k = [key_prefix, k].compact.join('.')
            if v.kind_of?(Hash)
              new_hash.update(dotify_keys(v, k))
            else
              next if DISALLOWED_KEYS.include?(k.to_sym)
              new_hash[k.to_sym] = v
            end
          end
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
honeybadger-2.7.2 lib/honeybadger/config/yaml.rb
honeybadger-2.7.1 lib/honeybadger/config/yaml.rb
honeybadger-2.7.0 lib/honeybadger/config/yaml.rb
honeybadger-2.6.1 lib/honeybadger/config/yaml.rb
honeybadger-2.6.0 lib/honeybadger/config/yaml.rb
honeybadger-2.5.3 lib/honeybadger/config/yaml.rb
honeybadger-2.5.2 lib/honeybadger/config/yaml.rb
honeybadger-2.5.1 lib/honeybadger/config/yaml.rb
honeybadger-2.5.0 lib/honeybadger/config/yaml.rb
honeybadger-2.4.1 lib/honeybadger/config/yaml.rb
honeybadger-2.4.0 lib/honeybadger/config/yaml.rb
honeybadger-2.3.3 lib/honeybadger/config/yaml.rb
honeybadger-2.3.2 lib/honeybadger/config/yaml.rb
honeybadger-2.3.2.beta.1 lib/honeybadger/config/yaml.rb
honeybadger-2.3.1 lib/honeybadger/config/yaml.rb
honeybadger-2.3.0 lib/honeybadger/config/yaml.rb
honeybadger-2.2.0 lib/honeybadger/config/yaml.rb
honeybadger-2.2.0.beta.1 lib/honeybadger/config/yaml.rb
honeybadger-2.1.6.beta.1 lib/honeybadger/config/yaml.rb
honeybadger-2.1.5 lib/honeybadger/config/yaml.rb