Class: Airhelp::Processor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output, options) ⇒ Processor

Returns a new instance of Processor.



8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/airhelp/processor.rb', line 6

def errors
  @errors
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/airhelp/processor.rb', line 6

def index
  @index
end

#inputObject

Returns the value of attribute input.



6
7
8
# File 'lib/airhelp/processor.rb', line 6

def input
  @input
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/airhelp/processor.rb', line 6

def options
  @options
end

#outputObject

Returns the value of attribute output.



6
7
8
# File 'lib/airhelp/processor.rb', line 6

def output
  @output
end

#recordsObject

Returns the value of attribute records.



6
7
8
# File 'lib/airhelp/processor.rb', line 6

def records
  @records
end

Instance Method Details

#readObject



23
24
25
26
27
28
29
# 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

#startObject



17
18
19
20
21
# File 'lib/airhelp/processor.rb', line 17

def start
  read
  write
  errors if @errors.any?
end

#writeObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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