module FlatFile::CSV

Public Class Methods

from_file(filepath) click to toggle source

Read a CSV file and return its contents as an array of hashes.

@param filepath [String] Path to the CSV file. @return [Array<Hash>]

# File lib/flat_file/csv.rb, line 10
def self.from_file(filepath)
  rows = []
  begin
    ::CSV.foreach(filepath, headers: true) do |row|
      rows.append(row)
    end
    return rows
  rescue StandardError => e
    # if defined?(Rails)
    #   Rails.logger.error({
    #     message: "Error reading CSV file",
    #     filepath: filepath,
    #     error: e,
    #   })
    # end
    return rows
  end
end
from_stream(data) click to toggle source

Read a CSV stream and return its contents as an array of hashes.

@param data [String, IO] Stream of CSV data. @return [Array<Hash>]

# File lib/flat_file/csv.rb, line 33
def self.from_stream(data)
  rows = []
  begin
    ::CSV.new(data, headers: true).each do |row|
      rows.append(row)
    end
    return rows
  rescue StandardError => e
    # if defined?(Rails)
    #   Rails.logger.error({
    #     message: "Error reading CSV data",
    #     error: e,
    #   })
    # end
    return rows
  end
end