class BBS::Store::CsvImport
The rows of a Storeβs CSV file of earlier versions, read once for the import into SQLite. A broken row does not stop the import: a record that is not CSV (an unclosed quote runs until a line that is a whole row, at most MAX_LINES lines), has more fields than the header or no session_id is skipped, and its lines are set aside in <path>.rejected, under the header, for a sysop to look at.
csv = BBS::Store::CsvImport.new('data/callers.csv', %w[session_id name]) csv.rows # => [{ 'session_id' => 'a', 'name' => 'Ada' }, ...] csv.finish('data/callers.sqlite3') # renames the file, writes the rejects
Constants
- MAX_LINES
-
The most lines a quoted field may run over before its record counts as broken.
Attributes
The broken records, each the lines it was read from.
The good rows, each a Hash of column name => String or nil.
Public Class Methods
Source
# File lib/bbs/store/csv_import.rb, line 25 def initialize(path, columns) @path = path @columns = columns @rows = [] @rejected = [] read end
Reads the CSV file at path (as bytes: invalid UTF-8 is dropped, not fatal), keeping the columns (Array of String) of each row.
Public Instance Methods
Source
# File lib/bbs/store/csv_import.rb, line 42 def finish(database) File.rename(@path, "#{@path}.imported") File.write("#{@path}.rejected", @header + @rejected.join) if @rejected.any? "#{@rows.size} rows of #{@path} imported into #{database}" + (@rejected.any? ? ", #{@rejected.size} set aside in #{@path}.rejected" : '') end
Renames the CSV file to <path>.imported, writes the rejected records to <path>.rejected when there are any, and returns a line for the log naming database, where the rows went.