Class: ElasticSearchThrift::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_search_thrift/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  host = options[:host] || options['host'] || '127.0.0.1'
  port = options[:port] || options['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

#clientObject (readonly)

Returns the value of attribute client.



27
28
29
# File 'lib/elastic_search_thrift/client.rb', line 27

def client
  @client
end

#protocolObject (readonly)

Returns the value of attribute protocol.



27
28
29
# File 'lib/elastic_search_thrift/client.rb', line 27

def protocol
  @protocol
end

#socketObject (readonly)

Returns the value of attribute socket.



27
28
29
# File 'lib/elastic_search_thrift/client.rb', line 27

def socket
  @socket
end

#transportObject (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

#closeObject



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

#openObject



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

Returns:

  • (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