class Datumfactory::Config

Constants

DEFAULTS
DEFAULT_PATHS
DEVELOPMENT_ENVIRONMENTS
DOTTED_KEY
KEY_REPLACEMENT
NOT_BLANK
OPTIONS

Attributes

env[RW]
framework[RW]
ruby[RW]
yaml[RW]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/datumfactory/util/config.rb, line 24
def initialize(opts = {})
  @ruby = opts.freeze
  @env = {}.freeze
  @yaml = {}.freeze
  @framework = {}.freeze
end

Public Instance Methods

[](key)
Alias for: get
[]=(key, value)
Alias for: set
backend() click to toggle source
# File lib/datumfactory/util/config.rb, line 120
def backend
  init_backend! unless @backend
  @backend
end
backend=(backend) click to toggle source
# File lib/datumfactory/util/config.rb, line 125
def backend=(backend)
  set(:backend, backend)
  @backend = nil
end
backtrace_filter(&block) click to toggle source
# File lib/datumfactory/util/config.rb, line 52
def backtrace_filter(&block)
  if block_given?
    warn('DEPRECATED: backtrace_filter is deprecated. Please use before_notify instead. See https://docs.honeybadger.io/ruby/support/v4-upgrade#backtrace_filter')
    self[:backtrace_filter] = block
  end

  self[:backtrace_filter]
end
before_notify_hooks() click to toggle source
# File lib/datumfactory/util/config.rb, line 61
def before_notify_hooks
  (ruby[:before_notify] || []).clone
end
ca_bundle_path() click to toggle source
# File lib/datumfactory/util/config.rb, line 163
def ca_bundle_path
  if self[:'connection.system_ssl_cert_chain'] && File.exist?(OpenSSL::X509::DEFAULT_CERT_FILE)
    OpenSSL::X509::DEFAULT_CERT_FILE
  elsif self[:'connection.ssl_ca_bundle_path']
    self[:'connection.ssl_ca_bundle_path']
  else
    local_cert_path
  end
end
configure() { |new_ruby| ... } click to toggle source
# File lib/datumfactory/util/config.rb, line 44
def configure
  new_ruby = Ruby.new(self)
  yield(new_ruby)
  self.ruby = ruby.merge(new_ruby).freeze
  @logger = @backend = nil
  self
end
connection_port() click to toggle source
# File lib/datumfactory/util/config.rb, line 177
def connection_port
  if self[:'connection.port']
    self[:'connection.port']
  elsif self[:'connection.secure']
    443
  else
    80
  end
end
connection_protocol() click to toggle source
# File lib/datumfactory/util/config.rb, line 187
def connection_protocol
  if self[:'connection.secure']
    'https'
  else
    'http'
  end
end
debug?() click to toggle source
# File lib/datumfactory/util/config.rb, line 145
def debug?
  !!self[:debug]
end
detected_framework() click to toggle source
# File lib/datumfactory/util/config.rb, line 240
def detected_framework
  if self[:framework] =~ NOT_BLANK
    self[:framework].to_sym
  elsif defined?(::Rails::VERSION) && ::Rails::VERSION::STRING > '3.0'
    :rails
  elsif defined?(::Sinatra::VERSION)
    :sinatra
  elsif defined?(::Rack.release)
    :rack
  else
    :ruby
  end
end
dev?() click to toggle source
# File lib/datumfactory/util/config.rb, line 130
def dev?
  self[:env] && Array(self[:development_environments]).include?(self[:env])
end
exception_filter(&block) click to toggle source
# File lib/datumfactory/util/config.rb, line 65
def exception_filter(&block)
  if block_given?
    warn('DEPRECATED: exception_filter is deprecated. Please use before_notify instead. See https://docs.honeybadger.io/ruby/support/v4-upgrade#exception_filter')
    self[:exception_filter] = block
  end

  self[:exception_filter]
end
exception_fingerprint(&block) click to toggle source
# File lib/datumfactory/util/config.rb, line 74
def exception_fingerprint(&block)
  if block_given?
    warn('DEPRECATED: exception_fingerprint is deprecated. Please use before_notify instead. See https://docs.honeybadger.io/ruby/support/v4-upgrade#exception_fingerprint')
    self[:exception_fingerprint] = block
  end

  self[:exception_fingerprint]
end
excluded_request_keys() click to toggle source
# File lib/datumfactory/util/config.rb, line 203
def excluded_request_keys
  [].tap do |keys|
    keys << :session  if self[:'request.disable_session']
    keys << :params   if self[:'request.disable_params']
    keys << :cgi_data if self[:'request.disable_environment']
    keys << :url      if self[:'request.disable_url']
  end
