Skip to content

Commit

Permalink
Support removing timestamp columns from vote tables
Browse files Browse the repository at this point in the history
  • Loading branch information
hbiede committed Jun 4, 2024
1 parent fa3bc5e commit 0acf8e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
10 changes: 5 additions & 5 deletions tests/vote_parser_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Author: Hundter Biede (hbiede.com)
# Version: 1.2
# Version: 1.3
# License: MIT
require_relative '../vote_parser'
require_relative './helper'
Expand Down Expand Up @@ -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'
Expand Down
20 changes: 17 additions & 3 deletions vote_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,24 @@
end.parse!
# :nocov:

# Strip timestamps from vote results
#
# @param [Array<Array<String>>] csv the contents of the given CSV to remove the timestamps from
# @return [Array<Array<String>>] 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<Array<String>>]the contents of the given CSV file
# @return [Array<Array<String>>] the contents of the given CSV file
def read_vote_csv(file_name)
begin
# @type [Array<Array<String>>]
Expand All @@ -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
Expand All @@ -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<Array<String>>]the contents of the token file
# @return [Array<Array<String>>] the contents of the token file
def self.read_tokens(file)
tokens = read_vote_csv file
tokens.delete_at(0) # remove headers
Expand Down

0 comments on commit 0acf8e1

Please sign in to comment.