Class: RelaxDB::CouchDB
- Inherits:
-
Object
- Object
- RelaxDB::CouchDB
- Defined in:
- lib/relaxdb/server.rb
Instance Attribute Summary collapse
-
#get_count ⇒ Object
Used for test instrumentation only i.e.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#post_count ⇒ Object
Used for test instrumentation only i.e.
-
#put_count ⇒ Object
Used for test instrumentation only i.e.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #db_exists?(name) ⇒ Boolean
- #delete(path = nil) ⇒ Object
-
#delete_db(name) ⇒ Object
URL encode slashes e.g.
- #get(path = nil) ⇒ Object
-
#initialize(config) ⇒ CouchDB
constructor
A new instance of CouchDB.
- #list_dbs ⇒ Object
- #name ⇒ Object
- #name=(name) ⇒ Object
- #post(path = nil, json = nil) ⇒ Object
- #put(path = nil, json = nil) ⇒ Object
- #replicate_db(source, target) ⇒ Object
- #req_count ⇒ Object
- #reset_req_count ⇒ Object
- #unesc(path) ⇒ Object
- #uri ⇒ Object
- #use_db(name) ⇒ Object
- #uuids(count = 1) ⇒ Object
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_count ⇒ Object
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 |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/relaxdb/server.rb', line 9 def logger @logger end |
#post_count ⇒ Object
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_count ⇒ Object
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 |
#server ⇒ Object (readonly)
Returns the value of attribute server.
9 10 11 |
# File 'lib/relaxdb/server.rb', line 9 def server @server end |
Instance Method Details
#db_exists?(name) ⇒ 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
53 54 55 56 |
# File 'lib/relaxdb/server.rb', line 53 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 35 36 37 38 39 |
# File 'lib/relaxdb/server.rb', line 31 def delete_db(name) # Close the http connection as CouchDB will keep a file handle to the db open # if the http connection remains open - this will result in CouchDB throwing # emfile errors after a significant number of databases are deleted. @server.close_connection @logger.info("Deleting database #{name}") @server.delete("/#{name}") end |
#get(path = nil) ⇒ Object
58 59 60 61 62 |
# File 'lib/relaxdb/server.rb', line 58 def get(path=nil) @get_count += 1 @logger.info("GET /#{@db}/#{unesc(path)}") @server.get("/#{@db}/#{path}") end |
#list_dbs ⇒ Object
41 42 43 |
# File 'lib/relaxdb/server.rb', line 41 def list_dbs JSON.parse(@server.get("/_all_dbs").body) end |
#name ⇒ Object
92 93 94 |
# File 'lib/relaxdb/server.rb', line 92 def name @db end |
#name=(name) ⇒ Object
96 97 98 |
# File 'lib/relaxdb/server.rb', line 96 def name=(name) @db = name end |
#post(path = nil, json = nil) ⇒ Object
64 65 66 67 68 |
# File 'lib/relaxdb/server.rb', line 64 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
70 71 72 73 74 |
# File 'lib/relaxdb/server.rb', line 70 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
45 46 47 48 49 50 51 |
# File 'lib/relaxdb/server.rb', line 45 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_count ⇒ Object
100 101 102 |
# File 'lib/relaxdb/server.rb', line 100 def req_count get_count + put_count + post_count end |
#reset_req_count ⇒ Object
104 105 106 |
# File 'lib/relaxdb/server.rb', line 104 def reset_req_count @get_count = @put_count = @post_count = 0 end |
#unesc(path) ⇒ Object
83 84 85 86 |
# File 'lib/relaxdb/server.rb', line 83 def unesc(path) # path path ? ::CGI::unescape(path) : "" end |
#uri ⇒ Object
88 89 90 |
# File 'lib/relaxdb/server.rb', line 88 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 |
#uuids(count = 1) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/relaxdb/server.rb', line 76 def uuids(count=1) @get_count += 1 uri = "/_uuids?count=#{count}" @logger.info "GET #{uri}" @server.get uri end |