Class: CSVInterpreter

Inherits:
Interpreter show all
Defined in:
lib/suds/interpreter/csv_interpreter.rb

Instance Attribute Summary collapse

Attributes inherited from Interpreter

#data, #headers

Instance Method Summary collapse

Constructor Details

#initialize(raw_data) ⇒ CSVInterpreter

Returns a new instance of CSVInterpreter.



7
8
9
10
# File 'lib/suds/interpreter/csv_interpreter.rb', line 7

def initialize raw_data
  @raw_data = raw_data
  super()
end

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



5
6
7
# File 'lib/suds/interpreter/csv_interpreter.rb', line 5

def filepath
  @filepath
end

Instance Method Details

#interpretObject



13
14
15
16
17
18
19
# File 'lib/suds/interpreter/csv_interpreter.rb', line 13

def interpret
  CSV.parse(@raw_data, headers: true, header_converters: :symbol).each do |row|
    @headers = row.headers if @headers.nil? || @headers.empty?
    interpret_unit row
  end
  @data
end

#interpret_unit(unit) ⇒ Object



21
22
23
24
25
26
# File 'lib/suds/interpreter/csv_interpreter.rb', line 21

def interpret_unit unit
  raise "Headers have not be set." if @headers.empty?
  raise "Invalid data for current headers." if @headers.size != unit.size

  @data << unit.to_h
end