2008-03-19 22:33:41 +01:00
|
|
|
require 'cgi'
|
2008-06-07 17:32:51 +02:00
|
|
|
require "base64"
|
2008-03-19 22:33:41 +01:00
|
|
|
|
2008-09-12 06:14:34 +02:00
|
|
|
module CouchRest
|
2008-03-18 19:37:10 +01:00
|
|
|
class Database
|
2008-09-08 00:28:20 +02:00
|
|
|
attr_reader :server, :host, :name, :root
|
|
|
|
|
2008-09-07 22:51:26 +02:00
|
|
|
def initialize server, name
|
2008-03-18 19:37:10 +01:00
|
|
|
@name = name
|
2008-09-07 22:51:26 +02:00
|
|
|
@server = server
|
|
|
|
@host = server.uri
|
2008-03-19 16:57:20 +01:00
|
|
|
@root = "#{host}/#{name}"
|
2008-03-18 19:37:10 +01:00
|
|
|
end
|
|
|
|
|
2008-08-03 21:51:17 +02:00
|
|
|
def to_s
|
|
|
|
@root
|
|
|
|
end
|
|
|
|
|
2008-09-07 21:43:13 +02:00
|
|
|
def info
|
|
|
|
CouchRest.get @root
|
|
|
|
end
|
|
|
|
|
2008-05-25 02:01:28 +02:00
|
|
|
def documents params = nil
|
|
|
|
url = CouchRest.paramify_url "#{@root}/_all_docs", params
|
|
|
|
CouchRest.get url
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
|
|
|
|
2008-05-23 06:57:21 +02:00
|
|
|
def temp_view funcs, params = nil
|
|
|
|
url = CouchRest.paramify_url "#{@root}/_temp_view", params
|
2008-05-24 22:17:44 +02:00
|
|
|
JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
|
2008-03-20 02:10:16 +01:00
|
|
|
end
|
|
|
|
|
2008-05-23 06:41:52 +02:00
|
|
|
def view name, params = nil
|
2008-05-23 06:57:21 +02:00
|
|
|
url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
|
2008-05-23 06:41:52 +02:00
|
|
|
CouchRest.get url
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
2008-06-12 18:09:56 +02:00
|
|
|
|
|
|
|
# experimental
|
|
|
|
def search params = nil
|
|
|
|
url = CouchRest.paramify_url "#{@root}/_search", params
|
|
|
|
CouchRest.get url
|
|
|
|
end
|
2008-07-05 01:56:09 +02:00
|
|
|
# experimental
|
|
|
|
def action action, params = nil
|
|
|
|
url = CouchRest.paramify_url "#{@root}/_action/#{action}", params
|
|
|
|
CouchRest.get url
|
|
|
|
end
|
|
|
|
|
2008-03-19 18:17:25 +01:00
|
|
|
def get id
|
2008-06-07 17:32:51 +02:00
|
|
|
slug = CGI.escape(id)
|
2008-03-19 22:33:41 +01:00
|
|
|
CouchRest.get "#{@root}/#{slug}"
|
2008-03-19 18:17:25 +01:00
|
|
|
end
|
2008-03-19 16:57:20 +01:00
|
|
|
|
2008-06-07 17:32:51 +02:00
|
|
|
def fetch_attachment doc, name
|
|
|
|
doc = CGI.escape(doc)
|
|
|
|
name = CGI.escape(name)
|
|
|
|
RestClient.get "#{@root}/#{doc}/#{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT or POST depending on presence of _id attribute
|
2008-03-19 16:57:20 +01:00
|
|
|
def save doc
|
2008-06-07 17:32:51 +02:00
|
|
|
if doc['_attachments']
|
|
|
|
doc['_attachments'] = encode_attachments(doc['_attachments'])
|
|
|
|
end
|
2008-03-19 16:57:20 +01:00
|
|
|
if doc['_id']
|
2008-03-19 22:33:41 +01:00
|
|
|
slug = CGI.escape(doc['_id'])
|
2008-09-14 00:59:24 +02:00
|
|
|
CouchRest.put "#{@root}/#{slug}", doc
|
2008-03-19 16:57:20 +01:00
|
|
|
else
|
2008-09-14 00:59:24 +02:00
|
|
|
begin
|
|
|
|
slug = doc['_id'] = @server.next_uuid
|
|
|
|
CouchRest.put "#{@root}/#{slug}", doc
|
|
|
|
rescue #old version of couchdb
|
|
|
|
CouchRest.post @root, doc
|
|
|
|
end
|
2008-03-19 16:57:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-20 00:38:07 +01:00
|
|
|
def bulk_save docs
|
2008-09-07 22:51:26 +02:00
|
|
|
ids, noids = docs.partition{|d|d['_id']}
|
|
|
|
uuid_count = [noids.length, @server.uuid_batch_count].max
|
|
|
|
noids.each do |doc|
|
2008-09-14 00:59:24 +02:00
|
|
|
nextid = @server.next_uuid(uuid_count) rescue nil
|
|
|
|
doc['_id'] = nextid if nextid
|
2008-09-07 22:51:26 +02:00
|
|
|
end
|
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
|
2008-09-07 22:51:26 +02:00
|
|
|
|
2008-06-07 17:32:51 +02:00
|
|
|
private
|
2008-09-07 22:51:26 +02:00
|
|
|
|
2008-06-07 17:32:51 +02:00
|
|
|
def encode_attachments attachments
|
|
|
|
attachments.each do |k,v|
|
2008-06-12 17:40:52 +02:00
|
|
|
next if v['stub']
|
2008-06-07 18:05:29 +02:00
|
|
|
v['data'] = base64(v['data'])
|
2008-06-07 17:32:51 +02:00
|
|
|
end
|
2008-06-07 18:05:29 +02:00
|
|
|
attachments
|
2008-06-07 17:32:51 +02:00
|
|
|
end
|
2008-09-07 22:51:26 +02:00
|
|
|
|
2008-06-07 17:32:51 +02:00
|
|
|
def base64 data
|
|
|
|
Base64.encode64(data).gsub(/\s/,'')
|
|
|
|
end
|
2008-03-18 19:37:10 +01:00
|
|
|
end
|
2008-06-20 23:26:26 +02:00
|
|
|
end
|