Class: ClusterPoint::ClusterPointAdapter
- Inherits:
-
Object
- Object
- ClusterPoint::ClusterPointAdapter
- Includes:
- Configuration, HTTParty
- Defined in:
- lib/cluster_point/cluster_point_adapter.rb
Instance Method Summary collapse
- #delete(id) ⇒ Object
- #get(id) ⇒ Object
-
#initialize ⇒ ClusterPointAdapter
constructor
A new instance of ClusterPointAdapter.
- #insert(document) ⇒ Object
- #query(text, docs = 10, offset = 0) ⇒ Object
- #update(document) ⇒ Object
- #where(conditions = {}, ordering = {string: {id: :ascending}}, docs = 20, offset = 0) ⇒ Object
Methods included from Configuration
Constructor Details
#initialize ⇒ ClusterPointAdapter
Returns a new instance of ClusterPointAdapter.
10 11 12 13 |
# File 'lib/cluster_point/cluster_point_adapter.rb', line 10 def initialize load_config @auth = {:username => @config["username"], :password => @config["password"]} end |
Instance Method Details
#delete(id) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cluster_point/cluster_point_adapter.rb', line 31 def delete(id) = { basic_auth: @auth, headers: { 'Content-Type' => 'application/json, charset=utf-8' } } resp = self.class.delete('/' + id + '.json', ) if resp.code == 200 resp.body else raise "Error while getting data: " + resp.response.to_s end end |
#get(id) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cluster_point/cluster_point_adapter.rb', line 53 def get(id) = { basic_auth: @auth, headers: { 'Content-Type' => 'application/json, charset=utf-8' } } resp = self.class.get('/' + id + '.json', ) if resp.code == 200 resp.body else raise "Error while getting data: " + resp.response.to_s end end |
#insert(document) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/cluster_point/cluster_point_adapter.rb', line 23 def insert(document) = { body: document.as_json, basic_auth: @auth, headers: { 'Content-Type' => 'application/json, charset=utf-8' }} resp = self.class.put("/.json", ) resp.body end |
#query(text, docs = 10, offset = 0) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/cluster_point/cluster_point_adapter.rb', line 42 def query(text, docs = 10, offset=0) = { body: [{ query: text, docs: docs, offset: offset }].to_json, basic_auth: @auth, headers: { 'Content-Type' => 'application/json, charset=utf-8' } } resp = self.class.post('/_search.json', ) resp.body end |
#update(document) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/cluster_point/cluster_point_adapter.rb', line 15 def update(document) = { body: document.as_json, basic_auth: @auth, headers: { 'Content-Type' => 'application/json, charset=utf-8' }} resp = self.class.put("/#{document.id}.json", ) resp.body end |
#where(conditions = {}, ordering = {string: {id: :ascending}}, docs = 20, offset = 0) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cluster_point/cluster_point_adapter.rb', line 64 def where(conditions={}, ordering={string: {id: :ascending}}, docs=20, offset=0) query_xml = conditions.to_xml(skip_instruct: true, skip_types: true, indent: 1) ordering_xml = ordering.to_xml(skip_instruct: true, skip_types: true, indent: 1) = { body: [{ query: query_xml.lines.to_a[1..-2].join, ordering: ordering_xml.lines.to_a[1..-2].join, docs: docs, offset: offset }].to_json, basic_auth: @auth, headers: { 'Content-Type' => 'application/json, charset=utf-8' } } resp = self.class.post('/_search.json', ) resp.body end |