Class: Elasticshell::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticshell/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/elasticshell/client.rb', line 8

def initialize options={}
  @options = options
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/elasticshell/client.rb', line 18

def connected?
  @client
end

#request(verb, params = {}, options = {}, body = '') ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/elasticshell/client.rb', line 26

def request verb, params={}, options={}, body=''
  raise ClientError.new("Not connected to any Elasticsearch servers.") unless connected?
  safe        = options.delete(:safely)
  safe_return = options.delete(:return)
  
  # Log by default
  log_request(verb, params, options) unless options.delete(:log) == false
  
  begin
    perform_request(verb, params, options, body)
  rescue ElasticSearch::RequestError, ArgumentError => e
    if safe
      safe_return
    else
      raise ClientError.new(e.message)
    end
  end
end

#safely(verb, params = {}, options = {}, body = '') ⇒ Object



22
23
24
# File 'lib/elasticshell/client.rb', line 22

def safely verb, params={}, options={}, body=''
  request(verb, params, options.merge(:safely => true), body)
end

#safely_connect(options = {}) ⇒ Object

Raises:



12
13
14
15
16
# File 'lib/elasticshell/client.rb', line 12

def safely_connect options={}
  servers = @options.merge(options)[:servers]
  raise ClientError.new("Must provide at least one server to connect to.") if servers.empty?
  connect(servers.dup, first_attempt=true)
end