diff options
-rw-r--r-- | lib/tapicero/couch_changes.rb | 15 | ||||
-rw-r--r-- | lib/tapicero/user_database.rb | 5 | ||||
-rw-r--r-- | lib/tapicero_daemon.rb | 6 |
3 files changed, 23 insertions, 3 deletions
diff --git a/lib/tapicero/couch_changes.rb b/lib/tapicero/couch_changes.rb index 376eb11..b45d54a 100644 --- a/lib/tapicero/couch_changes.rb +++ b/lib/tapicero/couch_changes.rb @@ -20,11 +20,20 @@ module Tapicero end end + def deleted(hash = {}, &block) + if block_given? + @deleted = block + else + @deleted && @deleted.call(hash) + end + end + def listen Tapicero.logger.info "listening..." Tapicero.logger.debug "Starting at sequence #{since}" db.changes :feed => :continuous, :since => since, :heartbeat => 1000 do |hash| callbacks(hash) + store_seq(hash["seq"]) end end @@ -36,10 +45,9 @@ module Tapicero def callbacks(hash) #changed callback - return if hash["deleted"] # deleted_callback return unless changes = hash["changes"] + return deleted(hash) if hash["deleted"] created(hash) if changes[0]["rev"].start_with?('1-') - store_seq(hash["seq"]) #updated callback end @@ -49,7 +57,8 @@ module Tapicero unless File.writable?(seq_filename) raise StandardError.new("Can't access sequence file") end - @since = File.read(seq_filename) + @since = File.read(seq_filename).to_i + Tapicero.logger.debug "Found sequence: #{@since}" rescue Errno::ENOENT => e Tapicero.logger.warn "No sequence file found. Starting from scratch" end diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb index 84ed300..ec2694a 100644 --- a/lib/tapicero/user_database.rb +++ b/lib/tapicero/user_database.rb @@ -24,6 +24,11 @@ module Tapicero CouchRest.put security_url, security end + def destroy + db = CouchRest.new(host).database(name) + db.delete! if db + end + protected def secured? diff --git a/lib/tapicero_daemon.rb b/lib/tapicero_daemon.rb index e38a4ad..9223acb 100644 --- a/lib/tapicero_daemon.rb +++ b/lib/tapicero_daemon.rb @@ -21,5 +21,11 @@ module Tapicero db.secure(Config.security) end + users.deleted do |hash| + Tapicero.logger.debug "Deleted user " + hash['id'] + db = UserDatabase.new(Config.couch_host, Config.db_prefix + hash['id']) + db.destroy + end + users.listen end |