Class: Passman::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/passman/manager.rb

Constant Summary collapse

DEFAULT_PATH =
'passman.json'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Manager

Returns a new instance of Manager.



9
10
11
12
# File 'lib/passman/manager.rb', line 9

def initialize(file = nil)
  @file_path = DEFAULT_PATH
  @file_path = file unless file.nil?
end

Instance Method Details

#add(account, username, password, key) ⇒ Object



18
19
20
21
22
23
# File 'lib/passman/manager.rb', line 18

def add(, username, password, key)
  validate_add_arguments(, username, password, key)
  db = database
  db[.to_s] = { username: username, password: Passman::Crypto.encrypt(password, key) }
  store_data(db)
end

#delete(account) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
# File 'lib/passman/manager.rb', line 25

def delete()
  raise ArgumentError, 'No account provided.' unless valid?()
  db = database
  db.delete(.to_s)
  store_data(db)
end

#exist?(account) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


39
40
41
42
# File 'lib/passman/manager.rb', line 39

def exist?()
  raise ArgumentError, 'No account provided.' unless valid?()
  !(!database[.to_s])
end

#generate_password(length = 32) ⇒ Object



14
15
16
# File 'lib/passman/manager.rb', line 14

def generate_password(length = 32)
  Passman::PasswordGenerator.generate(length)
end

#get(account, key) ⇒ Object



32
33
34
35
36
37
# File 'lib/passman/manager.rb', line 32

def get(, key)
  validate_get_arguments(, key)
  data = database[.to_s]
  [.to_s, { 'username' => data['username'],
                   'password' => Passman::Crypto.decrypt(data['password'], key) }]
end

#listObject



44
45
46
# File 'lib/passman/manager.rb', line 44

def list
  database
end