Sha256: a7093967f18fe310ab646ce16a8e3bf129e6ebd0bd22c4fb6afefc5b6dc5cd3b

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'yaml'
require 'singleton'
require 'active_support/core_ext/hash/indifferent_access'
require 'digest/sha1'

module Waistband
  class Configuration

    include Singleton

    attr_accessor :config_dir
    attr_reader   :env

    def initialize
      @yml_config = {}
      @indexes    = {}
    end

    def setup
      self.config_dir = default_config_dir unless config_dir

      raise "Please define a valid `config_dir` configuration variable!"  unless config_dir
      raise "Couldn't find configuration directory #{config_dir}"         unless File.exist?(config_dir)

      @env        ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
      @yml_config   = YAML.load_file("#{config_dir}/waistband.yml")[@env].with_indifferent_access
      @adapter      = @yml_config.delete('adapter')
    end

    def index(name)
      @indexes[name] ||= YAML.load_file("#{config_dir}/waistband_#{name}.yml")[@env].with_indifferent_access
    end

    def method_missing(method_name, *args, &block)
      return @yml_config[method_name] if @yml_config[method_name]
      super
    end

    def hosts
      @hosts ||= @yml_config['servers'].map do |server_name, config|
        config
      end
    end

    def client
      client_hash = {
        adapter: @adapter,
        hosts: hosts,
        randomize_hosts: true,
        retry_on_failure: retries,
        reload_on_failure: reload_on_failure,
        transport_options: {
          request: {
            open_timeout: @yml_config['timeout'],
            timeout: @yml_config['timeout']
          }
        }
      }

      Elasticsearch::Client.new client_hash
    end

    private

      def default_config_dir
        @default_config_dir ||= begin
          return nil unless defined?(Rails)
          File.join(Rails.root, 'config')
        end
      end

    # /private

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
waistband-0.9.4 lib/waistband/configuration.rb