end
framework_name() click to toggle source
# File lib/datumfactory/util/config.rb, line 254
def framework_name
  case detected_framework
  when :rails then "Rails #{::Rails::VERSION::STRING}"
  when :sinatra then "Sinatra #{::Sinatra::VERSION}"
  when :rack then "Rack #{::Rack.release}"
  else
    "Ruby #{RUBY_VERSION}"
  end
end
get(key) click to toggle source
# File lib/datumfactory/util/config.rb, line 83
def get(key)
  IVARS.each do |var|
    source = instance_variable_get(var)
    if source.has_key?(key)
      return source[key]
    end
  end

  DEFAULTS[key]
end
Also aliased as: []
ignored_classes() click to toggle source
# File lib/datumfactory/util/config.rb, line 155
def ignored_classes
  ignore_only = get(:'exceptions.ignore_only')
  return ignore_only if ignore_only
  return DEFAULTS[:'exceptions.ignore'] unless ignore = get(:'exceptions.ignore')

  DEFAULTS[:'exceptions.ignore'] | Array(ignore)
end
load!(framework: {}, env: ENV) click to toggle source
# File lib/datumfactory/util/config.rb, line 33
def load!(framework: {}, env: ENV)
  return self if @load

  self.framework = framework.freeze
  self.env = Env.new(env).freeze
  load_config_from_disk {|yaml| self.yaml = yaml.freeze }
  detect_revision!
  @loaded = true
  self
end
load_plugin?(name) click to toggle source
# File lib/datumfactory/util/config.rb, line 223
def load_plugin?(name)
  return false if includes_token?(self[:'skipped_plugins'], name)
  return true unless self[:plugins].kind_of?(Array)

  includes_token?(self[:plugins], name)
end
local_cert_path() click to toggle source
# File lib/datumfactory/util/config.rb, line 173
def local_cert_path
  File.expand_path(File.join('..', '..', '..', 'resources', 'ca-bundle.crt'), __FILE__)
end
log_debug?() click to toggle source
# File lib/datumfactory/util/config.rb, line 149
def log_debug?
  return debug? if self[:'logging.debug'].nil?

  !!self[:'logging.debug']
end
log_level(key = :'logging.level') click to toggle source
# File lib/datumfactory/util/config.rb, line 212
def log_level(key = :'logging.level')
  case self[key].to_s
  when /\A(0|debug)\z/i then Logger::DEBUG
  when /\A(1|info)\z/i  then Logger::INFO
  when /\A(2|warn)\z/i  then Logger::WARN
  when /\A(3|error)\z/i then Logger::ERROR
  else
    Logger::INFO
  end
end
logger() click to toggle source

Internal Helpers

# File lib/datumfactory/util/config.rb, line 115
def logger
  init_logging! unless @logger
  @logger
end
max_queue_size() click to toggle source
# File lib/datumfactory/util/config.rb, line 195
def max_queue_size
  self[:max_queue_size]
end
params_filters() click to toggle source
# File lib/datumfactory/util/config.rb, line 199
def params_filters
  Array(self[:'request.filter_keys'])
end
public?() click to toggle source
# File lib/datumfactory/util/config.rb, line 138
def public?
  return true if self[:report_data]
  return false if self[:report_data] == false

  !self[:env] || !dev?
end
root_regexp() click to toggle source
# File lib/datumfactory/util/config.rb, line 230
def root_regexp
  return @root_regexp if @root_regexp
  return nil if @no_root

  root = get(:root).to_s
  @no_root = true and return nil unless root =~ NOT_BLANK

  @root_regexp = Regexp.new("^#{ Regexp.escape(root) }")
end
set(key, value) click to toggle source
# File lib/datumfactory/util/config.rb, line 95
def set(key, value)
  self.ruby = ruby.merge(key => value).freeze
  @logger = @backend = nil
end
Also aliased as: []=
to_h(defaults = false)
Alias for: to_hash
to_hash(defaults = false) click to toggle source
# File lib/datumfactory/util/config.rb, line 101
def to_hash(defaults = false)
  hash = [:@ruby, :@env, :@yaml, :@framework].reverse.reduce({}) do |a,e|
    a.merge!(instance_variable_get(e))
  end

  hash = DEFAULTS.merge(hash) if defaults

  undotify_keys(hash.select {|k,v| DEFAULTS.has_key?(k) })
