Connection object prepared on initialization
The connection options we should use to connect with
URI object of the link to the server we’re using.
Number of UUIDs to fetch from the server when preparing to save new documents. Set to 1000 by default.
Accessor for the current internal array of UUIDs ready to be used when saving new documents. See also next_uuid.
# File lib/couchrest/server.rb, line 21 def initialize(server = 'http://127.0.0.1:5984', opts = {}) @uri = prepare_uri(server).freeze if opts.is_a?(Integer) # Backwards compatibility @uuid_batch_count = opts @connection_options = {} else @uuid_batch_count = opts.delete(:uuid_batch_count) @connection_options = opts end @uuid_batch_count ||= 1000 @connection = Connection.new(uri, connection_options) end
Create a database
# File lib/couchrest/server.rb, line 59 def create_db(name) connection.put name database(name) end
Returns a CouchRest::Database for the given name
# File lib/couchrest/server.rb, line 41 def database(name) CouchRest::Database.new(self, name) end
Creates the database if it doesn’t exist
# File lib/couchrest/server.rb, line 46 def database!(name) connection.head name # Check if the URL is valid database(name) rescue CouchRest::NotFound # Thrown if the HTTP HEAD fails create_db(name) end
Lists all databases on the server
# File lib/couchrest/server.rb, line 36 def databases connection.get "_all_dbs" end
GET the welcome message
# File lib/couchrest/server.rb, line 54 def info connection.get "" end
Retrive an unused UUID from CouchDB. Server instances manage caching a list of unused UUIDs.
# File lib/couchrest/server.rb, line 70 def next_uuid(count = @uuid_batch_count) if uuids.nil? || uuids.empty? @uuids = connection.get("_uuids?count=#{count}")["uuids"] end uuids.pop end
Restart the CouchDB instance
# File lib/couchrest/server.rb, line 65 def restart! connection.post "_restart" end
# File lib/couchrest/server.rb, line 79 def prepare_uri(url) uri = URI(url) uri.path = "" uri.query = nil uri.fragment = nil uri end