Class: ElasticRecord::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_record/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers, options = {}) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
14
15
16
# File 'lib/elastic_record/connection.rb', line 8

def initialize(servers, options = {})
  if servers.is_a?(Array)
    self.servers = servers
  else
    self.servers = servers.split(',')
  end

  self.options = options
end

Instance Attribute Details

#optionsObject

:timeout: 10 :retries: 2 :auto_discovery: false



7
8
9
# File 'lib/elastic_record/connection.rb', line 7

def options
  @options
end

#serversObject

:timeout: 10 :retries: 2 :auto_discovery: false



7
8
9
# File 'lib/elastic_record/connection.rb', line 7

def servers
  @servers
end

Instance Method Details

#head(path) ⇒ Object



18
19
20
# File 'lib/elastic_record/connection.rb', line 18

def head(path)
  http_request(Net::HTTP::Head, path).code
end

#http_request(request_klass, path, body = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/elastic_record/connection.rb', line 48

def http_request(request_klass, path, body = nil)
  request = request_klass.new(path)
  request.body = body

  http.request(request)
end

#json_delete(path, json = nil) ⇒ Object



34
35
36
# File 'lib/elastic_record/connection.rb', line 34

def json_delete(path, json = nil)
  json_request Net::HTTP::Delete, path, json
end

#json_get(path, json = nil) ⇒ Object



22
23
24
# File 'lib/elastic_record/connection.rb', line 22

def json_get(path, json = nil)
  json_request Net::HTTP::Get, path, json
end

#json_post(path, json = nil) ⇒ Object



26
27
28
# File 'lib/elastic_record/connection.rb', line 26

def json_post(path, json = nil)
  json_request Net::HTTP::Post, path, json
end

#json_put(path, json = nil) ⇒ Object



30
31
32
# File 'lib/elastic_record/connection.rb', line 30

def json_put(path, json = nil)
  json_request Net::HTTP::Put, path, json
end

#json_request(request_klass, path, json) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/elastic_record/connection.rb', line 38

def json_request(request_klass, path, json)
  body = json.is_a?(Hash) ? ActiveSupport::JSON.encode(json) : json
  response = http_request(request_klass, path, body)
  json = ActiveSupport::JSON.decode response.body

  raise json['error'] if json['error']

  json
end