Sha256: 895f5376d496eb0e34b79b97f13e52e6cdeb519a64e2c7772964e48ce8de5dfe

Contents?: true

Size: 1.84 KB

Versions: 20

Compression:

Stored size: 1.84 KB

Contents

#!/usr/bin/env ruby

require 'yaml'
require 'logger'

module Flapjack

  class Configuration

    DEFAULT_CONFIG_PATH = '/etc/flapjack/flapjack_config.yaml'

    attr_reader :filename

    def initialize(opts = {})
      @logger = opts[:logger]
    end

    def all
      @config_env
    end

    def for_redis
      return unless @config_env

      redis_defaults = {'host' => 'localhost',
                        'port' => 6379,
                        'path' => nil,
                        'db'   => 0}

      @config_env['redis'] = {} unless @config_env.has_key?('redis')

      redis = @config_env['redis']
      redis_defaults.each_pair do |k,v|
        next if redis.has_key?(k) && redis[k] &&
          !(redis[k].is_a?(String) && redis[k].empty?)
        redis[k] = v
      end

      redis_path = (redis['path'] || nil)
      base_opts = {:db => (redis['db'] || 0)}
      redis_config = base_opts.merge(
        (redis_path ? { :path => redis_path } :
                      { :host => (redis['host'] || '127.0.0.1'),
                        :port => (redis['port'] || 6379)})
      )

      redis_config
    end

    def load(filename)
      @filename = nil
      @config_env = nil

      unless File.file?(filename)
        @logger.error "Could not find file '#{filename}'" if @logger
        return
      end

      unless defined?(FLAPJACK_ENV)
        @logger.error "Environment variable 'FLAPJACK_ENV' is not set" if @logger
        return
      end

      config = YAML::load_file(filename)

      if config.nil?
        @logger.error "Could not load config file '#{filename}'" if @logger
        return
      end

      @config_env = config[FLAPJACK_ENV]

      if @config_env.nil?
        @logger.error "No config data for environment '#{FLAPJACK_ENV}' found in '#{filename}'" if @logger
        return
      end

      @filename = filename
    end

  end

end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
flapjack-0.8.11 lib/flapjack/configuration.rb
flapjack-0.8.10 lib/flapjack/configuration.rb
flapjack-0.8.9 lib/flapjack/configuration.rb
flapjack-0.8.8 lib/flapjack/configuration.rb
flapjack-0.8.7 lib/flapjack/configuration.rb
flapjack-0.8.6 lib/flapjack/configuration.rb
flapjack-0.8.5 lib/flapjack/configuration.rb
flapjack-0.8.4 lib/flapjack/configuration.rb
flapjack-0.8.3 lib/flapjack/configuration.rb
flapjack-0.8.2 lib/flapjack/configuration.rb
flapjack-0.8.1 lib/flapjack/configuration.rb
flapjack-0.8.0 lib/flapjack/configuration.rb
flapjack-0.7.35 lib/flapjack/configuration.rb
flapjack-0.7.34 lib/flapjack/configuration.rb
flapjack-0.7.33 lib/flapjack/configuration.rb
flapjack-0.7.32 lib/flapjack/configuration.rb
flapjack-0.7.31 lib/flapjack/configuration.rb
flapjack-0.7.30 lib/flapjack/configuration.rb
flapjack-0.7.29 lib/flapjack/configuration.rb
flapjack-0.7.28 lib/flapjack/configuration.rb