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
# File 'lib/elastic_record/connection.rb', line 9

def initialize(servers, options = {})
  self.servers = Array(servers)

  @shuffled_servers       = nil
  self.current_server     = next_server
  self.request_count      = 0
  self.max_request_count  = 100
  self.options            = options.symbolize_keys
  self.bulk_actions       = nil
end

Instance Attribute Details

#bulk_actionsObject

Returns the value of attribute bulk_actions.



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

def bulk_actions
  @bulk_actions
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



20
21
22
# File 'lib/elastic_record/connection.rb', line 20

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

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



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

def http_request(method, path, body = nil)
  request = new_request(method, path, 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

#http_request_with_retry(*args) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/elastic_record/connection.rb', line 50

def http_request_with_retry(*args)
  with_retry do
    response = http_request(*args)

    raise ConnectionError.new(response.code, response.body) if response.code.to_i >= 500

    response
  end
end

#json_delete(path, json = nil) ⇒ Object



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

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

#json_get(path, json = nil) ⇒ Object



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

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

#json_post(path, json = nil) ⇒ Object



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

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

#json_put(path, json = nil) ⇒ Object



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

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

#json_request(method, path, json) ⇒ Object

Raises:



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

def json_request(method, path, json)
  body = json.is_a?(Hash) ? JSON.generate(json) : json
  response = http_request_with_retry(method, path, body)

  json = JSON.parse(response.body)
  raise ConnectionError.new(response.code, json['error']) if json['error']

  json
end

#new_request(method, path, body) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/elastic_record/connection.rb', line 78

def new_request(method, path, body)
  request = METHODS[method].new(path.starts_with?('/') ? path : "/#{path}")
  request.basic_auth(options[:username], options[:password]) if options[:username].present?
  request.body = body
  request.content_type = 'application/json'
  request
end