Class: Passweird::Leet::Translate

Inherits:
Object
  • Object
show all
Defined in:
lib/passweird/leet/translate.rb

Overview

The Translate class is responsible for converting a given string into leet speak (1337 5p34k) and converting it back to normal text.

Example usage:

translator = Passweird::Leet::Translate.new("example")
leet_string = translator.leet
# => "3x4mpl3"
normal_string = translator.unleet
# => "example"

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_string) ⇒ Translate

Returns a new instance of Translate.

Raises:

  • (ArgumentError)


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_stringObject (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

Returns:

  • (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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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

#cipherObject



84
85
86
# File 'lib/passweird/leet/translate.rb', line 84

def cipher
  @cipher ||= ExtendedCipher::CIPHER.merge(CIPHER)
end

#cipher_regexObject



92
93
94
# File 'lib/passweird/leet/translate.rb', line 92

def cipher_regex
  @cipher_regex ||= Regexp.union(cipher.keys)
end

#inverted_cipherObject



88
89
90
# File 'lib/passweird/leet/translate.rb', line 88

def inverted_cipher
  @inverted_cipher ||= cipher.invert
end

#inverted_cipher_regexObject



96
97
98
# File 'lib/passweird/leet/translate.rb', line 96

def inverted_cipher_regex
  @inverted_cipher_regex ||= Regexp.union(inverted_cipher.keys)
end

#leetString

Converts the given_string to leet speak

Returns:

  • (String)

    the converted leet speak string



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

Returns:

  • (Boolean)


73
74
75
# File 'lib/passweird/leet/translate.rb', line 73

def leet?
  mixed_characters?(given_string) && given_string != leet
end

#unleetString

Converts the leet speak string back to normal text

Returns:

  • (String)

    the converted normal text string



80
81
82
# File 'lib/passweird/leet/translate.rb', line 80

def unleet
  given_string.upcase.gsub(cipher_regex, cipher)
end