2008-03-19 22:33:41 +01:00
|
|
|
require 'cgi'
|
|
|
|
|
2008-03-18 19:37:10 +01:00
|
|
|
class CouchRest
|
|
|
|
class Database
|
|
|
|
attr_accessor :host, :name
|
|
|
|
def initialize host, name
|
|
|
|
@name = name
|
|
|
|
@host = host
|
2008-03-19 16:57:20 +01:00
|
|
|
@root = "#{host}/#{name}"
|
2008-03-18 19:37:10 +01:00
|
|
|
end
|
|
|
|
|
2008-03-19 16:57:20 +01:00
|
|
|
def documents
|
2008-03-20 06:38:01 +01:00
|
|
|
CouchRest.get "#{@root}/_all_docs"
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
|
|
|
|
2008-03-20 02:10:16 +01:00
|
|
|
def temp_view func
|
|
|
|
JSON.parse(RestClient.post("#{@root}/_temp_view", func, {"Content-Type" => "text/javascript"}))
|
|
|
|
end
|
|
|
|
|
2008-03-19 16:57:20 +01:00
|
|
|
def view name
|
2008-03-20 06:38:01 +01:00
|
|
|
CouchRest.get "#{@root}/_view/#{name}"
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
2008-03-19 18:17:25 +01:00
|
|
|
|
|
|
|
def get id
|
2008-03-19 22:33:41 +01:00
|
|
|
slug = CGI.escape(id)
|
|
|
|
CouchRest.get "#{@root}/#{slug}"
|
2008-03-19 18:17:25 +01:00
|
|
|
end
|
2008-03-19 16:57:20 +01:00
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
# PUT or POST depending on precense of _id attribute
|
2008-03-19 16:57:20 +01:00
|
|
|
def save doc
|
|
|
|
if doc['_id']
|
2008-03-19 22:33:41 +01:00
|
|
|
slug = CGI.escape(doc['_id'])
|
|
|
|
CouchRest.put "#{@root}/#{slug}", doc
|
2008-03-19 16:57:20 +01:00
|
|
|
else
|
|
|
|
CouchRest.post "#{@root}", doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-20 00:38:07 +01:00
|
|
|
def bulk_save docs
|
2008-05-15 02:27:21 +02:00
|
|
|
CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
|
2008-03-20 00:38:07 +01:00
|
|
|
end
|
|
|
|
|
2008-03-19 23:21:27 +01:00
|
|
|
def delete doc
|
|
|
|
slug = CGI.escape(doc['_id'])
|
|
|
|
CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}"
|
|
|
|
end
|
|
|
|
|
2008-03-18 19:37:10 +01:00
|
|
|
def delete!
|
2008-03-19 16:57:20 +01:00
|
|
|
CouchRest.delete @root
|
2008-03-18 19:37:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|