Class: Humpass::DataStructure

Inherits:
Object
  • Object
show all
Defined in:
lib/humpass/data_structure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDataStructure

Returns a new instance of DataStructure.



14
15
16
17
18
19
# File 'lib/humpass/data_structure.rb', line 14

def initialize
  raise 'Configuration not sat!' if Humpass.configuration.nil?
  new_database_initialize if database.read.empty?
  get_structure_from_database
  Humpass.set_lock_words(self.lock_words)
end

Instance Attribute Details

#structureObject

Returns the value of attribute structure.



12
13
14
# File 'lib/humpass/data_structure.rb', line 12

def structure
  @structure
end

Instance Method Details

#add_password(place, password) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/humpass/data_structure.rb', line 61

def add_password(place, password)
  if self.passwords.select { |password| password.first.eql?(place) }.empty?
    self.passwords << [place, password.encrypt(self.master_password)]
    update_database
  else
    if authorize_replacement?
      remove_place(place)
      add_password(place, password)
    else
      abort('OK!')
    end
  end
end

#authorize_replacement?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
# File 'lib/humpass/data_structure.rb', line 106

def authorize_replacement?
  puts "This place seems to be already registered. Overwrite?[Y/n]"
  overwrite = gets.chomp
  authorize_replacement? unless %w[Y n].include?(overwrite)
  overwrite.eql?('Y') ? true : false
end

#databaseObject



102
103
104
# File 'lib/humpass/data_structure.rb', line 102

def database
  Humpass.database
end

#get_password(place) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/humpass/data_structure.rb', line 75

def get_password(place)
  place = self.passwords.detect { |password| password.first.eql?(place) }
  if place
    place.last.decrypt(self.master_password)
  else
    "Password Not Found!"
  end
end

#get_structure_from_databaseObject



88
89
90
91
# File 'lib/humpass/data_structure.rb', line 88

def get_structure_from_database
  database_read = database.read
  @structure = database_read.empty? ? '' : JSON.parse(database.read)
end

#init_structureObject



26
27
28
29
30
31
32
# File 'lib/humpass/data_structure.rb', line 26

def init_structure
  {
    master_password: Digest::SHA2.new(256).hexdigest(Humpass.configuration.master_password),
    lock_words: Humpass::LockWord.new.words,
    passwords: []
  }
end

#list_passwordsObject



84
85
86
# File 'lib/humpass/data_structure.rb', line 84

def list_passwords
  passwords.map{|password| [password.first, password.last.decrypt(self.master_password)]}
end

#lock_wordsObject



48
49
50
# File 'lib/humpass/data_structure.rb', line 48

def lock_words
  @structure["lock_words"]
end

#lock_words=(lock_words) ⇒ Object



43
44
45
46
# File 'lib/humpass/data_structure.rb', line 43

def lock_words=(lock_words)
  @structure["lock_words"] = lock_words
  update_database
end

#master_passwordObject



39
40
41
# File 'lib/humpass/data_structure.rb', line 39

def master_password
  @structure["master_password"]
end

#master_password=(master_password) ⇒ Object



34
35
36
37
# File 'lib/humpass/data_structure.rb', line 34

def master_password=(master_password)
  @structure["master_password"] = Digest::SHA2.new(256).hexdigest(master_password)
  update_database
end

#new_database_initializeObject



21
22
23
24
# File 'lib/humpass/data_structure.rb', line 21

def new_database_initialize
  @structure = init_structure
  update_database
end

#passwordsObject



57
58
59
# File 'lib/humpass/data_structure.rb', line 57

def passwords
  @structure["passwords"]
end

#passwords=(passwords) ⇒ Object



52
53
54
55
# File 'lib/humpass/data_structure.rb', line 52

def passwords=(passwords)
  @structure["passwords"] = passwords
  update_database
end

#remove_place(place) ⇒ Object



93
94
95
96
# File 'lib/humpass/data_structure.rb', line 93

def remove_place(place)
  self.passwords.delete_if { |password| password.first.eql?(place) }
  update_database
end

#update_databaseObject



98
99
100
# File 'lib/humpass/data_structure.rb', line 98

def update_database
  database.write(@structure.to_json)
end