Class: NewsReaderCli::ApiKeyValidate

Inherits:
Object
  • Object
show all
Defined in:
lib/news_reader_cli/api_key_validate.rb

Instance Method Summary collapse

Instance Method Details

#key_validateObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/news_reader_cli/api_key_validate.rb', line 2

def key_validate
  # verify if the file .env exist.
  # File.expand_path if return true, the file exist. if not, File.new create a file.
  # if not create one. Let user input the key.
  # if .env exists, load the file.
  if !File.exist?(".env")
    key_file = File.new(".env", "w+")
    puts "please type in API Key from API News:"

    # a condition verify key value is AES 128 Hex string 32 chars

    key = gets.chomp.strip

    while !key.match(/([0-9]|\w){32}/)  do

      # break if key == "exit"
      # need to get out of the loop and remove the file .env

      puts "\n      Your API keys value shows invalid.\n      Please check verify your API Key and type in again.\n      To exit, type in \"exit\"\n\n      HEREDOC\n\n      key = gets.chomp.strip\n    end\n\n    key_file.puts(\"key = \#{key}\")\n    key_file.close\n\n    #binding.pry\n  end\n\nend\n"