Class: ElasticSearchThrift::Client
- Inherits:
-
Object
- Object
- ElasticSearchThrift::Client
- Defined in:
- lib/elastic_search_thrift/client.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#transport ⇒ Object
readonly
Returns the value of attribute transport.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #delete(path, parameters = {}, body = {}) ⇒ Object
- #get(path, parameters = {}, body = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #normalize_hash(hash) ⇒ Object
- #open ⇒ Object
- #open? ⇒ Boolean
- #post(path, parameters = {}, body = {}) ⇒ Object
- #put(path, parameters = {}, body = {}) ⇒ Object
- #request(method, uri, parameters = {}, body = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/elastic_search_thrift/client.rb', line 11 def initialize( = {}) host = [:host] || ['host'] || '127.0.0.1' port = [:port] || ['port'] || 9500 @socket = Thrift::Socket.new(host, port) @transport = Thrift::BufferedTransport.new(socket) @protocol = Thrift::BinaryProtocol.new(transport) @client = ElasticSearchThrift::Rest::Client.new(protocol) # Create finalizer proc in separate scope so it doesn't prevent GC. ObjectSpace.define_finalizer(self, self.class.finalizer_for(self)) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
27 28 29 |
# File 'lib/elastic_search_thrift/client.rb', line 27 def client @client end |
#protocol ⇒ Object (readonly)
Returns the value of attribute protocol.
27 28 29 |
# File 'lib/elastic_search_thrift/client.rb', line 27 def protocol @protocol end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
27 28 29 |
# File 'lib/elastic_search_thrift/client.rb', line 27 def socket @socket end |
#transport ⇒ Object (readonly)
Returns the value of attribute transport.
27 28 29 |
# File 'lib/elastic_search_thrift/client.rb', line 27 def transport @transport end |
Class Method Details
.finalizer_for(instance) ⇒ Object
23 24 25 |
# File 'lib/elastic_search_thrift/client.rb', line 23 def self.finalizer_for(instance) -> { instance.close } end |
Instance Method Details
#close ⇒ Object
70 71 72 |
# File 'lib/elastic_search_thrift/client.rb', line 70 def close @transport.close if open? end |
#delete(path, parameters = {}, body = {}) ⇒ Object
41 42 43 |
# File 'lib/elastic_search_thrift/client.rb', line 41 def delete(path, parameters = {}, body = {}) request(ElasticSearchThrift::Method::DELETE, path, parameters, body) end |
#get(path, parameters = {}, body = {}) ⇒ Object
29 30 31 |
# File 'lib/elastic_search_thrift/client.rb', line 29 def get(path, parameters = {}, body = {}) request(ElasticSearchThrift::Method::GET, path, parameters, body) end |
#normalize_hash(hash) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/elastic_search_thrift/client.rb', line 74 def normalize_hash(hash) return hash if hash.empty? result = {} hash.each do |key, value| result[key.to_s] = value.to_s end result end |
#open ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/elastic_search_thrift/client.rb', line 55 def open @transport.open unless open? if block_given? begin yield self ensure close end end end |
#open? ⇒ Boolean
66 67 68 |
# File 'lib/elastic_search_thrift/client.rb', line 66 def open? @transport.open? end |
#post(path, parameters = {}, body = {}) ⇒ Object
37 38 39 |
# File 'lib/elastic_search_thrift/client.rb', line 37 def post(path, parameters = {}, body = {}) request(ElasticSearchThrift::Method::POST, path, parameters, body) end |
#put(path, parameters = {}, body = {}) ⇒ Object
33 34 35 |
# File 'lib/elastic_search_thrift/client.rb', line 33 def put(path, parameters = {}, body = {}) request(ElasticSearchThrift::Method::PUT, path, parameters, body) end |
#request(method, uri, parameters = {}, body = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/elastic_search_thrift/client.rb', line 45 def request(method, uri, parameters = {}, body = {}) request = RestRequest.new request.method = method request.uri = uri request.parameters = normalize_hash(parameters) request.headers = {} request.body = body.to_json @client.execute(request) end |