Class: EnigmaEncrypto::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/enigma_encrypto/encrypt.rb

Instance Method Summary collapse

Constructor Details

#initializeEncrypt

Returns a new instance of Encrypt.



7
8
9
10
11
# File 'lib/enigma_encrypto/encrypt.rb', line 7

def initialize
  @file_handler = FileHandler.new
  @rotator = Rotator.new
  @rotation_num_gen = RotationNumGen.new
end

Instance Method Details

#actionObject



37
38
39
40
41
42
43
44
45
# 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_argsObject



23
24
25
26
27
28
# 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

#encryptObject



30
31
32
33
34
35
# 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_argsObject



13
14
15
16
17
18
19
20
21
# 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