Sha256: b8a77e0a7e124eee4771d9185626f5bdf4ebcada372e7a8d9fd39293a6d147df

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

module Waistband
  class Client

    class << self

      def from_config(config_hash)
        new(
          config_hash['servers'], randomize_hosts: true,
          retry_on_failure: config_hash['retries'],
          reload_on_failure: config_hash['reload_on_failure'],
          timeout: config_hash['timeout'],
          adapter: config_hash['adapter']
        )
      end

    end

    attr_reader :servers

    def initialize(servers, options = {})
      @servers = servers
      @randomize_hosts = options.fetch(:randomize_hosts, true)
      @retry_on_failure = options[:retry_on_failure]
      @reload_on_failure = options[:reload_on_failure]
      @timeout = options[:timeout]
      @adapter = options[:adapter]
    end

    def connection
      @connection ||= Elasticsearch::Client.new config_hash
    end

    def config_hash
      {
        adapter: @adapter,
        hosts: hosts,
        randomize_hosts: @randomize_hosts,
        retry_on_failure: @retry_on_failure,
        reload_on_failure: @reload_on_failure,
        transport_options: {
          request: {
            open_timeout: @timeout,
            timeout: @timeout
          }
        }
      }
    end

    def method_missing(method_name, *args, &block)
      return connection.send(method_name, *args, &block) if connection.respond_to?(method_name)
      super
    end

    private

      def hosts
        @hosts ||= @servers.map do |server_name, config|
          config
        end
      end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
waistband-6.1.1 lib/waistband/client.rb
waistband-6.1.0 lib/waistband/client.rb
waistband-6.0.0 lib/waistband/client.rb
waistband-0.16.7 lib/waistband/client.rb
waistband-0.16.6 lib/waistband/client.rb
waistband-0.16.5 lib/waistband/client.rb
waistband-0.16.3 lib/waistband/client.rb
waistband-0.16.2 lib/waistband/client.rb
waistband-0.16.1 lib/waistband/client.rb
waistband-0.15.2 lib/waistband/client.rb