class Encrubto::Caesar::Encryptor

Constants

ORIGINAL

Public Instance Methods

decrypt(encrypted, offset) click to toggle source
# File lib/encrubto/caesar/encryptor.rb, line 19
def decrypt(encrypted, offset)
  if encrypted.is_a?(String) && offset.is_a?(Integer)
    if 0 <= offset && offset <= 26
      caesar = shift(offset)
      encrypted.tr("#{ caesar }#{ caesar.to_s.upcase }", "#{ ORIGINAL }#{ ORIGINAL.upcase }")
    else
      raise 'Offset must be between 0 and 26!'
    end
  else
      raise 'First param must be String, second param must be Integer!'
  end
end
encrypt(str, offset) click to toggle source
# File lib/encrubto/caesar/encryptor.rb, line 6
def encrypt(str, offset)
  if str.is_a?(String) && offset.is_a?(Integer)
    if 0 <= offset && offset <= 26
      caesar = shift(offset)
      str.tr("#{ ORIGINAL }#{ ORIGINAL.upcase }", "#{ caesar }#{ caesar.to_s.upcase }")
    else
      raise 'Offset must be between 0 and 26!'
    end
  else
    raise 'First param must be String, second param must be Integer!'
  end
end
shift(offset) click to toggle source
# File lib/encrubto/caesar/encryptor.rb, line 32
def shift(offset)
  caesar_first_part = ORIGINAL[offset..ORIGINAL.length-1]
  caesar_second_part = ORIGINAL[0..offset-1]
  caesar_first_part.to_s + caesar_second_part.to_s
end