Class: RelaxDB::CouchDB

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CouchDB

Returns a new instance of CouchDB.



15
16
17
18
19
# File 'lib/relaxdb/server.rb', line 15

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

Instance Attribute Details

#get_countObject

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



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

def get_count
  @get_count
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#post_countObject

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



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

def post_count
  @post_count
end

#put_countObject

Used for test instrumentation only i.e. to assert that an expected number of requests have been issued



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

def put_count
  @put_count
end

Instance Method Details

#db_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/relaxdb/server.rb', line 26

def db_exists?(name)
  @server.get("/#{name}") rescue false
end

#delete(path = nil) ⇒ Object



48
49
50
51
# File 'lib/relaxdb/server.rb', line 48

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

#delete_db(name) ⇒ Object

URL encode slashes e.g. RelaxDB.delete_db “foo%2Fbar”



31
32
33
34
# File 'lib/relaxdb/server.rb', line 31

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

#get(path = nil) ⇒ Object



53
54
55
56
57
# File 'lib/relaxdb/server.rb', line 53

def get(path=nil)
  @get_count += 1
  @logger.info("GET /#{@db}/#{unesc(path)}")
  @server.get("/#{@db}/#{path}")
end

#list_dbsObject



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

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

#nameObject



80
81
82
# File 'lib/relaxdb/server.rb', line 80

def name
  @db
end

#name=(name) ⇒ Object



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

def name=(name)
  @db = name
end

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



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

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

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



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

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

#replicate_db(source, target) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/relaxdb/server.rb', line 40

def replicate_db(source, target)
  @logger.info("Replicating from #{source} to #{target}")
  create_db_if_non_existant target      
  # Manual JSON encoding to allow for dbs containing a '/'
  data = %Q({"source":"#{source}","target":"#{target}"})       
  @server.post("/_replicate", data)
end

#req_countObject



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

def req_count
  get_count + put_count + post_count
end

#reset_req_countObject



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

def reset_req_count
  @get_count = @put_count = @post_count = 0
end

#unesc(path) ⇒ Object



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

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

#uriObject



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

def uri
  "#@server" / @db
end

#use_db(name) ⇒ Object



21
22
23
24
# File 'lib/relaxdb/server.rb', line 21

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