Sha256: b68d9c2529d7d9d6df56d92817664f7646bda536808221cf6ed05a7fefd7b6fd

Contents?: true

Size: 1.83 KB

Versions: 32

Compression:

Stored size: 1.83 KB

Contents

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

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

      def self.new(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."
        end

        yaml = load_yaml(path)
        yaml.merge!(yaml[env]) if yaml[env].kind_of?(Hash)

        dotify_keys(yaml)
      end

      def self.load_yaml(path)
        begin
          yaml = YAML.load(ERB.new(path.read).result)
        rescue => e
          config_error = ConfigError.new(e.to_s)

          if e.backtrace
            backtrace = e.backtrace.map do |line|
              if line.start_with?('(erb)'.freeze)
                line.gsub('(erb)'.freeze, path.to_s)
              else
                line
              end
            end
            config_error.set_backtrace(backtrace)
          end

          raise config_error
        end

        case yaml
        when Hash
          yaml
        when NilClass, FalseClass
          {}
        else
          raise ConfigError, "The configuration file #{path} is invalid."
        end
      end

      def self.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

32 entries across 32 versions & 1 rubygems

Version Path
honeybadger-4.9.0 lib/honeybadger/config/yaml.rb
honeybadger-4.8.0 lib/honeybadger/config/yaml.rb
honeybadger-4.7.3 lib/honeybadger/config/yaml.rb
honeybadger-4.7.2 lib/honeybadger/config/yaml.rb
honeybadger-4.7.1 lib/honeybadger/config/yaml.rb
honeybadger-4.7.0 lib/honeybadger/config/yaml.rb
honeybadger-4.6.0 lib/honeybadger/config/yaml.rb
honeybadger-4.5.6 lib/honeybadger/config/yaml.rb
honeybadger-4.5.5 lib/honeybadger/config/yaml.rb
honeybadger-4.5.4 lib/honeybadger/config/yaml.rb
honeybadger-4.5.3 lib/honeybadger/config/yaml.rb
honeybadger-4.5.2 lib/honeybadger/config/yaml.rb
honeybadger-4.5.1 lib/honeybadger/config/yaml.rb
honeybadger-4.5.0 lib/honeybadger/config/yaml.rb
honeybadger-4.4.2 lib/honeybadger/config/yaml.rb
honeybadger-4.4.1 lib/honeybadger/config/yaml.rb
honeybadger-4.4.0 lib/honeybadger/config/yaml.rb
honeybadger-4.3.1 lib/honeybadger/config/yaml.rb
honeybadger-4.3.0 lib/honeybadger/config/yaml.rb
honeybadger-4.2.2 lib/honeybadger/config/yaml.rb