Sha256: 7e3681d15a726d17fef75abc20729c8260afb20f6c7ca09453535d47d385753a

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require "yaml"

module Librato
  module Rails
    # Adds yaml-based config and extra properties to the configuration
    # class provided by librato-rack
    #
    # https://github.com/librato/librato-rack/blob/master/lib/librato/rack/configuration.rb
    #
    class Configuration < Rack::Configuration
      CONFIG_SETTABLE = %w{user token flush_interval log_level prefix source source_pids proxy}

      attr_accessor :config_by, :config_file

      # options:
      # * :config_file - alternate config file location
      #
      def initialize(options={})
        self.config_file = options[:config_file] || 'config/librato.yml'
        super()
        self.log_prefix = '[librato-rails] '
      end

      # detect and load configuration from config file or env vars
      def load_configuration
        if self.config_file && File.exists?(self.config_file)
          configure_with_config_file
        else
          self.config_by = :environment
          super
        end

        # respect autorun and log_level env vars regardless of config method
        self.autorun = detect_autorun
        self.log_level = :info if log_level.blank?
        self.log_level = ENV['LIBRATO_LOG_LEVEL'] if ENV['LIBRATO_LOG_LEVEL']
      end

      private

      def configure_with_config_file
        self.config_by = :config_file
        env_specific = YAML.load(ERB.new(File.read(config_file)).result)[::Rails.env]
        if env_specific
          settable = CONFIG_SETTABLE & env_specific.keys
          settable.each { |key| self.send("#{key}=", env_specific[key]) }
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
librato-rails-1.2.0 lib/librato/rails/configuration.rb
librato-rails-1.1.0 lib/librato/rails/configuration.rb