Class: ElasticsearchServerless::Client

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/elasticsearch-serverless.rb

Constant Summary collapse

VALID_PARAMETERS =
[:adapter, :log, :logger, :serializer_class, :trace, :tracer, :headers]

Constants included from API

API::API_NAMESPACES, API::HTTP_DELETE, API::HTTP_GET, API::HTTP_HEAD, API::HTTP_POST, API::HTTP_PUT, API::UPPERCASE_APIS

Instance Method Summary collapse

Methods included from API

serializer

Methods included from API::Actions

#bulk, #clear_scroll, #close_point_in_time, #count, #create, #delete, #delete_by_query, #delete_script, #exists, #exists_source, #explain, #field_caps, #get, #get_script, #get_source, #index, #info, #mget, #msearch, #msearch_template, #mtermvectors, #open_point_in_time, #ping, #put_script, #rank_eval, #reindex, #render_search_template, #scripts_painless_execute, #scroll, #search, #search_mvt, #search_template, #terms_enum, #termvector, #termvectors, #update, #update_by_query

Constructor Details

#initialize(api_key:, url:, arguments: {}) ⇒ Client

Initializes an Elasticsearch Serverless Client

Parameters:

  • :api_key (String)

    Base64 String, format used to authenticate with Elasticsearch

  • :url (String)

    Elasticsearch endpoint

  • :arguments (Hash)

    Other optional arguments.

  • arguments (Hash) (defaults to: {})

    a customizable set of options

Options Hash (arguments:):

  • :adapter (Symbol)

    A specific adapter for Faraday (e.g. ‘:patron`)

  • :log (Boolean)

    Use the default logger (disabled by default)

  • :logger (Object)

    An instance of a Logger-compatible object

  • :trace (Boolean)

    Use the default tracer (disabled by default)

  • :tracer (Object)

    An instance of a Logger-compatible object

  • :serializer_class (Constant)

    A specific serializer class to use, will be initialized by the transport and passed the transport instance

  • :headers (Hash)

    Custom HTTP Request Headers



42
43
44
45
46
# File 'lib/elasticsearch-serverless.rb', line 42

def initialize(api_key:, url:, arguments: {})
  validate_arguments(arguments)
  arguments.merge!(essential_parameters(url, api_key))
  @transport = Elastic::Transport::Client.new(arguments)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Metaprogramming for integration with Transport



69
70
71
72
73
74
75
# File 'lib/elasticsearch-serverless.rb', line 69

def method_missing(name, *args, &block)
  if methods.include?(name)
    super
  else
    @transport.send(name, *args, &block)
  end
end

Instance Method Details

#essential_parameters(url, api_key) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/elasticsearch-serverless.rb', line 48

def essential_parameters(url, api_key)
  {
    host: url,
    transport_options: {
      headers: {
        'Authorization' => "ApiKey #{api_key}",
        'Elastic-Api-Version' => ElasticsearchServerless::API_VERSION,
        user_agent: user_agent
      }
    },
    compression: true
  }
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/elasticsearch-serverless.rb', line 77

def respond_to_missing?(method_name, *args)
  @transport.respond_to?(method_name) || super
end

#user_agentObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/elasticsearch-serverless.rb', line 81

def user_agent
  user_agent = [
    "elasticsearch-serverless-ruby/#{ElasticsearchServerless::VERSION}",
    "elastic-transport-ruby/#{Elastic::Transport::VERSION}",
    "RUBY_VERSION: #{RUBY_VERSION}"
  ]
  if RbConfig::CONFIG && RbConfig::CONFIG['host_os']
    user_agent << "#{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}"
  end
  user_agent.join("; ")
end

#validate_arguments(arguments) ⇒ Object



62
63
64
65
66
# File 'lib/elasticsearch-serverless.rb', line 62

def validate_arguments(arguments)
  arguments.map do |k, _|
    raise ArgumentError, "Parameter #{k}" unless VALID_PARAMETERS.include?(k)
  end
end