Class: RelaxDB::CouchDB

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CouchDB

Returns a new instance of CouchDB.



56
57
58
59
# File 'lib/relaxdb/server.rb', line 56

def initialize(config)
  @server = RelaxDB::Server.new(config[:host], config[:port])
  @logger = config[:logger] ? config[:logger] : Logger.new(Tempfile.new('couchdb.log'))
end

Instance Method Details

#delete(path = nil) ⇒ Object



82
83
84
85
# File 'lib/relaxdb/server.rb', line 82

def delete(path=nil)
  @logger.info("DELETE /#{@db}/#{unesc(path)}")
  @server.delete("/#{@db}/#{path}")
end

#delete_db(name) ⇒ Object



66
67
68
69
# File 'lib/relaxdb/server.rb', line 66

def delete_db(name)
  @logger.info("Deleting database #{name}")
  @server.delete("/#{name}")
end

#get(path = nil, *ignored) ⇒ Object

*ignored allows methods to invoke get or post indifferently



88
89
90
91
# File 'lib/relaxdb/server.rb', line 88

def get(path=nil, *ignored)
  @logger.info("GET /#{@db}/#{unesc(path)}")
  @server.get("/#{@db}/#{path}")
end

#list_dbsObject



71
72
73
# File 'lib/relaxdb/server.rb', line 71

def list_dbs
  JSON.parse(@server.get("/_all_dbs").body)
end

#loggerObject



116
117
118
# File 'lib/relaxdb/server.rb', line 116

def logger
  @logger
end

#nameObject



112
113
114
# File 'lib/relaxdb/server.rb', line 112

def name
  @db
end

#post(path = nil, json = nil) ⇒ Object



93
94
95
96
# File 'lib/relaxdb/server.rb', line 93

def post(path=nil, json=nil)
  @logger.info("POST /#{@db}/#{unesc(path)} #{json}")
  @server.post("/#{@db}/#{path}", json)
end

#put(path = nil, json = nil) ⇒ Object



98
99
100
101
# File 'lib/relaxdb/server.rb', line 98

def put(path=nil, json=nil)
  @logger.info("PUT /#{@db}/#{unesc(path)} #{json}")
  @server.put("/#{@db}/#{path}", json)
end

#replicate_db(source, target) ⇒ Object



75
76
77
78
79
80
# File 'lib/relaxdb/server.rb', line 75

def replicate_db(source, target)
  @logger.info("Replicating from #{source} to #{target}")
  create_db_if_non_existant target      
  data = { "source" => source, "target" => target}
  @server.post("/_replicate", data.to_json)
end

#unesc(path) ⇒ Object



103
104
105
106
# File 'lib/relaxdb/server.rb', line 103

def unesc(path)
  # path
  path ? ::CGI::unescape(path) : ""
end

#uriObject



108
109
110
# File 'lib/relaxdb/server.rb', line 108

def uri
  "#@server" / @db
end

#use_db(name) ⇒ Object



61
62
63
64
# File 'lib/relaxdb/server.rb', line 61

def use_db(name)
  create_db_if_non_existant(name)
  @db = name
end