Class: WordBank

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(words, number = 8) ⇒ WordBank

Returns a new instance of WordBank.



38
39
40
41
42
43
44
# File 'lib/password_genie_cl.rb', line 38

def initialize(words, number=8)
  self.word_bank= words
  self.number= number
  self.special_chars= special_chars
  @password_ary = []
  @numbers= (0..9).to_a
end

Instance Attribute Details

#numberObject

Returns the value of attribute number.



7
8
9
# File 'lib/password_genie_cl.rb', line 7

def number
  @number
end

#special_charsObject

Returns the value of attribute special_chars.



7
8
9
# File 'lib/password_genie_cl.rb', line 7

def special_chars
  @special_chars
end

#word_bankObject

Returns the value of attribute word_bank.



7
8
9
# File 'lib/password_genie_cl.rb', line 7

def word_bank
  @word_bank
end

Instance Method Details

#add_capsObject



46
47
48
49
50
51
52
53
# File 'lib/password_genie_cl.rb', line 46

def add_caps
  @caps_bank = []
  @word_bank.each do |letter|
    @caps_bank << letter.upcase
  end
  @word_bank.concat(@caps_bank)
  @caps_bank
end

#add_numbersObject



60
61
62
63
# File 'lib/password_genie_cl.rb', line 60

def add_numbers
  @word_bank.concat(@numbers)
  @word_bank
end

#add_or_replace_info(site, username, password, choice) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/password_genie_cl.rb', line 129

def add_or_replace_info(site, username, password, choice)
  begin
    db = SQLite3::Database.open('genie.db')
    return "please set up database by restarting and choosing [1]" unless File.file?('genie.db')
    puts db.get_first_value "select SQLite_VERSION()"
    db.transaction
    if choice == 3
      db.execute2 "CREATE table if not exists site_info(Id INTEGER PRIMARY KEY, Site TEXT, Username TEXT, Password TEXT)" 
      db.execute2 "INSERT into site_info(Site, Password, Username) values(:site, :password, :username)" , site, password, username
    else
      db.execute2 "UPDATE site_info SET Password = :password WHERE Site = :site AND Username = :username" , password, site, username 
    end
    db.commit
    if db.changes != 1
      alter = "changes"
    else
      alter = "change"
    end
    puts "you made #{db.changes} #{alter}."
  rescue SQLite3::Exception => e
    puts e
    db.rollback
  ensure
    db.close if db
  end
end

#add_special_charsObject



55
56
57
58
# File 'lib/password_genie_cl.rb', line 55

def add_special_chars
  @word_bank.concat(@special_chars)
  @word_bank
end

#createObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/password_genie_cl.rb', line 65

def create
  i = @number
  until i == 0
    if !@words.nil?
      if i == 3 && (!(@word_bank & @caps_bank).empty? && (@password_ary & @caps_bank).empty?)
        @password_ary << @caps_bank[rand(0..@caps_bank.size - 1)]
      end
      if i == 2 && (!(@word_bank & @special_chars).empty? && (@password_ary & @special_chars).empty?)
        @password_ary << @special_chars[rand(0..@special_chars.size - 1)]
      end
      if i == 1 && (!(@word_bank & @numbers).empty? && (@password_ary & @numbers).empty?)
        @password_ary << @numbers[rand(0..@numbers.size - 1)]
      end      
    else
      letter = rand(0..word_bank.size - 1)
      @password_ary << word_bank[letter]
      @word_bank.delete_at(letter)   
      @password = @password_ary.join()

    end
    i -= 1
  end        
  puts @password
  @password
end

#find_info(info) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/password_genie_cl.rb', line 111

def find_info(info)
  begin
    db = SQLite3::Database.open('genie.db')
    puts db.get_first_value "select SQLite_VERSION()"
    return "please create a directory first" unless File.exist?('genie.db')
    print_out = db.execute2 "SELECT * FROM site_info WHERE Site= :info OR Username = :info", info
    return "no match" unless print_out != nil
    print_out.each do |line|
      puts "[%5s] %8s | %s" % [line[1], line[2], line[3]]
    end
    puts
  rescue SQLite3::Exception => e
    puts e
  ensure
    db.close if db
  end
end

#save_info(site, username, password = @password) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/password_genie_cl.rb', line 91

def save_info(site,username,password=@password)
  begin
    db = SQLite3::Database.open('genie.db')
    puts db.get_first_value "select SQLite_VERSION()"
    db.results_as_hash = true
    site_in = site; username_in = username; pw_in = password
    db.transaction
    db.execute "create table if not exists site_info(Id INTEGER PRIMARY KEY, Site TEXT, Username TEXT, Password TEXT)"
    db_in = db.prepare "insert into site_info(Site, Username, Password) values(:site_in, :username_in, :pw_in)"
    db_in.execute site_in, username_in, pw_in
    db.commit
  rescue SQLite3::Exception => e
    puts "something went wrong: #{e}"
    db.rollback
  ensure
    db_in.close if db_in
    db.close if db
  end
end