Class: Elasticsearch::Client::Base

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

Direct Known Subclasses

ESClient

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 9200, logger = Logger.new(STDOUT)) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
# File 'lib/elasticsearch/client/base.rb', line 7

def initialize(host = 'localhost', port = 9200, logger = Logger.new(STDOUT))
  @host = host
  @port = port
  @logger = logger
end

Instance Method Details

#_get(uri, params = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/elasticsearch/client/base.rb', line 31

def _get(uri, params = nil)
  url = _build_url(uri)
  opts = _prep_opts(params)

  begin
    return RestClient.get url, opts
  rescue Exception => e
    @logger.error(e.message)
    raise e
    raise IOError.new "Unable to complete get request: #{e}"
  end
end

#_prep_opts(params) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/elasticsearch/client/base.rb', line 56

def _prep_opts(params)
  unless params.nil?
           {:params => params}
         else
           {}
         end
end

#_put(uri, body, params = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/elasticsearch/client/base.rb', line 44

def _put(uri, body, params = nil)
  url = _build_url(uri)
  opts = _prep_opts(params)

  begin
    return RestClient.put url, body, opts
  rescue Exception => e
    @logger.error(e.message)
    raise IOError.new "Unable to complete put request: #{e}"
  end
end

#get(uri, params = nil) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/elasticsearch/client/base.rb', line 13

def get(uri, params = nil)
  raw = _get(uri, params)
  if !raw.headers[:content_type].nil? && raw.headers[:content_type][/json/]
    JSON.parse(raw)
  else
    raw
  end
end

#put(uri, body, params = nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/elasticsearch/client/base.rb', line 22

def put(uri, body, params = nil)
  raw = _put(uri, body, params)
  if !raw.headers[:content_type].nil? && raw.headers[:content_type][/json/]
    JSON.parse(raw)
  else
    raw
  end
end