summaryrefslogtreecommitdiff
path: root/lib/tapicero/user_database.rb
blob: ec2694ae049576fb3f72d55d7f40b9caf77ce361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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?
      Tapicero.logger.info "Writing Security to #{security_url}"
      Tapicero.logger.debug security.to_json
      CouchRest.put security_url, security
    end

    def destroy
      db = CouchRest.new(host).database(name)
      db.delete! if db
    end

    protected

    def secured?
      CouchRest.get(security_url).keys.any?
    end

    def security_url
      "#{host}/#{name}/_security"
    end

    attr_reader :host, :name
  end
end