Class: Ruxster::Base
- Inherits:
-
Object
- Object
- Ruxster::Base
- Defined in:
- lib/ruxster/base.rb
Class Method Summary collapse
- .connect_string ⇒ Object
- .create_index(automatic = true) ⇒ Object
- .find(key, value) ⇒ Object
- .get(id) ⇒ Object
- .indices ⇒ Object
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
-
#initialize(hash = {}) ⇒ Base
constructor
A new instance of Base.
- #parameterize ⇒ Object
- #properties_hash ⇒ Object
- #type ⇒ Object
- #type=(value) ⇒ Object
- #update ⇒ Object
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_string ⇒ Object
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 |
.indices ⇒ Object
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
#create ⇒ Object
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 |
#destroy ⇒ Object
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 |
#id ⇒ Object
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 |
#parameterize ⇒ Object
6 7 8 |
# File 'lib/ruxster/base.rb', line 6 def parameterize URI.escape(properties_hash.collect{|k,v| "#{k}=#{v}"}.join('&')) end |
#properties_hash ⇒ Object
9 10 11 |
# File 'lib/ruxster/base.rb', line 9 def properties_hash @properties_hash ||= {} end |
#type ⇒ Object
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 |
#update ⇒ Object
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 |