class CouchRest::Server

Attributes

connection[R]

Connection object prepared on initialization

connection_options[R]

The connection options we should use to connect with

uri[R]

URI object of the link to the server we're using.

uuid_batch_count[R]

Number of UUIDs to fetch from the server when preparing to save new documents. Set to 1000 by default.

uuids[R]

Accessor for the current internal array of UUIDs ready to be used when saving new documents. See also next_uuid.

Public Class Methods

new(server = 'http://127.0.0.1:5984', opts = {}) click to toggle source
# 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

Public Instance Methods

create_db(name) click to toggle source

Create a database

# File lib/couchrest/server.rb, line 59
def create_db(name)
  connection.put name
  database(name)
end
database(name) click to toggle source

Returns a CouchRest::Database for the given name

# File lib/couchrest/server.rb, line 41
def database(name)
  CouchRest::Database.new(self, name)
end
database!(name) click to toggle source

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
databases() click to toggle source

Lists all databases on the server

# File lib/couchrest/server.rb, line 36
def databases
  connection.get "_all_dbs"
end
info() click to toggle source

GET the welcome message

# File lib/couchrest/server.rb, line 54
def info
  connection.get ""
end
next_uuid(count = @uuid_batch_count) click to toggle source

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!() click to toggle source

Restart the CouchDB instance

# File lib/couchrest/server.rb, line 65
def restart!
  connection.post "_restart"
end

Protected Instance Methods

prepare_uri(url) click to toggle source
# File lib/couchrest/server.rb, line 79
def prepare_uri(url)
  uri = URI(url)
  uri.path     = ""
  uri.query    = nil
  uri.fragment = nil
  uri
end