Skip to content

Instantly share code, notes, and snippets.

@PlanetRoast
Last active November 8, 2017 10:53
Show Gist options
  • Save PlanetRoast/3ce2b584d614391f28ef02cfc3c023b6 to your computer and use it in GitHub Desktop.
Save PlanetRoast/3ce2b584d614391f28ef02cfc3c023b6 to your computer and use it in GitHub Desktop.
# Password Generator
# Class name: Originally I had named my class 'PasswordGenerator' and found this was causing a 'not a class' error. Changing the name of the class to 'GeneratePassword' fixed the problem.
# Boolean checking: When writing the validations for the arguements I wanted to check if that data passed in was a boolean. I was surprised to discover that you can't use: foo.is_a?(Boolean) in the same way that foo.is_a?(Integer) works. Found a neat way around it: !!foo == foo which converts the value to a boolean then checks it against itself.
class GeneratePassword
def initialize(length, uppercase, lowercase, number, special)
@length = length
@uppercase = uppercase
@lowercase = lowercase
@number = number
@special = special
validate_args
create_pool
@password = generate
end
def password
@password
end
def strength
# Future Feature (work in progress)
# => 81% (a percentage score showing how secure the password is)
end
def has_uppercase?
if uppercase_letters.any? {|letter| @password.include?(letter)}
return true
else
return false
end
end
def has_lowercase?
if lowercase_letters.any? {|letter| @password.include?(letter)}
return true
else
return false
end
end
def has_number?
if numbers.any? {|number| @password.include?(number.to_s)}
return true
else
return false
end
end
def has_special?
if special_characters.any? {|character| @password.include?(character)}
return true
else
return false
end
end
private
def generate
# => A string of x many characters taken from the pool
@length.times.map {@pool.sample}.join
end
def create_pool
# => An array containing all potential characters/numbers for the password
pool = []
pool += uppercase_letters if @uppercase
pool += lowercase_letters if @lowercase
pool += numbers if @number
pool += special_characters if @special
@pool = pool
end
def uppercase_letters
# => ["A", "B", "C", "D" etc ]
('A'..'Z').to_a
end
def lowercase_letters
# => ["a", "b", "c", "d" etc ]
('a'..'z').to_a
end
def numbers
# => ["1", "2", "3", "4" etc ]
(0..9).to_a
end
def special_characters
# => ["!", "$", "%", "&" etc ]
%w(! $ % & * @ ^)
end
def validate_args
validate_length_arg
validate_uppercase_arg
validate_lowercase_arg
validate_number_arg
validate_special_arg
end
def validate_length_arg
unless @length.integer? && @length.between?(1,512)
message = "Password Generator error: "
message += "Your 1st argument should be an Integer between 1 and 512. "
message += "It sets the length of your password."
raise message
end
end
def validate_uppercase_arg
unless !!@uppercase == @uppercase
message = "Password Generator error: "
message += "Your 2nd argument should be a boolean. "
message += "Set it to true to include uppercase letters from A-Z in your password."
raise message
end
end
def validate_lowercase_arg
unless !!@lowercase == @lowercase
message = "Password Generator error: "
message += "Your 3rd argument should be a boolean. "
message += "Set it to true to include lowercase letters from a-z in your password."
raise message
end
end
def validate_number_arg
unless !!@number == @number
message = "Password Generator error: "
message += "Your 4th argument should be a boolean. "
message += "Set it to true to include numbers from 0-9 in your password."
raise message
end
end
def validate_special_arg
unless !!@special == @special
message = "Password Generator error: "
message += "Your 5th argument should be a boolean. "
message += "Set it to true to include these special characters in your password: #{special_characters.join}."
raise message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment