Class: ElasticRecord::Connection
- Inherits:
-
Object
- Object
- ElasticRecord::Connection
- Defined in:
- lib/elastic_record/connection.rb
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(request_klass, 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(request_klass, path, json) ⇒ Object
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, = {}) 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
7 8 9 |
# File 'lib/elastic_record/connection.rb', line 7 def @options end |
#servers ⇒ Object
: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 |