Module: Honeybadger::Config::Yaml Private

Defined in:
lib/honeybadger/config/yaml.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Constant Summary collapse

DISALLOWED_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[:'config.path'].freeze

Class Method Summary collapse

Class Method Details

.dotify_keys(hash, key_prefix = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/honeybadger/config/yaml.rb', line 61

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

.load_yaml(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/honeybadger/config/yaml.rb', line 27

def self.load_yaml(path)
  begin
    # This uses `YAML.unsafe_load` to support loading arbitrary Ruby
    # classes, such as !ruby/regexp. This was the default behavior prior
    # to Psych 4. https://bugs.ruby-lang.org/issues/17866
    method = YAML.respond_to?(:unsafe_load) ? :unsafe_load : :load
    yaml = YAML.send(method, 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

.new(path, env = 'production') ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/honeybadger/config/yaml.rb', line 10

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