Class: ElasticRecord::Connection
- Inherits:
-
Object
- Object
- ElasticRecord::Connection
- 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
-
#options ⇒ Object
:timeout: 10 :retries: 2 :auto_discovery: false.
-
#servers ⇒ Object
:timeout: 10 :retries: 2 :auto_discovery: false.
Instance Method Summary collapse
- #head(path) ⇒ Object
- #http_request(method, path, body = nil) ⇒ Object
-
#initialize(servers, options = {}) ⇒ Connection
constructor
A new instance of Connection.
- #json_delete(path, json = nil) ⇒ Object
- #json_get(path, json = nil) ⇒ Object
- #json_post(path, json = nil) ⇒ Object
- #json_put(path, json = nil) ⇒ Object
- #json_request(method, path, json) ⇒ Object
Constructor Details
#initialize(servers, options = {}) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 15 16 17 18 |
# File 'lib/elastic_record/connection.rb', line 10 def initialize(servers, = {}) if servers.is_a?(Array) self.servers = servers else self.servers = servers.split(',') end self. = end |
Instance Attribute Details
#options ⇒ Object
:timeout: 10 :retries: 2 :auto_discovery: false
9 10 11 |
# File 'lib/elastic_record/connection.rb', line 9 def @options end |
#servers ⇒ Object
:timeout: 10 :retries: 2 :auto_discovery: false
9 10 11 |
# File 'lib/elastic_record/connection.rb', line 9 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(:head, path).code end |
#http_request(method, path, body = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/elastic_record/connection.rb', line 58 def http_request(method, path, body = nil) request = METHODS[method].new(path) request.body = body ActiveSupport::Notifications.instrument("request.elastic_record") do |payload| payload[:method] = method payload[:request_uri] = path payload[:response] = http.request(request) 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
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) ? ActiveSupport::JSON.encode(json) : json response = http_request(method, path, body) json = ActiveSupport::JSON.decode response.body raise json['error'] if json['error'] json end |