2 # A CouchDb database object
4 # Create a new database at +uri+ with the name if +dbname+
5 def self.create!(uri, dbname)
6 server = Server.new(uri)
7 response = Response.new(server.put("/#{dbname}", "")).parse
11 # Delete the database at +uri+ with the name if +dbname+
12 def self.delete!(uri, dbname)
13 server = Server.new(uri)
14 response = Response.new(server.delete("/#{dbname}")).parse
18 # All databases at +uri+
19 def self.all_databases(uri)
20 # FIXME: Move to Server ?
21 server = Server.new(uri)
22 resp = server.get("/_all_dbs")
23 response = Response.new(resp).parse
27 # Open a connection to the database at +uri+, where +uri+ is a full uri
28 # like: http://localhost:8888/foo
31 server_uri = "#{uri.scheme}://#{uri.host}:#{uri.port}"
32 new(server_uri, uri.path.tr("/", ""))
35 def initialize(uri, dbname)
38 @server = Server.new(@uri)
42 # The full url of this database, eg http://localhost:8888/foo
44 Utils.join_url(@uri, @dbname).to_s
47 # Name of this database
52 # Send a GET request to the +path+ which is relative to the database path
53 # so calling with with "bar" as the path in the "foo_db" database will call
54 # http://host:port/foo_db/bar.
55 # Returns a Response object
57 Response.new(@server.get("/#{Utils.join_url(@dbname, path)}")).parse
60 # Send a POST request to the +path+ which is relative to the database path
61 # so calling with with "bar" as the path in the "foo_db" database will call
62 # http://host:port/foo_db/bar. The post body is the +payload+
63 # Returns a Response object
64 def post(path, payload)
65 Response.new(@server.post("/#{Utils.join_url(@dbname, path)}", payload)).parse
68 # Send a PUT request to the +path+ which is relative to the database path
69 # so calling with with "bar" as the path in the "foo_db" database will call
70 # http://host:port/foo_db/bar. The put body is the +payload+
71 # Returns a Response object
72 def put(path, payload="")
73 Response.new(@server.put("/#{Utils.join_url(@dbname, path)}", payload)).parse
76 # Send a DELETE request to the +path+ which is relative to the database path
77 # so calling with with "bar" as the path in the "foo_db" database will call
78 # http://host:port/foo_db/bar.
79 # Returns a Response object
81 Response.new(@server.delete("/#{Utils.join_url(@dbname, path)}")).parse
84 # Get a document by id
89 # Get a document by +id+, optionally a specific +revision+ too
90 def document(id, revision=nil)
92 get("#{id}?rev=#{revision}")
98 # Returns an Array of all the documents in this db
100 resp = Response.new(get("_all_docs")).parse
101 resp.to_document.rows
104 # Queries the database with the block (using a temp. view)
110 view = View.new(self, view_name)