end
Also aliased as: to_h
warn_development?() click to toggle source
# File lib/datumfactory/util/config.rb, line 134
def warn_development?
  dev? && backend.kind_of?(Backend::Null)
end

Private Instance Methods

build_file_logger(path) click to toggle source
# File lib/datumfactory/util/config.rb, line 322
def build_file_logger(path)
  Logger.new(path).tap do |logger|
    logger.level = log_level
    logger.formatter = Logger::Formatter.new
  end
end
build_logger() click to toggle source
# File lib/datumfactory/util/config.rb, line 333
def build_logger
  return ruby[:logger] if ruby[:logger]

  return build_stdout_logger if log_stdout?

  if path = log_path
    FileUtils.mkdir_p(path.dirname) unless path.dirname.writable?
    return build_file_logger(path)
  end

  return framework[:logger] if framework[:logger]

  Logger.new(nil)
end
build_stdout_logger() click to toggle source
# File lib/datumfactory/util/config.rb, line 313
def build_stdout_logger
  logger = Logger.new($stdout)
  logger.formatter = lambda do |severity, datetime, progname, msg|
    "#{msg}\n"
  end
  logger.level = log_level
  Logging::FormattedLogger.new(logger)
end
config_path() click to toggle source
# File lib/datumfactory/util/config.rb, line 279
def config_path
  config_paths.first
end
config_paths() click to toggle source
# File lib/datumfactory/util/config.rb, line 283
def config_paths
  Array(ENV['HONEYBADGER_CONFIG_PATH'] || get(:'config.path')).map do |c|
    locate_absolute_path(c, self[:root])
  end
end
default_backend() click to toggle source
# File lib/datumfactory/util/config.rb, line 289
def default_backend
  return Backend::Server.new(self) if public?

  Backend::Null.new(self)
end
detect_revision!() click to toggle source
# File lib/datumfactory/util/config.rb, line 266
def detect_revision!
  return if self[:revision]

  set(:revision, Util::Revision.detect(self[:root]))
end
includes_token?(obj, value) click to toggle source

Takes an Array and a value and returns true if the value exists in the array in String or Symbol form, otherwise false.

# File lib/datumfactory/util/config.rb, line 354
def includes_token?(obj, value)
  return false unless obj.kind_of?(Array)

  obj.map(&:to_sym).include?(value.to_sym)
end
init_backend!() click to toggle source
# File lib/datumfactory/util/config.rb, line 295
def init_backend!
  if self[:backend].is_a?(String) || self[:backend].is_a?(Symbol)
    @backend = Backend.for(self[:backend].to_sym).new(self)
    return
  end

  if ruby[:backend].respond_to?(:notify)
    @backend = ruby[:backend]
    return
  end

  if ruby[:backend]
    logger.warn(sprintf('Unknown backend: %p; default will be used. Backend must respond to #notify', self[:backend]))
  end

  @backend = default_backend
end
init_logging!() click to toggle source
# File lib/datumfactory/util/config.rb, line 348
def init_logging!
  @logger = Logging::ConfigLogger.new(self, build_logger)
end
load_config_from_disk() { |yml| ... } click to toggle source
# File lib/datumfactory/util/config.rb, line 369
def load_config_from_disk
  if (path = config_paths.find(&:exist?)) && path.file?
    Yaml.new(path, self[:env]).tap do |yml|
      yield(yml) if block_given?
    end
  end
end
locate_absolute_path(path, root) click to toggle source
# File lib/datumfactory/util/config.rb, line 360
def locate_absolute_path(path, root)
  path = Pathname.new(path.to_s)
  if path.absolute?
    path
  else
    Pathname.new(root.to_s).join(path.to_s)
  end
end
log_path() click to toggle source
# File lib/datumfactory/util/config.rb, line 272
def log_path
  return if log_stdout?
  return if !self[:'logging.path']

  locate_absolute_path(self[:'logging.path'], self[:root])
end
log_stdout?() click to toggle source
# File lib/datumfactory/util/config.rb, line 329
def log_stdout?
  self[:'logging.path'] && self[:'logging.path'].to_s.downcase == 'stdout'
end
undotify_keys(hash) click to toggle source
# File lib/datumfactory/util/config.rb, line 377
def undotify_keys(hash)
  {}.tap do |new_hash|
    hash.each_pair do |k,v|
      if k.to_s =~ DOTTED_KEY
        new_hash[$1] ||= {}
        new_hash[$1] = undotify_keys(new_hash[$1].merge({$2 => v}))
      else
        new_hash[k.to_s] = v
      end
    end
  end
end