Created
March 19, 2020 17:49
-
-
Save alexshagov/aeb3d99344ded9469f56ffbc8a253f11 to your computer and use it in GitHub Desktop.
Archive creator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# CLI tool, accepts user input; | |
# Saves as txt file | |
# archives that txt file | |
require 'pry' | |
require 'zip' | |
USER_INPUT_FILENAME = 'user_input.txt' | |
ARCHIVE_BASE_NAME = 'archive' | |
ARCHIVE_EXTENSION = '.zip' | |
class InvalidUserInput < StandardError; end | |
def process_user_input | |
puts 'Please, write something ...' | |
user_input = gets.chomp | |
puts "Your input: #{user_input}" | |
validate_user_input!(user_input) | |
create_file!(user_input) | |
create_archive! | |
end | |
def create_file!(user_input) | |
File.open(USER_INPUT_FILENAME, 'a') do |f| | |
f.write(user_input) | |
end | |
end | |
def create_archive! | |
archive_name = ARCHIVE_BASE_NAME + Time.now.to_i.to_s + ARCHIVE_EXTENSION | |
Zip::File.open(archive_name, Zip::File::CREATE) do |zipfile| | |
zipfile.add(USER_INPUT_FILENAME, File.join(USER_INPUT_FILENAME)) | |
end | |
end | |
def validate_user_input!(user_input) | |
raise InvalidUserInput if user_input.empty? | |
end | |
process_user_input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment