Class: Certmeister::Pg::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/certmeister/pg/store.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_string, options = {}) ⇒ Store

Returns a new instance of Store.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/certmeister/pg/store.rb', line 10

def initialize(connection_string, options = {})
  @db = Sequel.connect(connection_string, options)
  @db.create_table? :certificates do
    primary_key :id
    String :cn, unique: true, null: false
    File :pem, null: false
    Time :created_at, null: false
    Time :updated_at, null: false
  end
  @certificates = @db[:certificates]
  @healthy = true
end

Instance Method Details

#fetch(cn) ⇒ Object



30
31
32
33
34
# File 'lib/certmeister/pg/store.rb', line 30

def fetch(cn)
  if cert = @certificates[cn: cn]
    cert[:pem]
  end
end

#health_checkObject



41
42
43
# File 'lib/certmeister/pg/store.rb', line 41

def health_check
  @healthy
end

#remove(cn) ⇒ Object



36
37
38
39
# File 'lib/certmeister/pg/store.rb', line 36

def remove(cn)
  num_removed = @certificates.where('cn = ?', cn).delete
  num_removed == 1
end

#store(cn, pem) ⇒ Object



23
24
25
26
27
28
# File 'lib/certmeister/pg/store.rb', line 23

def store(cn, pem)
  now = Time.now
  if 1 != @certificates.where('cn = ?', cn).update(pem: pem, updated_at: now)
    @certificates.insert(cn: cn, pem: pem, created_at: now, updated_at: now)
  end
end