Class: CertBot::CsvAccessor
- Inherits:
-
Object
- Object
- CertBot::CsvAccessor
- Defined in:
- lib/cert_bot/io/csv_accessor.rb
Overview
Simple file reader using the csv library to read a csv file requires csv
Instance Attribute Summary collapse
-
#data ⇒ Array
readonly
An array containing the read data.
-
#delimiter ⇒ String
readonly
private
The delimiter character.
-
#filename ⇒ String
readonly
private
The target filename.
Instance Method Summary collapse
-
#append_row(row_entries) ⇒ Object
method to append a row at the end of the data file.
-
#initialize(filename, delimiter) ⇒ CsvAccessor
constructor
initialization.
-
#read_csv ⇒ Object
reads the csv file at the filename location and the given delimiter.
Constructor Details
#initialize(filename, delimiter) ⇒ CsvAccessor
initialization
16 17 18 19 20 |
# File 'lib/cert_bot/io/csv_accessor.rb', line 16 def initialize(filename, delimiter) @filename = filename @delimiter = delimiter @data = Array.new end |
Instance Attribute Details
#data ⇒ Array (readonly)
Returns an array containing the read data.
11 12 13 |
# File 'lib/cert_bot/io/csv_accessor.rb', line 11 def data @data end |
#delimiter ⇒ String (readonly, private)
Returns the delimiter character.
49 50 51 |
# File 'lib/cert_bot/io/csv_accessor.rb', line 49 def delimiter @delimiter end |
#filename ⇒ String (readonly, private)
Returns the target filename.
47 48 49 |
# File 'lib/cert_bot/io/csv_accessor.rb', line 47 def filename @filename end |
Instance Method Details
#append_row(row_entries) ⇒ Object
method to append a row at the end of the data file
38 39 40 41 42 |
# File 'lib/cert_bot/io/csv_accessor.rb', line 38 def append_row(row_entries) CSV.open(@filename, "a", col_sep: @delimiter) do |csv| csv << row_entries end end |
#read_csv ⇒ Object
reads the csv file at the filename location and the given delimiter
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cert_bot/io/csv_accessor.rb', line 24 def read_csv begin @data = CSV.read(@filename, col_sep: @delimiter) # remove nil entries @data.each { |line| line.compact! } rescue StandardError => e raise IOError, e..concat(".") end end |