Class: Passweird::Leet::Translate
- Inherits:
-
Object
- Object
- Passweird::Leet::Translate
- Defined in:
- lib/passweird/leet/translate.rb
Overview
Constant Summary collapse
- CIPHER =
{ "4" => "A", "8" => "B", "{" => "C", "(" => "C", "3" => "E", "6" => "G", "0" => "O", "5" => "S", "7" => "T", "2" => "Z" }.freeze
- MIXED_CHAR_REGEX =
Pattern to check if string is either:
-
Letters + Numbers
-
Letters + Special Characters
-
Letters + Numbers + Special Characters
-
/^(?=.*[a-zA-Z])(?=.*[\d\W]).+$/.freeze
Instance Attribute Summary collapse
-
#given_string ⇒ Object
readonly
Returns the value of attribute given_string.
Class Method Summary collapse
- .leet(given_string) ⇒ Object
- .leet?(given_string) ⇒ Boolean
- .leet_all(array_of_strings) ⇒ Object
- .unleet(given_string) ⇒ Object
- .unleet_all(array_of_strings) ⇒ Object
Instance Method Summary collapse
- #cipher ⇒ Object
- #cipher_regex ⇒ Object
-
#initialize(given_string) ⇒ Translate
constructor
A new instance of Translate.
- #inverted_cipher ⇒ Object
- #inverted_cipher_regex ⇒ Object
-
#leet ⇒ String
Converts the given_string to leet speak.
- #leet? ⇒ Boolean
-
#unleet ⇒ String
Converts the leet speak string back to normal text.
Constructor Details
#initialize(given_string) ⇒ Translate
Returns a new instance of Translate.
60 61 62 63 64 |
# File 'lib/passweird/leet/translate.rb', line 60 def initialize(given_string) raise ArgumentError, "given_string must be a String" unless given_string.is_a?(String) @given_string = given_string end |
Instance Attribute Details
#given_string ⇒ Object (readonly)
Returns the value of attribute given_string.
15 16 17 |
# File 'lib/passweird/leet/translate.rb', line 15 def given_string @given_string end |
Class Method Details
.leet(given_string) ⇒ Object
36 37 38 |
# File 'lib/passweird/leet/translate.rb', line 36 def self.leet(given_string) new(given_string).leet end |
.leet?(given_string) ⇒ Boolean
40 41 42 |
# File 'lib/passweird/leet/translate.rb', line 40 def self.leet?(given_string) new(given_string).leet? end |
.leet_all(array_of_strings) ⇒ Object
44 45 46 47 48 |
# File 'lib/passweird/leet/translate.rb', line 44 def self.leet_all(array_of_strings) raise ArgumentError, "array_of_strings must be an Array" unless array_of_strings.is_a?(Array) array_of_strings.map { |string| leet(string) } end |
.unleet(given_string) ⇒ Object
50 51 52 |
# File 'lib/passweird/leet/translate.rb', line 50 def self.unleet(given_string) new(given_string).unleet end |
.unleet_all(array_of_strings) ⇒ Object
54 55 56 57 58 |
# File 'lib/passweird/leet/translate.rb', line 54 def self.unleet_all(array_of_strings) raise ArgumentError, "array_of_strings must be an Array" unless array_of_strings.is_a?(Array) array_of_strings.map { |string| unleet(string) } end |
Instance Method Details
#cipher ⇒ Object
84 85 86 |
# File 'lib/passweird/leet/translate.rb', line 84 def cipher @cipher ||= ExtendedCipher::CIPHER.merge(CIPHER) end |
#cipher_regex ⇒ Object
92 93 94 |
# File 'lib/passweird/leet/translate.rb', line 92 def cipher_regex @cipher_regex ||= Regexp.union(cipher.keys) end |
#inverted_cipher ⇒ Object
88 89 90 |
# File 'lib/passweird/leet/translate.rb', line 88 def inverted_cipher @inverted_cipher ||= cipher.invert end |
#inverted_cipher_regex ⇒ Object
96 97 98 |
# File 'lib/passweird/leet/translate.rb', line 96 def inverted_cipher_regex @inverted_cipher_regex ||= Regexp.union(inverted_cipher.keys) end |
#leet ⇒ String
Converts the given_string to leet speak
69 70 71 |
# File 'lib/passweird/leet/translate.rb', line 69 def leet given_string.upcase.gsub(inverted_cipher_regex, inverted_cipher) end |
#leet? ⇒ Boolean
73 74 75 |
# File 'lib/passweird/leet/translate.rb', line 73 def leet? mixed_characters?(given_string) && given_string != leet end |
#unleet ⇒ String
Converts the leet speak string back to normal text
80 81 82 |
# File 'lib/passweird/leet/translate.rb', line 80 def unleet given_string.upcase.gsub(cipher_regex, cipher) end |