class NewsReaderCli::ApiKeyValidate

Public Instance Methods

key_validate() click to toggle source
# File lib/news_reader_cli/api_key_validate.rb, line 2
def key_validate

  if !File.exist?(".env")
    puts "please type in API Key from API News:"
    key = gets.chomp.strip

    url = 'https://newsapi.org/v2/top-headlines?'\
          'country=us&'\
          "apiKey=#{key}&"\
          'pageSize=100'
    begin
      req = open(url)
    rescue
      puts "You API Key is not valid, please try again."
      return self.key_validate
    end

    key_file = File.new(".env", "w+")
    key_file.puts("key = #{key}")
    key_file.close
  end
end