Class: CouchCrumbs::Server

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

Overview

Represents an instance of a live running CouchDB server

Constant Summary collapse

DEFAULT_URI =
"http://couchdb.local:5984".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Server

Create a new instance of Server



13
14
15
16
17
# File 'lib/couch_crumbs/server.rb', line 13

def initialize(opts = {})
  self.uri = opts[:uri] || DEFAULT_URI

  self.status = JSON.parse(RestClient.get(self.uri))
end

Instance Attribute Details

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#databasesObject

TODO:
  • add a :refresh argument with a 10 second cache of the DBs

Return an array of databases



22
23
24
25
26
# File 'lib/couch_crumbs/server.rb', line 22

def databases
  JSON.parse(RestClient.get(File.join(self.uri, "_all_dbs"))).collect do |database_name|
    Database.new(:name => database_name)
  end
end

#uuids(count = 1) ⇒ Object

Return a new random UUID for use in documents



30
31
32
33
34
35
36
37
38
# File 'lib/couch_crumbs/server.rb', line 30

def uuids(count = 1)
  uuids = JSON.parse(RestClient.get(File.join(self.uri, "_uuids?count=#{ count }")))["uuids"]
  
  if count > 1
    uuids
  else
    uuids.first
  end
end