class Elasticsearch::Client

Attributes

transport[RW]

Public Class Methods

new(arguments = {}, &block) click to toggle source

See Elasticsearch::Transport::Client for initializer parameters

# File lib/elasticsearch.rb, line 33
def initialize(arguments = {}, &block)
  @verified = false
  @transport = Elasticsearch::Transport::Client.new(arguments, &block)
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/elasticsearch.rb, line 38
def method_missing(name, *args, &block)
  if name == :perform_request
    verify_elasticsearch unless @verified
    @transport.perform_request(*args, &block)
  else
    super
  end
end

Private Instance Methods

elasticsearch_validation_request() click to toggle source
# File lib/elasticsearch.rb, line 91
def elasticsearch_validation_request
  @transport.perform_request('GET', '/')
end
verify_elasticsearch() click to toggle source
# File lib/elasticsearch.rb, line 49
def verify_elasticsearch
  begin
    response = elasticsearch_validation_request
  rescue Elasticsearch::Transport::Transport::Errors::Unauthorized,
         Elasticsearch::Transport::Transport::Errors::Forbidden
    @verified = true
    warn(SECURITY_PRIVILEGES_VALIDATION_WARNING)
    return
  end

  body = if response.headers['content-type'] == 'application/yaml'
           require 'yaml'
           YAML.load(response.body)
         else
           response.body
         end
  version = body.dig('version', 'number')
  verify_with_version_or_header(body, version, response.headers)
end
verify_with_version_or_header(body, version, headers) click to toggle source
# File lib/elasticsearch.rb, line 69
def verify_with_version_or_header(body, version, headers)
  raise Elasticsearch::UnsupportedProductError if version.nil? || version < '6.0.0'

  if version == '7.x-SNAPSHOT' || Gem::Version.new(version) >= Gem::Version.new('7.14-SNAPSHOT')
    raise Elasticsearch::UnsupportedProductError unless headers['x-elastic-product'] == 'Elasticsearch'

    @verified = true
  elsif Gem::Version.new(version) > Gem::Version.new('6.0.0') &&
        Gem::Version.new(version) < Gem::Version.new('7.0.0')
    raise Elasticsearch::UnsupportedProductError unless
      body['tagline'] == YOU_KNOW_FOR_SEARCH

    @verified = true
  elsif Gem::Version.new(version) >= Gem::Version.new('7.0.0') &&
        Gem::Version.new(version) < Gem::Version.new('7.14-SNAPSHOT')
    raise Elasticsearch::UnsupportedProductError unless body['tagline'] == YOU_KNOW_FOR_SEARCH
    raise Elasticsearch::UnsupportedProductError.new(NOT_SUPPORTED_ELASTICSEARCH_WARNING) unless body.dig('version', 'build_flavor') == 'default'

    @verified = true
  end
end