Class: Protons8::ProtonCouch

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

Defined Under Namespace

Classes: CouchResponse

Instance Method Summary collapse

Constructor Details

#initialize(constrings) ⇒ ProtonCouch

Initialization



9
10
11
12
13
14
15
16
17
18
# File 'lib/protoncouch.rb', line 9

def initialize(constrings)
  @s = { }
  #Construct our settings
  constrings.each do |k,v|
    #Get URI from value
    uri = URI.parse(v)
    #Fill a simple hash without Object (I've no idea why i do this :D)
    @s[k] = { :host => uri.host, :port => uri.port, :path => uri.path, :user => uri.user, :password => uri.password }
  end
end

Instance Method Details

#delete(db, id) ⇒ Object



51
52
53
54
55
# File 'lib/protoncouch.rb', line 51

def delete(db, id)
  response = self.get(db, id)
  rev = response.exists? ? response.rev : ''
  CouchResponse::PutPostDelete.new request( db, Net::HTTP::Delete.new( "#{@s[db][:path]}/#{id}?rev=#{rev}") )
end

#delete_rev(db, id, rev) ⇒ Object



57
58
59
# File 'lib/protoncouch.rb', line 57

def delete_rev(db, id, rev)
  CouchResponse::PutPostDelete.new request( db, Net::HTTP::Delete.new( "#{@s[db][:path]}/#{id}?rev=#{rev}") )
end

#get(db, id, params = {}) ⇒ Object



24
25
26
# File 'lib/protoncouch.rb', line 24

def get(db, id, params = {})
  CouchResponse::Get.new request( db, Net::HTTP::Get.new( "#{@s[db][:path]}/" + id + get_qs(params) ) )
end

#info(db) ⇒ Object



20
21
22
# File 'lib/protoncouch.rb', line 20

def info(db)
  CouchResponse::Get.new request( db, Net::HTTP::Get.new("#{@s[db][:path]}/") )
end

#save(db, document) ⇒ Object



28
29
30
31
32
33
# File 'lib/protoncouch.rb', line 28

def save(db, document)
  req = Net::HTTP::Post.new( "#{@s[db][:path]}/" )
  req["content-type"] = "application/json"
   req.body = document.to_json
  CouchResponse::PutPostDelete.new request( db, req )
end

#save_or_update(db, id, document) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/protoncouch.rb', line 42

def save_or_update(db, id, document)
  response = self.get(db, id)
  document['_rev'] = response.rev if response.exists?      
  req = Net::HTTP::Put.new( "#{@s[db][:path]}/" + id )
  req["content-type"] = "application/json"
   req.body = document.to_json
  CouchResponse::PutPostDelete.new request( db, req )
end

#save_with_id(db, id, document) ⇒ Object



35
36
37
38
39
40
# File 'lib/protoncouch.rb', line 35

def save_with_id(db, id, document)
  req = Net::HTTP::Put.new( "#{@s[db][:path]}/" + id )
  req["content-type"] = "application/json"
   req.body = document.to_json
  CouchResponse::PutPostDelete.new request( db, req )
end

#view(db, view, params = {}) ⇒ Object



61
62
63
# File 'lib/protoncouch.rb', line 61

def view(db, view, params = {})
    CouchResponse::View.new request( db, Net::HTTP::Get.new("#{@s[db][:path]}/_design/#{view.split('/')[0]}/_view/#{view.split('/')[1]}" + get_qs(params) ) )
end