class Tolq::Parsers::CSV::Parser

Public Class Methods

new(quality:, source_language_code:, target_language_code:) click to toggle source

Creates a new csv parser

@param quality [Symbol] the Tolq quality to order in @param source_language_code [String] The two letter source language code @param target_language_code [String] The two letter target language code @return [Tolq::Parsers::CSV::Parser

# File lib/csv/parser.rb, line 35
def initialize(quality:, source_language_code:, target_language_code:)
  @quality = quality
  @source_language_code = source_language_code
  @target_language_code = target_language_code
end
to_hash(csv_io, **parse_opts) click to toggle source

Helper method to convert a CSV into a annotated hash. Note that format is different from the Tolq api. It's just a hash.

@param csv_io [IO] String or IO of csv @param parse_opts [Hash] Options for the CSV parser @return [Hash] Hash representation of csv, i.e. { 'A1' => 'Hello World'

# File lib/csv/parser.rb, line 13
def self.to_hash(csv_io, **parse_opts)
  rdata = {}
  begin
    CSV.new(csv_io, parse_opts.merge(self.default_options)).each.with_index do |row, rowidx|
      row.each.with_index do |col, colidx|
        charcol = ColumnHelper.column_to_char(colidx)
        rdata["#{charcol}#{rowidx + 1}"] = fix_escaped_quotes(col) if col && col.length > 0
      end
    end
  rescue CSV::MalformedCSVError => e
    raise ParseError.new(e.message)
  end

  rdata
end

Private Class Methods

default_options() click to toggle source
# File lib/csv/parser.rb, line 62
def self.default_options
  major, minor, patch = RUBY_VERSION.split(".").map(&:to_i)
  if major >= 2 && minor >= 4
    { liberal_parsing: true }
  else
    {}
  end
end
fix_escaped_quotes(col) click to toggle source
# File lib/csv/parser.rb, line 71
def self.fix_escaped_quotes(col)
  col.gsub(/(?<!\\)\\"/,'""')
end

Public Instance Methods

parse(csv_io, exclude: [], **parse_opts) click to toggle source

Parses a csv

@param csv_io [IO] A string or IO object to parse @param parse_opts [Hash] Options for the CSV parser (i.e. col_sep, quote_char) @param exclude [Array] List of ranges to exclude in excel notation, i.e. [“A1:A3”] @return [Hash] A hash suitable to be converted to json for a Tolq api request

# File lib/csv/parser.rb, line 47
def parse(csv_io, exclude: [], **parse_opts)
  @exclude = to_numeric_exclude_ranges(exclude)
  request_data = extract_request_data_from_csv(csv_io, parse_opts)
  return nil if request_data.empty?

  {
    "request" => request_data,
    "source_language_code" => @source_language_code,
    "target_language_code" => @target_language_code,
    "quality" => @quality.to_s
  }
end

Private Instance Methods

excluded?(colidx, rowidx) click to toggle source
# File lib/csv/parser.rb, line 92
def excluded?(colidx, rowidx)
  !!@exclude.find do |range|
    from, to = range
    colidx >= from.last && colidx <= to.last &&
      rowidx >= from.first && rowidx <= to.first
  end
end
extract_request_data_from_csv(csv_text, parse_opts) click to toggle source
# File lib/csv/parser.rb, line 75
def extract_request_data_from_csv(csv_text, parse_opts)
  rdata = {}
  begin
    CSV.new(csv_text, parse_opts).each.with_index do |row, rowidx|
      row.each.with_index do |col, colidx|
        next if excluded?(colidx, rowidx)
        charcol = ColumnHelper.column_to_char(colidx)
        rdata["#{charcol}#{rowidx + 1}"] = { "text" => self.class.fix_escaped_quotes(col) } if col && col.length > 0
      end
    end
  rescue CSV::MalformedCSVError => e
    raise ParseError.new(e.message)
  end

  rdata
end
to_numeric_exclude_ranges(exclude_arr) click to toggle source
# File lib/csv/parser.rb, line 100
def to_numeric_exclude_ranges(exclude_arr)
  exclude_arr.map do |exrange|
    exrange
      .split(":")
      .map { |k| ColumnHelper.from_char_notation(k) }
  end
end