2008-03-19 14:33:41 -07:00
|
|
|
require 'cgi'
|
|
|
|
|
2008-03-18 11:37:10 -07:00
|
|
|
class CouchRest
|
|
|
|
class Database
|
|
|
|
attr_accessor :host, :name
|
|
|
|
def initialize host, name
|
|
|
|
@name = name
|
|
|
|
@host = host
|
2008-03-19 08:57:20 -07:00
|
|
|
@root = "#{host}/#{name}"
|
2008-03-18 11:37:10 -07:00
|
|
|
end
|
|
|
|
|
2008-05-24 17:01:28 -07:00
|
|
|
def documents params = nil
|
|
|
|
url = CouchRest.paramify_url "#{@root}/_all_docs", params
|
|
|
|
CouchRest.get url
|
2008-03-19 08:57:20 -07:00
|
|
|
end
|
|
|
|
|
2008-05-22 21:57:21 -07:00
|
|
|
def temp_view funcs, params = nil
|
|
|
|
url = CouchRest.paramify_url "#{@root}/_temp_view", params
|
2008-05-24 13:17:44 -07:00
|
|
|
JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
|
2008-03-19 18:10:16 -07:00
|
|
|
end
|
|
|
|
|
2008-05-22 21:41:52 -07:00
|
|
|
def view name, params = nil
|
2008-05-22 21:57:21 -07:00
|
|
|
url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
|
2008-05-22 21:41:52 -07:00
|
|
|
CouchRest.get url
|
2008-03-19 08:57:20 -07:00
|
|
|
end
|
2008-03-19 10:17:25 -07:00
|
|
|
|
|
|
|
def get id
|
2008-03-19 14:33:41 -07:00
|
|
|
slug = CGI.escape(id)
|
|
|
|
CouchRest.get "#{@root}/#{slug}"
|
2008-03-19 10:17:25 -07:00
|
|
|
end
|
2008-03-19 08:57:20 -07:00
|
|
|
|
2008-03-19 10:17:25 -07:00
|
|
|
# PUT or POST depending on precense of _id attribute
|
2008-03-19 08:57:20 -07:00
|
|
|
def save doc
|
|
|
|
if doc['_id']
|
2008-03-19 14:33:41 -07:00
|
|
|
slug = CGI.escape(doc['_id'])
|
|
|
|
CouchRest.put "#{@root}/#{slug}", doc
|
2008-03-19 08:57:20 -07:00
|
|
|
else
|
|
|
|
CouchRest.post "#{@root}", doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-19 16:38:07 -07:00
|
|
|
def bulk_save docs
|
2008-05-14 17:27:21 -07:00
|
|
|
CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
|
2008-03-19 16:38:07 -07:00
|
|
|
end
|
|
|
|
|
2008-03-19 15:21:27 -07:00
|
|
|
def delete doc
|
|
|
|
slug = CGI.escape(doc['_id'])
|
|
|
|
CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}"
|
|
|
|
end
|
|
|
|
|
2008-03-18 11:37:10 -07:00
|
|
|
def delete!
|
2008-03-19 08:57:20 -07:00
|
|
|
CouchRest.delete @root
|
2008-03-18 11:37:10 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|