Sha256: 30311cc3160d65d26a599531c0f778da2f8d5d8aa8a00ef026025ea7bb67496e

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require 'yaml'
require 'erb'
require 'hashie/mash'

class Alerty
  class Config
    class << self
      attr_reader :opts

      def configure(opts)
        @opts = opts
      end

      def config_path
        @config_path ||= opts[:config_path] || ENV['ALERTY_CONFIG_PATH'] || '/etc/alerty/alerty.yml'
      end

      def config
        @config ||=
          begin
            content = File.read(config_path)
            erb = ERB.new(content, nil, '-')
            erb_content = erb.result
            puts erb_content if debug?
            Hashie::Mash.new(YAML.load(erb_content))
          end
      end

      def log_path
        opts[:log_path] || config.log_path || 'STDOUT'
      end

      def log_level
        opts[:log_level] || config.log_level || 'warn'
      end

      def log_shift_age
        opts[:log_shift_age] || config.shift_age || 0
      end

      def log_shift_size
        opts[:log_shift_size] || config.shift_size || 1048576
      end

      def timeout
        opts[:timeout] || config.timeout
      end

      def lock_path
        opts[:lock_path] || config.lock_path
      end

      def retry_limit
        opts[:retry_limit] || config.retry_limit || 0
      end

      def retry_wait
        opts[:retry_wait] || config.retry_wait || 1.0
      end

      def debug?
        !!opts[:debug]
      end

      def retry_interval
        @random ||= Random.new
        randomness = retry_wait * 0.125
        retry_wait + @random.rand(-randomness .. randomness)
      end

      def plugins
        @plugins ||= config.fetch('plugins').map do |plugin|
          require "alerty/plugin/#{plugin.type}"
          class_name = "Alerty::Plugin::#{StringUtil.camelize(plugin.type)}" 
          Object.const_get(class_name).new(plugin)
        end
      end

      # for debug
      def reset
        @config_path = nil
        @config = nil
        @plugins = nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
alerty-0.2.0 lib/alerty/config.rb