Class: CouchRest::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server = 'http://127.0.0.1:5984', opts = {}) ⇒ Server

Returns a new instance of Server.


21
22
23
24
25
26
27
28
29
30
31
32
33
# 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

Instance Attribute Details

#connectionObject (readonly)

Connection object prepared on initialization


16
17
18
# File 'lib/couchrest/server.rb', line 16

def connection
  @connection
end

#connection_optionsObject (readonly)

The connection options we should use to connect with


19
20
21
# File 'lib/couchrest/server.rb', line 19

def connection_options
  @connection_options
end

#uriObject (readonly)

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


5
6
7
# File 'lib/couchrest/server.rb', line 5

def uri
  @uri
end

#uuid_batch_countObject (readonly)

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


9
10
11
# File 'lib/couchrest/server.rb', line 9

def uuid_batch_count
  @uuid_batch_count
end

#uuidsObject (readonly)

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


13
14
15
# File 'lib/couchrest/server.rb', line 13

def uuids
  @uuids
end

Instance Method Details

#create_db(name) ⇒ Object

Create a database


59
60
61
62
# File 'lib/couchrest/server.rb', line 59

def create_db(name)
  connection.put name
  database(name)
end

#database(name) ⇒ Object

Returns a CouchRest::Database for the given name


41
42
43
# File 'lib/couchrest/server.rb', line 41

def database(name)
  CouchRest::Database.new(self, name)
end

#database!(name) ⇒ Object

Creates the database if it doesn't exist


46
47
48
49
50
51
# 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

#databasesObject

Lists all databases on the server


36
37
38
# File 'lib/couchrest/server.rb', line 36

def databases
  connection.get "_all_dbs"
end

#infoObject

GET the welcome message


54
55
56
# File 'lib/couchrest/server.rb', line 54

def info
  connection.get ""
end

#next_uuid(count = @uuid_batch_count) ⇒ Object

Retrive an unused UUID from CouchDB. Server instances manage caching a list of unused UUIDs.


70
71
72
73
74
75
# 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!Object

Restart the CouchDB instance


65
66
67
# File 'lib/couchrest/server.rb', line 65

def restart!
  connection.post "_restart"
end