class EnigmaEncrypto::Encrypt

Public Class Methods

new() click to toggle source
# File lib/enigma_encrypto/encrypt.rb, line 7
def initialize
  @file_handler = FileHandler.new
  @rotator = Rotator.new
  @rotation_num_gen = RotationNumGen.new
end

Public Instance Methods

action() click to toggle source
# File lib/enigma_encrypto/encrypt.rb, line 37
def action
  exit if !get_command_args
  exit if !check_command_args
  encrypt
  key = @rotation_num_gen.showkey
  date = @rotation_num_gen.today_date
  puts "Created #{@encrypted} with the key #{key} and date #{date}"
  true
end
check_command_args() click to toggle source
# File lib/enigma_encrypto/encrypt.rb, line 23
def check_command_args
  operation = @file_handler.check_file_useability(@message, @encrypted)
  return false if operation == false || operation == "c"
  File.truncate(@encrypted, 0) if operation == "w"
  true
end
encrypt() click to toggle source
# File lib/enigma_encrypto/encrypt.rb, line 30
def encrypt
  @opened_msg = @file_handler.read(@message)
  length = @opened_msg.size
  encrypt_message(length)
  @opened_msg.close
end
get_command_args() click to toggle source
# File lib/enigma_encrypto/encrypt.rb, line 13
def get_command_args
  if ARGV.length != 2
    puts "Oooops!\nIncorrect number of arguments supplied.\nTry again."
    false
  else
    @message = ARGV[0]
    @encrypted = ARGV[1]
  end
end

Private Instance Methods

encrypt_message(length) click to toggle source
# File lib/enigma_encrypto/encrypt.rb, line 49
def encrypt_message(length)
  rotation_num_index = 0
  while length > 0
    character_in_msg = @opened_msg.getc
    rotate_msg(character_in_msg, rotation_num_index)
    rotation_num_index += 1
    rotation_num_index = 0 if rotation_num_index == 4
    length -= 1
  end
end
rotate_msg(character, index) click to toggle source
# File lib/enigma_encrypto/encrypt.rb, line 60
def rotate_msg(character, index)
  @rotation = @rotation_num_gen.rotation_num_array
  rotated_char = @rotator.rotate_text(character, @rotation[index])
  @file_handler.writer(@encrypted, rotated_char)
end