Class: PasswordSafe::Keyring

Inherits:
Object
  • Object
show all
Defined in:
lib/passwordsafe/keyring.rb

Defined Under Namespace

Classes: KeyExistsException, KeyMissingException

Instance Method Summary collapse

Constructor Details

#initialize(safe = nil) ⇒ Keyring

Returns a new instance of Keyring.



9
10
11
12
# File 'lib/passwordsafe/keyring.rb', line 9

def initialize safe = nil
  @safe = safe
  @ring = load_from_safe
end

Instance Method Details

#add(name, password) ⇒ Object

Raises:



22
23
24
25
26
# File 'lib/passwordsafe/keyring.rb', line 22

def add name, password
  raise KeyExistsException, "Key already exists in keyring, if you'd like to add it remove the existing key", caller if @ring.has_key?(name)
  @ring.store(name, password)
  @safe.write_safe @ring
end

#change(name, password) ⇒ Object



28
29
30
31
32
# File 'lib/passwordsafe/keyring.rb', line 28

def change name, password
  raise KeyMissingException, "#{name} does not exist in this safe.", caller unless @ring.has_key?(name)
  @ring[name] = password
  @safe.write_safe @ring
end

#generate(name, length = 8) ⇒ Object

Raises:



34
35
36
37
38
39
40
41
# File 'lib/passwordsafe/keyring.rb', line 34

def generate name, length = 8
  raise KeyExistsException, "Key already exists in keyring, if you'd like to add it remove the existing key", caller if @ring.has_key?(name)
  password = generate_password(length)
  @ring.store(name, password)
  @safe.write_safe @ring
  Clipboard.copy(password)
  password
end

#get(name) ⇒ Object



43
44
45
46
47
48
# File 'lib/passwordsafe/keyring.rb', line 43

def get name
  raise KeyMissingException, "#{name} does not exist in this safe.", caller unless @ring.has_key?(name)
  password = @ring[name]
  Clipboard.copy(password)
  password
end

#has_a_safe?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/passwordsafe/keyring.rb', line 14

def has_a_safe?
  @safe.is_a? PasswordSafe::Safe
end

#lengthObject



18
19
20
# File 'lib/passwordsafe/keyring.rb', line 18

def length
  @ring.length
end

#listObject



56
57
58
# File 'lib/passwordsafe/keyring.rb', line 56

def list
  @ring.keys
end

#remove(name) ⇒ Object



50
51
52
53
54
# File 'lib/passwordsafe/keyring.rb', line 50

def remove name
  raise KeyMissingException, "#{name} does not exist in this safe.", caller unless @ring.has_key?(name)
  @ring.delete(name)
  @safe.write_safe @ring
end