Class: Elastirad::Client
- Inherits:
-
Object
- Object
- Elastirad::Client
- Includes:
- Elasticsearch::API
- Defined in:
- lib/elastirad/client.rb
Instance Attribute Summary collapse
-
#sIndex ⇒ Object
Returns the value of attribute sIndex.
Instance Method Summary collapse
-
#initialize(dOptions = {}) ⇒ Client
constructor
A new instance of Client.
- #perform_request(method, path, params, body) ⇒ Object
- #rad_index(dEsRes = {}) ⇒ Object
- #rad_request(dEsReq = {}) ⇒ Object
- #rad_request_all(dEsReq = {}) ⇒ Object
Constructor Details
#initialize(dOptions = {}) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/elastirad/client.rb', line 10 def initialize(dOptions={}) iPort = iPort.to_i if iPort.is_a?(String) @sProtocol = dOptions.has_key?(:protocol) && dOptions[:protocol] \ ? dOptions[:protocol] : 'http' @sHostname = dOptions.has_key?(:hostname) && dOptions[:hostname] \ ? dOptions[:hostname] : 'localhost' @iPort = dOptions.has_key?(:port) && dOptions[:port] \ ? dOptions[:port].to_i : 9200 @sIndex = dOptions.has_key?(:index) && dOptions[:index] \ ? dOptions[:index].strip : nil @sUrl = makeUrl(@sProtocol,@sHostname,@iPort) @oFaraday = Faraday::Connection.new url: @sUrl || 'http://localhost:9200' @dVerbs = {:put=>1,:get=>1,:post=>1,:delete=>1} end |
Instance Attribute Details
#sIndex ⇒ Object
Returns the value of attribute sIndex.
8 9 10 |
# File 'lib/elastirad/client.rb', line 8 def sIndex @sIndex end |
Instance Method Details
#perform_request(method, path, params, body) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/elastirad/client.rb', line 41 def perform_request(method,path,params,body) @oFaraday.run_request \ method.downcase.to_sym, path, body, {'Content-Type' => 'application/json'} end |
#rad_index(dEsRes = {}) ⇒ Object
25 26 27 28 |
# File 'lib/elastirad/client.rb', line 25 def rad_index(dEsRes={}) dEsRes[:verb] = :put return self.rad_request(dEsRes) end |
#rad_request(dEsReq = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/elastirad/client.rb', line 30 def rad_request(dEsReq={}) oEsRes = self.perform_request \ getVerbFromEsReq(dEsReq), getPathFromEsReq(dEsReq), nil, getBodyFromEsReq(dEsReq) dEsResBody = oEsRes.body \ ? MultiJson.decode( oEsRes.body, :symbolize_keys => true ) : nil end |
#rad_request_all(dEsReq = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/elastirad/client.rb', line 49 def rad_request_all(dEsReq={}) if dEsReq.has_key?(:body) if dEsReq[:body].is_a?(String) dEsReqBody = dEsReq[:body] \ ? MultiJson.decode( dEsResBody, :symbolize_keys => true ) : {} dEsReq[:body] = dEsReqBody end else dEsReq[:body] = {} end dEsReq[:body][:from] = 0 dEsRes1 = self.rad_request( dEsReq ) dEsRes = dEsRes1 if !dEsReq.has_key?(:verb) || dEsReq[:verb] == :get || dEsReq[:verb].downcase == 'get' if dEsRes1.has_key?(:hits) && dEsRes1[:hits].has_key?(:total) iHitsTotal = dEsRes1[:hits][:total].to_i iHitsSize = dEsRes1[:hits][:hits].length.to_i if iHitsTotal > iHitsSize dEsReq[:body][:size] = iHitsTotal dEsRes2 = self.rad_request( dEsReq ) dEsRes = dEsRes2 end end end return dEsRes end |