Class: Protons8::Couch

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

Defined Under Namespace

Classes: CouchResponse

Instance Method Summary collapse

Constructor Details

#initialize(constrings) ⇒ Couch

Initialization



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

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



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

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



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

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



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

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



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

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

#save(db, document) ⇒ Object



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

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



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

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



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

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



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

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