Class: Ruxster::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruxster/base.rb

Direct Known Subclasses

Edge, Vertex

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Base

Returns a new instance of Base.



3
4
5
# File 'lib/ruxster/base.rb', line 3

def initialize(hash={})
  hash.each { |k,v| properties_hash[k] = v }
end

Class Method Details

.connect_stringObject



26
27
28
# File 'lib/ruxster/base.rb', line 26

def self.connect_string
  Ruxster::Config.connect_string
end

.create_index(automatic = true) ⇒ Object



57
58
59
60
61
# File 'lib/ruxster/base.rb', line 57

def self.create_index(automatic=true)
  url = Base.connect_string + "/indices/index?class=#{self.index_type}"
  url += "&type=automatic" if automatic
  Excon.post(url)
end

.find(key, value) ⇒ Object



68
69
70
71
# File 'lib/ruxster/base.rb', line 68

def self.find(key, value)
  response = JSON.parse(Excon.get(Base.connect_string + "/indices/#{url_directory}?key=#{key}&value=#{value}").body)
  response["results"]
end

.get(id) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/ruxster/base.rb', line 30

def self.get(id)
  object = nil
  response = Excon.get(Base.connect_string + "/#{self.url_directory}/#{id}")
  if response
    results = JSON.parse(response.body)["results"]
    object = self.new(results) if results
  end
  object
end

.indicesObject



63
64
65
66
# File 'lib/ruxster/base.rb', line 63

def self.indices
  response = JSON.parse(Excon.get(Base.connect_string + "/indices").body)
  response["results"]
end

Instance Method Details

#createObject



40
41
42
43
44
45
46
47
# File 'lib/ruxster/base.rb', line 40

def create
  response = Excon.post(Base.connect_string + "/#{self.class.url_directory}?" + parameterize)
  if response
    results = JSON.parse(response.body)["results"]
    properties_hash["_id"] = results["_id"]
    properties_hash["_type"] = results["_type"]
  end
end

#destroyObject



53
54
55
# File 'lib/ruxster/base.rb', line 53

def destroy
  response = Excon.delete(Base.connect_string + "/#{self.class.url_directory}/#{self.id}")
end

#idObject



13
14
15
# File 'lib/ruxster/base.rb', line 13

def id
  properties_hash["_id"]
end

#id=(value) ⇒ Object



16
17
18
# File 'lib/ruxster/base.rb', line 16

def id=(value)
  properties_hash["_id"] = value
end

#parameterizeObject



6
7
8
# File 'lib/ruxster/base.rb', line 6

def parameterize
  URI.escape(properties_hash.collect{|k,v| "#{k}=#{v}"}.join('&'))
end

#properties_hashObject



9
10
11
# File 'lib/ruxster/base.rb', line 9

def properties_hash
  @properties_hash ||= {}
end

#typeObject



19
20
21
# File 'lib/ruxster/base.rb', line 19

def type
  properties_hash["_type"]
end

#type=(value) ⇒ Object



22
23
24
# File 'lib/ruxster/base.rb', line 22

def type=(value)
  properties_hash["_type"] = value
end

#updateObject



49
50
51
# File 'lib/ruxster/base.rb', line 49

def update
  response = Excon.post(Base.connect_string + "/#{self.class.url_directory}/#{self.id}?" + parameterize)
end