Class: CsvParser

Inherits:
Object
  • Object
show all
Defined in:
lib/ebutil.rb

Defined Under Namespace

Classes: HeaderException

Instance Method Summary collapse

Constructor Details

#initialize(translations) ⇒ CsvParser

Returns a new instance of CsvParser.



185
186
187
# File 'lib/ebutil.rb', line 185

def initialize translations
  @translations = translations
end

Instance Method Details

#parse(csv_content, &block) ⇒ Object



189
190
191
192
# File 'lib/ebutil.rb', line 189

def parse(csv_content, &block)
  lines = csv_content.encode('utf-8', 'binary', :invalid => :replace, :undef => :replace).split($/)
  LineParser.new(parse_keys(lines.shift), &block).parse(lines)
end

#parse_keys(head) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ebutil.rb', line 194

def parse_keys(head)
  head.strip!
  keys = {}
  translations = @translations.dup
  head.split('","').each_with_index do |header_cell, index|
    header_cell = header_cell.unquote
    keys[index] = translations.delete header_cell if @translations.has_key?(header_cell)
  end
  raise HeaderException.translations_missing(translations) unless translations.empty?
  return keys
end