Class: ClusterPoint::ClusterPointAdapter

Inherits:
Object
  • Object
show all
Includes:
Configuration, HTTParty
Defined in:
lib/cluster_point/cluster_point_adapter.rb

Instance Method Summary collapse

Methods included from Configuration

#get_base_uri, #load_config

Constructor Details

#initializeClusterPointAdapter

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)
  options = { basic_auth: @auth,
              headers: { 'Content-Type' => 'application/json, charset=utf-8' } }
  resp = self.class.delete('/' + id + '.json', options)
  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)
  options = { basic_auth: @auth,
              headers: { 'Content-Type' => 'application/json, charset=utf-8' } }
  resp = self.class.get('/' + id + '.json', options)
  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)
  options = { body: document.as_json,
              basic_auth: @auth,
              headers: { 'Content-Type' => 'application/json, charset=utf-8' }}
  resp = self.class.put("/.json", options)
  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)
  options = { 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', options)
  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)
  options = { body: document.as_json,
              basic_auth: @auth,
              headers: { 'Content-Type' => 'application/json, charset=utf-8' }}
  resp = self.class.put("/#{document.id}.json", options)
  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)
  options = { 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', options)
  resp.body
end