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.



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

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

  self.current_server = choose_server
  self.request_count = 0
  self.max_request_count = 100
  self.options = options
end

Instance Attribute Details

#current_serverObject

Returns the value of attribute current_server.



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

def current_server
  @current_server
end

#max_request_countObject

Returns the value of attribute max_request_count.



10
11
12
# File 'lib/elastic_record/connection.rb', line 10

def max_request_count
  @max_request_count
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#request_countObject

Returns the value of attribute request_count.



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

def request_count
  @request_count
end

#serversObject

Returns the value of attribute servers.



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

def servers
  @servers
end

Instance Method Details

#head(path) ⇒ Object



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

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

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



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

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

#json_delete(path, json = nil) ⇒ Object



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

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

#json_get(path, json = nil) ⇒ Object



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

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

#json_post(path, json = nil) ⇒ Object



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

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

#json_put(path, json = nil) ⇒ Object



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

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

#json_request(method, path, json) ⇒ Object

Raises:



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

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