2008-09-12 06:14:34 +02:00
|
|
|
module CouchRest
|
|
|
|
class Server
|
|
|
|
attr_accessor :uri, :uuid_batch_count
|
2008-12-14 12:05:02 +01:00
|
|
|
def initialize server = 'http://127.0.0.1:5984', uuid_batch_count = 1000
|
2008-09-12 06:14:34 +02:00
|
|
|
@uri = server
|
|
|
|
@uuid_batch_count = uuid_batch_count
|
|
|
|
end
|
|
|
|
|
2008-09-30 08:39:57 +02:00
|
|
|
# List all databases on the server
|
2008-09-12 06:14:34 +02:00
|
|
|
def databases
|
|
|
|
CouchRest.get "#{@uri}/_all_dbs"
|
|
|
|
end
|
|
|
|
|
2008-09-30 08:39:57 +02:00
|
|
|
# Returns a CouchRest::Database for the given name
|
2008-09-12 06:14:34 +02:00
|
|
|
def database name
|
|
|
|
CouchRest::Database.new(self, name)
|
|
|
|
end
|
|
|
|
|
2008-09-30 08:39:57 +02:00
|
|
|
# Creates the database if it doesn't exist
|
2008-09-12 06:14:34 +02:00
|
|
|
def database! name
|
|
|
|
create_db(name) rescue nil
|
|
|
|
database name
|
|
|
|
end
|
|
|
|
|
2008-09-30 08:39:57 +02:00
|
|
|
# GET the welcome message
|
2008-09-12 06:14:34 +02:00
|
|
|
def info
|
|
|
|
CouchRest.get "#{@uri}/"
|
|
|
|
end
|
|
|
|
|
2008-09-30 08:39:57 +02:00
|
|
|
# Create a database
|
2008-09-12 06:14:34 +02:00
|
|
|
def create_db name
|
|
|
|
CouchRest.put "#{@uri}/#{name}"
|
|
|
|
database name
|
|
|
|
end
|
|
|
|
|
2008-09-30 08:39:57 +02:00
|
|
|
# Restart the CouchDB instance
|
2008-09-12 06:14:34 +02:00
|
|
|
def restart!
|
|
|
|
CouchRest.post "#{@uri}/_restart"
|
|
|
|
end
|
|
|
|
|
2008-09-30 08:39:57 +02:00
|
|
|
# Retrive an unused UUID from CouchDB. Server instances manage caching a list of unused UUIDs.
|
2008-09-12 06:14:34 +02:00
|
|
|
def next_uuid count = @uuid_batch_count
|
|
|
|
@uuids ||= []
|
|
|
|
if @uuids.empty?
|
|
|
|
@uuids = CouchRest.post("#{@uri}/_uuids?count=#{count}")["uuids"]
|
|
|
|
end
|
|
|
|
@uuids.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|