Class: CertBot::CsvAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/cert_bot/io/csv_accessor.rb

Overview

Simple file reader using the csv library to read a csv file requires csv

Raises:

  • (IOError)

    if csv throws an exception

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, delimiter) ⇒ CsvAccessor

initialization

Parameters:

  • filename (String)

    filepath of the file which should be read

  • delimiter (String)

    the column delimiter that is need to read the file



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

#dataArray (readonly)

Returns an array containing the read data.

Returns:

  • (Array)

    an array containing the read data



11
12
13
# File 'lib/cert_bot/io/csv_accessor.rb', line 11

def data
  @data
end

#delimiterString (readonly, private)

Returns the delimiter character.

Returns:

  • (String)

    the delimiter character



49
50
51
# File 'lib/cert_bot/io/csv_accessor.rb', line 49

def delimiter
  @delimiter
end

#filenameString (readonly, private)

Returns the target filename.

Returns:

  • (String)

    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

Parameters:

  • row_entries (Array)

    the entries for the csv 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_csvObject

reads the csv file at the filename location and the given delimiter

Raises:

  • (IOError)

    if an error occurs while the file is read



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.message.concat(".")
  end
end