class Tolq::Parsers::CSV::Unparser
Unparses a tolq response to CSV
suitable
Public Instance Methods
from_hash(translations, **args)
click to toggle source
Unparses a translations hash of key => translation
@param translations [Hash] A hash of translations with key => translation @return [String] A csv formatted string
# File lib/csv/unparser.rb, line 19 def from_hash(translations, **args) generate_csv(translations) end
from_tolq_response(tolq_response, **args)
click to toggle source
Unparses the tolq response
@param tolq_response [Hash] A parsed response from our api @return [String] A csv formatted string
# File lib/csv/unparser.rb, line 10 def from_tolq_response(tolq_response, **args) translations = tolq_response['translations'].values.first generate_csv(translations) end
Private Instance Methods
build_translation_matrix(translations_hash)
click to toggle source
# File lib/csv/unparser.rb, line 37 def build_translation_matrix(translations_hash) @maxcol = 0 translations_hash.inject([]) do |matrix, (k,translation)| row,col = ColumnHelper.from_char_notation(k) matrix[row] ||= [] matrix[row][col] = translation @maxcol = col if col > @maxcol matrix end end
generate_csv(translations)
click to toggle source
# File lib/csv/unparser.rb, line 25 def generate_csv(translations) CSV.generate(force_quotes: true) do |csv| build_translation_matrix(translations).each do |row| if row.nil? # skipped empty rows csv << [].fill(nil, 0..@maxcol) else csv << row.fill(nil, row.length..@maxcol) end end end end