Skip to content

Commit

Permalink
use result
Browse files Browse the repository at this point in the history
  • Loading branch information
fwolfst committed Mar 3, 2023
1 parent d1031d0 commit d34186f
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion exe/shoplex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require 'shoplex'

puts Shoplex::process ARGF.read
puts Shoplex::process(ARGF.read).csv_out

exit 0
7 changes: 4 additions & 3 deletions exe/shoplex-web
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ __END__
%h2 Result
%h3 Errors and invoices to look out for
%div(style="color: red")
Errors here
#{@result.errors.count} errors here
#{@result.errors.inspect}
%h3 Lexware file
%a{role: 'button', href:"data:attachment/csv;charset=utf-8,#{URI.encode_www_form_component @result}", target: '_blank', download: 'filename.csv'}
%a{role: 'button', href:"data:attachment/csv;charset=utf-8,#{URI.encode_www_form_component @result.csv_out}", target: '_blank', download: 'filename.csv'}
Download file
%pre
= @result
= @result.csv_out
9 changes: 6 additions & 3 deletions lib/shoplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ module Shoplex
class Error < StandardError; end

def self.process file_content
shopware_invoices = Shoplex::ShopwareCSVParser.parse(file_content).invoices
bookings = shopware_invoices.map do |invoice|
result = Shoplex::ShopwareCSVParser.parse(file_content)
bookings = result.invoices.map do |invoice|
begin
Shoplex::ShippingSplitter::apply!(invoice:)
Shoplex::InvoiceBookingConverter.convert(invoice:)
rescue => e
result.mark_error(maker: self, error: :unknown, obj: [e, invoice])
STDERR.puts e
STDERR.puts e.backtrace
end
end.compact

return Shoplex::LexwareCSV.create_from(bookings:)
result.csv_out = Shoplex::LexwareCSV.create_from(bookings:)
return result
end
end
3 changes: 2 additions & 1 deletion lib/shoplex/result.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module Shoplex
class Result
attr_accessor :invoices
attr_accessor :errors
attr_reader :errors
attr_accessor :csv_out

def initialize
@invoices = []
Expand Down
2 changes: 2 additions & 0 deletions lib/shoplex/shopware_csv_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def self.parse csv_file_content
result.invoices << create_invoice_from(row:)
rescue => e
result.mark_error(maker: self, error: :creation_failed, obj: [e, row])
STDERR.puts e
STDERR.puts e.backtrace
end
else
result.mark_error(maker: self, error: :no_invoice_number, obj: row)
Expand Down
4 changes: 2 additions & 2 deletions test/test_shoplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def test_that_it_has_a_version_number
end

def test_it_ignores_lines_without_invoice_number
result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1))
result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1)).csv_out
assert_equal 3, result_file_content.lines.count
end

def test_it_does_the_whole_shebang
result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1))
result_file_content = Shoplex::process(File.read('test/files/two_lines_one_invoice_shopware.csv', encoding: Encoding::ISO_8859_1)).csv_out
expected = <<~CSV
26.10.2022,6010,6010 90067 LastNameOfBill,59.39,11100,0,EUR
26.10.2022,6010,6010 90067 LastNameOfBill,2.23,0,8300,EUR
Expand Down

0 comments on commit d34186f

Please sign in to comment.