class Fech::CsvDoctor

Public Class Methods

parse_row(file_path, opts) { |safe_line(line, clean_opts(opts))| ... } click to toggle source

Skips FasterCSV's whole-file wrapper, and passes each line in the file to a function that will parse it individually. @option opts [Boolean] :row_type yield only rows that match this type

# File lib/fech/csv.rb, line 41
def self.parse_row(file_path, opts)
  File.open(file_path, "r:#{opts[:encoding]}").each do |line|
    # Skip empty lines
    next if line.strip.empty?

    # Skip non-matching row-types
    next if opts.key?(:row_type) && !Fech.regexify(opts[:row_type]).match(line)

    yield safe_line(line, clean_opts(opts))
  end
end
safe_line(line, opts) click to toggle source

Tries to parse the line with FasterCSV.parse_line and the given quote_char. If this fails, try again with the “0” quote_char. @param [String] line the file's row to parse @options opts :quote_char the quote_char to try initially

# File lib/fech/csv.rb, line 57
def self.safe_line(line, opts)
  # :encoding isn't a valid argument to Csv#parse_line
  opts.delete(:encoding)
  begin
    parse_line(line, opts)
  rescue Fech::Csv::MalformedCSVError
    row = parse_line(line, clean_opts(opts).merge(:quote_char => "\0"))
    row.map! { |val| safe_value(val) }
  end
end
safe_value(val) click to toggle source

Removes extraneous quotes from values.

# File lib/fech/csv.rb, line 69
def self.safe_value(val)
  return val unless val.is_a?(String)
  begin
    parse_line(val).first
  rescue Fech::Csv::MalformedCSVError
    val
  end
end