Class: Airhelp::Processor
- Inherits:
-
Object
- Object
- Airhelp::Processor
- Defined in:
- lib/airhelp/processor.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#index ⇒ Object
Returns the value of attribute index.
-
#input ⇒ Object
Returns the value of attribute input.
-
#options ⇒ Object
Returns the value of attribute options.
-
#output ⇒ Object
Returns the value of attribute output.
-
#records ⇒ Object
Returns the value of attribute records.
Instance Method Summary collapse
-
#initialize(input, output, options) ⇒ Processor
constructor
A new instance of Processor.
- #read ⇒ Object
- #start ⇒ Object
- #write ⇒ Object
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, ) @input = input @output = output @records = Array.new @errors = Array.new @index = 0 @options = end |
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
6 7 8 |
# File 'lib/airhelp/processor.rb', line 6 def errors @errors end |
#index ⇒ Object
Returns the value of attribute index.
6 7 8 |
# File 'lib/airhelp/processor.rb', line 6 def index @index end |
#input ⇒ Object
Returns the value of attribute input.
6 7 8 |
# File 'lib/airhelp/processor.rb', line 6 def input @input end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/airhelp/processor.rb', line 6 def @options end |
#output ⇒ Object
Returns the value of attribute output.
6 7 8 |
# File 'lib/airhelp/processor.rb', line 6 def output @output end |
#records ⇒ Object
Returns the value of attribute records.
6 7 8 |
# File 'lib/airhelp/processor.rb', line 6 def records @records end |
Instance Method Details
#read ⇒ Object
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 |
#start ⇒ Object
17 18 19 20 21 |
# File 'lib/airhelp/processor.rb', line 17 def start read write errors if @errors.any? end |
#write ⇒ Object
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 |