diff options
author | Azul <azul@riseup.net> | 2013-09-12 09:57:45 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2013-09-12 09:57:45 +0200 |
commit | 37f87300c9804bdad66d730b676715e242b9ca42 (patch) | |
tree | 7fa746846c2a127812d28d58cc5f0abd04cf180d /lib/tapicero/user_database.rb | |
parent | 929fcdea37f92f6b175482c6e0d1b8ff3229854f (diff) |
use CouchRest::Database#changes, store and retrieve seq
This commit bundles a few different changes:
* we now use the CouchRest::Database#changes stream instead of our own
* we store and read the last sequence number so on a restart we can start where we left
* we now have a UserDatabase class rather than the CouchDatabaseCreator. It also knows about it's name and we create an instance of it per database we want to create. It's also more flexible when the databases already exist.
Diffstat (limited to 'lib/tapicero/user_database.rb')
-rw-r--r-- | lib/tapicero/user_database.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb new file mode 100644 index 0000000..fcdd272 --- /dev/null +++ b/lib/tapicero/user_database.rb @@ -0,0 +1,39 @@ +require 'couchrest' +require 'json' + +module Tapicero + class UserDatabase + + def initialize(host, name) + @host = host + @name = name + end + + def create + begin + CouchRest.new(host).create_db(name) + rescue RestClient::PreconditionFailed # database already existed + end + end + + def secure(security) + # let's not overwrite if we have a security doc already + return if secured? + puts security.to_json + puts "-> #{security_url}" + CouchRest.put security_url, security + end + + protected + + def secured? + CouchRest.get(security_url).keys.any? + end + + def security_url + "#{host}/#{name}/_security" + end + + attr_reader :host, :name + end +end |