Class: Slosilo::Adapters::FileAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/slosilo/adapters/file_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ FileAdapter

Returns a new instance of FileAdapter.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/slosilo/adapters/file_adapter.rb', line 8

def initialize(dir)
  @dir = dir
  @keys = {}
  @fingerprints = {}
  Dir[File.join(@dir, "*.key")].each do |f|
    key = Slosilo::EncryptedAttributes.decrypt File.read(f)
    id = File.basename(f, '.key')
    key = @keys[id] = Slosilo::Key.new(key)
    @fingerprints[key.fingerprint] = id
  end
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



6
7
8
# File 'lib/slosilo/adapters/file_adapter.rb', line 6

def dir
  @dir
end

Instance Method Details

#each(&block) ⇒ Object



37
38
39
# File 'lib/slosilo/adapters/file_adapter.rb', line 37

def each(&block)
  @keys.each(&block)
end

#get_by_fingerprint(fp) ⇒ Object



32
33
34
35
# File 'lib/slosilo/adapters/file_adapter.rb', line 32

def get_by_fingerprint fp
  id = @fingerprints[fp]
  [@keys[id], id]
end

#get_key(id) ⇒ Object



28
29
30
# File 'lib/slosilo/adapters/file_adapter.rb', line 28

def get_key id
  @keys[id]
end

#put_key(id, value) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/slosilo/adapters/file_adapter.rb', line 20

def put_key id, value
  raise "id should not contain a period" if id.index('.')
  fname = File.join(dir, "#{id}.key")
  File.write(fname, Slosilo::EncryptedAttributes.encrypt(value.to_der))
  File.chmod(0400, fname)
  @keys[id] = value
end