Class: EsClient::Client

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

RETRY_TIMES =
1

Instance Method Summary collapse

Constructor Details

#initialize(host, options) ⇒ Client

Returns a new instance of Client.



5
6
7
8
# File 'lib/es_client/client.rb', line 5

def initialize(host, options)
  @host = host
  @options = options
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



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

def delete(path, options={})
  request options.merge(method: :delete, path: path)
end

#get(path, options = {}) ⇒ Object



10
11
12
# File 'lib/es_client/client.rb', line 10

def get(path, options={})
  request options.merge(method: :get, path: path)
end

#head(path, options = {}) ⇒ Object



26
27
28
# File 'lib/es_client/client.rb', line 26

def head(path, options={})
  request options.merge(method: :head, path: path)
end

#httpObject



49
50
51
# File 'lib/es_client/client.rb', line 49

def http
  @http ||= Excon.new(@host, @options)
end

#log(message, level = :info) ⇒ Object



57
58
59
# File 'lib/es_client/client.rb', line 57

def log(message, level=:info)
  EsClient.logger.try!(level, message)
end

#post(path, options = {}) ⇒ Object



14
15
16
# File 'lib/es_client/client.rb', line 14

def post(path, options={})
  request options.merge(method: :post, path: path)
end

#put(path, options = {}) ⇒ Object



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

def put(path, options={})
  request options.merge(method: :put, path: path)
end

#reconnect!Object



53
54
55
# File 'lib/es_client/client.rb', line 53

def reconnect!
  @http = nil
end

#request(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/es_client/client.rb', line 30

def request(options)
  retry_times = 0
  begin
    raw_response = http.request(options)
    response = ::EsClient::Response.new(raw_response.body, raw_response.status, raw_response.headers)
    EsClient.logger.request(http, response, options) if EsClient.logger.try!(:debug?)
    response
  rescue Excon::Errors::SocketError => e
    if retry_times >= RETRY_TIMES
      exception = ::EsClient::Client::Error.new(e, self)
      EsClient.logger.exception(exception, http, options) if EsClient.logger
      raise exception
    end
    retry_times += 1
    reconnect!
    retry
  end
end