summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@riseup.net>2014-02-04 14:00:58 +0100
committerAzul <azul@riseup.net>2014-02-04 14:00:58 +0100
commita8194a21cb9f452adb77d24b60434f921c60d496 (patch)
tree775288199dbc9f85e3bd4525ad8763222433b61b
parentc3cb71ca5e6d32960e4493d85799f3706ea91fe8 (diff)
refactor: init user_database with a couchrest db
-rw-r--r--lib/tapicero.rb2
-rw-r--r--lib/tapicero/user_database.rb42
-rw-r--r--lib/tapicero_daemon.rb4
3 files changed, 27 insertions, 21 deletions
diff --git a/lib/tapicero.rb b/lib/tapicero.rb
index 390257d..fd53d64 100644
--- a/lib/tapicero.rb
+++ b/lib/tapicero.rb
@@ -30,6 +30,6 @@ module Tapicero
require 'tapicero/user_database'
def self.user_database(id)
- UserDatabase.new(config.couch_host, config.options[:db_prefix] + id)
+ UserDatabase.new(id)
end
end
diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb
index 3cfdd70..d35af20 100644
--- a/lib/tapicero/user_database.rb
+++ b/lib/tapicero/user_database.rb
@@ -4,16 +4,15 @@ require 'json'
module Tapicero
class UserDatabase
- def initialize(host, name)
- @host = host
- @name = name
+ def initialize(user_id)
+ @db = couch.database(config.options[:db_prefix] + user_id)
end
- def prepare(config)
+ def prepare
create
- secure(config.options[:security])
+ secure
add_design_docs
- Tapicero.logger.info "Prepared storage " + name
+ Tapicero.logger.info "Prepared storage " + db.name
end
def create
@@ -22,7 +21,8 @@ module Tapicero
end
end
- def secure(security)
+ def secure(security = nil)
+ security ||= config.options[:security]
# let's not overwrite if we have a security doc already
return if secured? && !Tapicero::FLAGS.include?('--overwrite-security')
retry_request_once "Writing security to" do
@@ -42,8 +42,7 @@ module Tapicero
end
def upload_design_doc(file)
- url = design_url(file.basename('.json'))
- CouchRest.put url, JSON.parse(file.read)
+ CouchRest.put design_url(file.basename('.json')), JSON.parse(file.read)
rescue RestClient::Conflict
end
@@ -57,25 +56,23 @@ module Tapicero
protected
def create_db
- CouchRest.new(host).create_db(name)
- rescue RestClient::PreconditionFailed # database already existed
+ db.create! || db.info
end
def delete_db
- db = CouchRest.new(host).database(name)
db.delete! if db
rescue RestClient::ResourceNotFound # no database found
end
def retry_request_once(action)
second_try ||= false
- Tapicero.logger.debug "#{action} #{name}"
+ Tapicero.logger.debug "#{action} #{db.name}"
yield
rescue RestClient::Exception => exc
if second_try
- log_error "#{action} #{name} failed twice due to:", exc
+ log_error "#{action} #{db.name} failed twice due to:", exc
else
- log_error "#{action} #{name} failed due to:", exc
+ log_error "#{action} #{db.name} failed due to:", exc
sleep 5
second_try = true
retry
@@ -95,13 +92,22 @@ module Tapicero
end
def security_url
- "#{host}/#{name}/_security"
+ db.root + "/_security"
end
def design_url(doc_name)
- "#{host}/#{name}/_design/#{doc_name}"
+ db.root + "/_design/#{doc_name}"
+ end
+
+ attr_reader :db
+
+ def couch
+ @couch ||= CouchRest.new(config.couch_host)
+ end
+
+ def config
+ Tapicero.config
end
- attr_reader :host, :name
end
end
diff --git a/lib/tapicero_daemon.rb b/lib/tapicero_daemon.rb
index 23248e3..b9f22dc 100644
--- a/lib/tapicero_daemon.rb
+++ b/lib/tapicero_daemon.rb
@@ -12,7 +12,7 @@ module Tapicero
users.created do |hash|
logger.debug "Created user " + hash['id']
- user_database(hash['id']).prepare(config)
+ user_database(hash['id']).prepare
end
# Sometimes changes log starts with rev 2. So the
@@ -21,7 +21,7 @@ module Tapicero
# couchrest changes takes this into account.
users.updated do |hash|
logger.debug "Updated user " + hash['id']
- user_database(hash['id']).prepare(config)
+ user_database(hash['id']).prepare
end
users.deleted do |hash|