class Airhelp::Processor

Attributes

errors[RW]
index[RW]
input[RW]
options[RW]
output[RW]
records[RW]

Public Class Methods

new(input, output, options) click to toggle source
# File lib/airhelp/processor.rb, line 8
def initialize(input, output, options)
  @input = input
  @output = output
  @records = Array.new
  @errors = Array.new
  @index = 0
  @options = options
end

Public Instance Methods

read() click to toggle source
# File lib/airhelp/processor.rb, line 23
def read
  CSV.parse(File.read(@input), headers: true) do |row|
    @index += 1
    @records << Record.new(id: row[0], carrier_code: row[1], flight_number: row[2], flight_date: row[3])
  end
  puts "Parsed #{index} rows"
end
start() click to toggle source
# File lib/airhelp/processor.rb, line 17
def start
  read
  write
  errors if @errors.any?
end
write() click to toggle source
# File lib/airhelp/processor.rb, line 31
def write
  CSV.open(@output, 'w', write_headers: true,
  headers: Record.headers) do |writer|
    valid_index = 0
    @records.each do |record|
      record.process
      if record.valid?
        writer << record.to_a
        valid_index += 1
      else
        @errors << record
      end
    end
    puts "Records counter: #{valid_index}"
  end
end