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.



11
12
13
14
# File 'lib/cluster_point/cluster_point_adapter.rb', line 11

def initialize
  load_config
  @auth = {:username => @config["username"], :password => @config["password"]}
end

Instance Method Details

#delete(id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/cluster_point/cluster_point_adapter.rb', line 32

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



54
55
56
57
58
59
60
61
62
63
# File 'lib/cluster_point/cluster_point_adapter.rb', line 54

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



24
25
26
27
28
29
30
# File 'lib/cluster_point/cluster_point_adapter.rb', line 24

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



43
44
45
46
47
48
49
50
51
52
# File 'lib/cluster_point/cluster_point_adapter.rb', line 43

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



16
17
18
19
20
21
22
# File 'lib/cluster_point/cluster_point_adapter.rb', line 16

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(hash = {}, docs = 20, offset = 0) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cluster_point/cluster_point_adapter.rb', line 65

def where(hash={}, docs=20, offset=0)
  xml = hash.to_xml(skip_instruct: true, :indent => 1)
  options = { body: [{ query: 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