Class: ElasticRecord::Connection

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

Constant Summary collapse

METHODS =
{
  head: Net::HTTP::Head,
  get: Net::HTTP::Get,
  post: Net::HTTP::Post,
  put: Net::HTTP::Put,
  delete: Net::HTTP::Delete
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Connection.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/elastic_record/connection.rb', line 9

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

  self.current_server     = next_server
  self.request_count      = 0
  self.max_request_count  = 100
  self.options            = options
  self.bulk_stack        = []
end

Instance Attribute Details

#bulk_stackObject

Returns the value of attribute bulk_stack.



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

def bulk_stack
  @bulk_stack
end

#current_serverObject

Returns the value of attribute current_server.



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

def current_server
  @current_server
end

#max_request_countObject

Returns the value of attribute max_request_count.



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

def max_request_count
  @max_request_count
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/elastic_record/connection.rb', line 5

def options
  @options
end

#request_countObject

Returns the value of attribute request_count.



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

def request_count
  @request_count
end

#serversObject

Returns the value of attribute servers.



5
6
7
# File 'lib/elastic_record/connection.rb', line 5

def servers
  @servers
end

Instance Method Details

#head(path) ⇒ Object



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

def head(path)
  http_request(:head, path).code
end

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



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/elastic_record/connection.rb', line 61

def http_request(method, path, body = nil)
  with_retry do
    request = METHODS[method].new(path)
    request.body = body
    http = new_http

    ActiveSupport::Notifications.instrument("request.elastic_record") do |payload|
      payload[:http]      = http
      payload[:request]   = request
      payload[:response]  = http.request(request)
    end
  end
end

#json_delete(path, json = nil) ⇒ Object



39
40
41
# File 'lib/elastic_record/connection.rb', line 39

def json_delete(path, json = nil)
  json_request :delete, path, json
end

#json_get(path, json = nil) ⇒ Object



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

def json_get(path, json = nil)
  json_request :get, path, json
end

#json_post(path, json = nil) ⇒ Object



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

def json_post(path, json = nil)
  json_request :post, path, json
end

#json_put(path, json = nil) ⇒ Object



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

def json_put(path, json = nil)
  json_request :put, path, json
end

#json_request(method, path, json) ⇒ Object

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/elastic_record/connection.rb', line 43

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

  json = ActiveSupport::JSON.decode response.body
  raise ConnectionError.new(json['error']) if json['error']

  json
end