From 0acf8e16d5754ae616f6c60c1ddee6643bc9a72b Mon Sep 17 00:00:00 2001 From: Hundter Biede <6586509+hbiede@users.noreply.github.com> Date: Tue, 4 Jun 2024 12:03:52 -0600 Subject: [PATCH] Support removing timestamp columns from vote tables --- tests/vote_parser_test.rb | 10 +++++----- vote_parser.rb | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/vote_parser_test.rb b/tests/vote_parser_test.rb index 6ee7718..9df3bc6 100644 --- a/tests/vote_parser_test.rb +++ b/tests/vote_parser_test.rb @@ -1,5 +1,5 @@ # Author: Hundter Biede (hbiede.com) -# Version: 1.2 +# Version: 1.3 # License: MIT require_relative '../vote_parser' require_relative './helper' @@ -330,10 +330,10 @@ def test_init # Standard vote_file = 'init_vote_test.csv' CSV.open(vote_file, 'w') do |f| - f << %w[Password President VP] - f << %w[abc You I] - f << %w[123 Me Them] - f << %w[fake Villain Henchman] + f << %w[Timestamp Password President VP] + f << ['4/12/2024 17:22:30', 'abc', 'You', 'I'] + f << ['4/12/2024 17:22:30', '123', 'Me', 'Them'] + f << ['4/12/2024 17:22:30', 'fake', 'Villain', 'Henchman'] end token_file = 'init_token_test.csv' diff --git a/vote_parser.rb b/vote_parser.rb index c585723..961149c 100644 --- a/vote_parser.rb +++ b/vote_parser.rb @@ -19,10 +19,24 @@ end.parse! # :nocov: +# Strip timestamps from vote results +# +# @param [Array>] csv the contents of the given CSV to remove the timestamps from +# @return [Array>] the file with timestamps removed +def remove_timestamps(csv) + return [] if csv.nil? || csv.empty? + + timestamp_index = csv[0].index { |col| col =~ /Timestamp/i } + timestamp_index.nil? ? csv : csv.map do |row| + row.delete_at timestamp_index + row + end +end + # Read the contents of the given CSV file # # @param [String] file_name The name of the file -# @return [Array>]the contents of the given CSV file +# @return [Array>] the contents of the given CSV file def read_vote_csv(file_name) begin # @type [Array>] @@ -32,7 +46,7 @@ def read_vote_csv(file_name) exit 1 end csv.delete_if { |line| line.join =~ /^\s*$/ } # delete blank lines - csv + remove_timestamps csv end # Parse a vote record @@ -58,7 +72,7 @@ def self.vote_arg_count_validator(args) # Read the contents of the token file # # @param [String] file The file to read from - # @return [Array>]the contents of the token file + # @return [Array>] the contents of the token file def self.read_tokens(file) tokens = read_vote_csv file tokens.delete_at(0) # remove headers