norma43_parser 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +85 -0
  5. data/Rakefile +1 -0
  6. data/doc/classes.jpg +0 -0
  7. data/doc/cuaderno_43_-_junio_2012.pdf +800 -1
  8. data/lib/norma43/line_handlers.rb +54 -0
  9. data/lib/norma43/line_parsers/file_format_validator.rb +19 -0
  10. data/lib/norma43/line_parsers/line_parser.rb +42 -0
  11. data/lib/norma43/line_parsers/line_parsers.rb +69 -0
  12. data/lib/norma43/line_processors.rb +60 -0
  13. data/lib/norma43/models.rb +85 -0
  14. data/lib/norma43/parser.rb +79 -0
  15. data/lib/norma43/utils/contexts.rb +54 -0
  16. data/lib/norma43/utils/string_helpers.rb +10 -0
  17. data/lib/norma43/utils/typecaster.rb +19 -0
  18. data/lib/norma43/version.rb +3 -0
  19. data/lib/norma43.rb +8 -0
  20. data/norma43_parser.gemspec +27 -0
  21. data/spec/example1_parse_spec.rb +91 -0
  22. data/spec/fixtures/example1.n43 +10 -0
  23. data/spec/norma43/line_parsers/account_end_spec.rb +51 -0
  24. data/spec/norma43/line_parsers/account_start_spec.rb +55 -0
  25. data/spec/norma43/line_parsers/additional_currency_spec.rb +27 -0
  26. data/spec/norma43/line_parsers/additional_items_spec.rb +23 -0
  27. data/spec/norma43/line_parsers/document_end_spec.rb +16 -0
  28. data/spec/norma43/line_parsers/document_start_spec.rb +32 -0
  29. data/spec/norma43/line_parsers/transaction_spec.rb +55 -0
  30. data/spec/norma43/line_processors/account_end_spec.rb +34 -0
  31. data/spec/norma43/line_processors/account_start_spec.rb +40 -0
  32. data/spec/norma43/line_processors/additional_currency_spec.rb +42 -0
  33. data/spec/norma43/line_processors/additional_items_spec.rb +42 -0
  34. data/spec/norma43/line_processors/document_end_spec.rb +30 -0
  35. data/spec/norma43/line_processors/document_start_spec.rb +27 -0
  36. data/spec/norma43/line_processors/transaction_spec.rb +42 -0
  37. data/spec/norma43/parser_spec.rb +23 -0
  38. data/spec/norma43_spec.rb +13 -0
  39. data/spec/spec_helper.rb +20 -0
  40. data/spec/support/shared_examples_for_values_line_parsers.rb +15 -0
  41. metadata +173 -0
@@ -0,0 +1,55 @@
1
+ require "norma43/line_parsers/line_parsers"
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ RSpec.describe AccountStart do
6
+ let :account_start do
7
+ AccountStart.new "119999111101234567890408040409052000000001234569783MY ACCOUNT "
8
+ end
9
+
10
+ it "parses the bank code" do
11
+ expect(account_start.bank_code).to eq 9999
12
+ end
13
+
14
+ it "parses the branch code" do
15
+ expect(account_start.branch_code).to eq 1111
16
+ end
17
+
18
+ it "parses the account number" do
19
+ expect(account_start.account_number).to eq 123456789
20
+ end
21
+
22
+
23
+ it "parses the start date" do
24
+ expect(account_start.start_date).to be_a Date
25
+ expect(account_start.start_date.to_s).to eq "2004-08-04"
26
+ end
27
+
28
+ it "parses the end date" do
29
+ expect(account_start.end_date).to be_a Date
30
+ expect(account_start.end_date.to_s).to eq "2004-09-05"
31
+ end
32
+
33
+ it "parses the balance code" do
34
+ expect(account_start.balance_code).to eq 2
35
+ end
36
+
37
+ it "parses the balance amount" do
38
+ expect(account_start.balance_amount).to eq 123456
39
+ end
40
+
41
+ it "parses the currency code" do
42
+ expect(account_start.currency_code).to eq 978
43
+ end
44
+
45
+ it "parses the information mode code" do
46
+ expect(account_start.information_mode_code).to eq 3
47
+ end
48
+
49
+ it "parses the abbreviated name" do
50
+ expect(account_start.abbreviated_name).to eq "MY ACCOUNT"
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,27 @@
1
+ require "norma43/line_parsers/line_parsers"
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ RSpec.describe AdditionalCurrency do
6
+ let :additional_currency do
7
+ AdditionalCurrency.new "230197802030405600040 TRASPASO EJEMPLO "
8
+ end
9
+
10
+ it "parses the data code" do
11
+ expect(additional_currency.data_code).to eq 1
12
+ end
13
+
14
+ it "parses the first item" do
15
+ expect(additional_currency.currency_code).to eq 978
16
+ end
17
+
18
+ it "parses the second item" do
19
+ expect(additional_currency.amount).to eq 2030405600040
20
+ end
21
+
22
+ it "parses the free" do
23
+ expect(additional_currency.free).to eq "TRASPASO EJEMPLO"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require "norma43/line_parsers/line_parsers"
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ RSpec.describe AdditionalItem do
6
+ let :additional_items do
7
+ AdditionalItem.new "2301REF XXXXXXXXX TRASPASO EJEMPLO "
8
+ end
9
+
10
+ it "parses the data code" do
11
+ expect(additional_items.data_code).to eq 1
12
+ end
13
+
14
+ it "parses the first item" do
15
+ expect(additional_items.item_1).to eq "REF XXXXXXXXX"
16
+ end
17
+
18
+ it "parses the second item" do
19
+ expect(additional_items.item_2).to eq "TRASPASO EJEMPLO"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ require "norma43/line_parsers/line_parsers"
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ RSpec.describe DocumentEnd do
6
+ let :document_end do
7
+ DocumentEnd.new "88999999999999999999000048 "
8
+ end
9
+
10
+ it "parses the record number" do
11
+ expect(document_end.record_number).to eq 48
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require "norma43/line_parsers/line_parsers"
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ RSpec.describe DocumentStart do
6
+ let :document_start do
7
+ DocumentStart.new "00TI222222 2011102504133012345601TL1Norma43"
8
+ end
9
+
10
+ it "extracts the id" do
11
+ expect(document_start.id).to eq "TI222222"
12
+ end
13
+
14
+ it "extracts the created at timestamp" do
15
+ expect(document_start.created_at).to be_a Time
16
+ expect(document_start.created_at.strftime("%Y-%m-%d %H:%M:%S")).to eq "2011-10-25 04:13:30"
17
+ end
18
+
19
+ it "parses the deliver number" do
20
+ expect(document_start.delivery_number).to eq 1
21
+ end
22
+
23
+ it "parses the file type" do
24
+ expect(document_start.file_type).to eq "TL1"
25
+ end
26
+
27
+ it "parses file name" do
28
+ expect(document_start.name).to eq "Norma43"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,55 @@
1
+ require "norma43/line_parsers/line_parsers"
2
+
3
+ module Norma43
4
+ module LineParsers
5
+ RSpec.describe Transaction do
6
+ let :transaction do
7
+
8
+ Transaction.new "2256781127040805040805020092000000000012340000000000000000000000 REF 2"
9
+ end
10
+
11
+ it "parses the origin branch code" do
12
+ expect(transaction.origin_branch_code).to eq 1127
13
+ end
14
+
15
+ it "parses the transaction date" do
16
+ expect(transaction.transaction_date).to be_a Date
17
+ expect(transaction.transaction_date.to_s).to eq "2004-08-05"
18
+ end
19
+
20
+ it "parses the value date" do
21
+ expect(transaction.value_date).to be_a Date
22
+ expect(transaction.value_date.to_s).to eq "2004-08-05"
23
+ end
24
+
25
+ it "parses the shared item" do
26
+ expect(transaction.shared_item).to eq 2
27
+ end
28
+
29
+ it "parses the own item" do
30
+ expect(transaction.own_item).to eq 9
31
+ end
32
+
33
+ it "parses the amount_code" do
34
+ expect(transaction.amount_code).to eq 2
35
+ end
36
+
37
+ it "parses the amount" do
38
+ expect(transaction.amount).to eq 1234
39
+ end
40
+
41
+ it "parses the document number" do
42
+ expect(transaction.document_number).to eq 0
43
+ end
44
+
45
+ it "parses the reference 1" do
46
+ expect(transaction.reference_1).to eq 0
47
+ end
48
+
49
+ it "parses the reference 2" do
50
+ expect(transaction.reference_2).to eq "REF 2"
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,34 @@
1
+ require "norma43/utils/contexts"
2
+ require "norma43/line_processors"
3
+
4
+ module Norma43
5
+ module LineProcessors
6
+ RSpec.describe "AccountEnd" do
7
+ let :line do
8
+ double "Line", attributes: {}
9
+ end
10
+
11
+ let(:account){ Models::Account.new }
12
+ let(:contexts){ Contexts.new(
13
+ [
14
+ Models::Document.new,
15
+ account,
16
+ Models::Transaction.new
17
+ ])
18
+ }
19
+
20
+ it "moves to the nearest account and assigns the line attributes" do
21
+ allow(account).to receive(:attributes=)
22
+ AccountEnd.call line, contexts
23
+ expect(account).to have_received(:attributes=).with line.attributes
24
+ end
25
+
26
+ it "sets the account as the current context" do
27
+ new_contexts = AccountEnd.call line, contexts
28
+
29
+ expect(new_contexts.current).to be account
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,40 @@
1
+ require "norma43/utils/contexts"
2
+ require "norma43/line_processors"
3
+
4
+ module Norma43
5
+ module LineProcessors
6
+ RSpec.describe "AccountStart" do
7
+ let(:line){ double "Line", attributes: {} }
8
+ let(:document) { Models::Document.new }
9
+ let(:contexts){ Contexts.new}
10
+
11
+ before do
12
+ contexts.add document
13
+ end
14
+
15
+ it "instantiates a new account with the line attributes" do
16
+ allow(Models::Account).to receive :new
17
+
18
+ AccountStart.call line, contexts
19
+
20
+ expect(Models::Account).to have_received(:new).with line.attributes
21
+ end
22
+
23
+ context "when AccountStart is called" do
24
+ let(:fake_account) { double "Models::Account" }
25
+ before do
26
+ allow(Models::Account).to receive(:new) { fake_account }
27
+ end
28
+ let!(:subject) { AccountStart.call line, contexts }
29
+
30
+ it "adds the account to the document" do
31
+ expect(document.accounts).to include fake_account
32
+ end
33
+
34
+ it "sets the account as the current context" do
35
+ expect(subject.current).to be fake_account
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,42 @@
1
+ require "norma43/utils/contexts"
2
+ require "norma43/line_processors"
3
+
4
+ module Norma43
5
+ module LineProcessors
6
+ RSpec.describe "AdditionalCurrency" do
7
+ let(:line){ double "Line", attributes: {} }
8
+ let(:transaction){ Models::Transaction.new }
9
+ let(:contexts){ Contexts.new(
10
+ [
11
+ Models::Document.new ,
12
+ Models::Account.new ,
13
+ transaction
14
+ ])
15
+ }
16
+
17
+ it "instantiates a new AdditionalCurrency with the line attributes" do
18
+ allow(Models::AdditionalCurrency).to receive :new
19
+
20
+ AdditionalCurrency.call line, contexts
21
+
22
+ expect(Models::AdditionalCurrency).to have_received(:new).with line.attributes
23
+ end
24
+
25
+ context "when AdditionalCurrency is called" do
26
+ let(:fake_additional_currency) { double "Models::AdditionalCurrency" }
27
+ before do
28
+ allow(Models::AdditionalCurrency).to receive(:new) { fake_additional_currency }
29
+ end
30
+ let!(:subject) { AdditionalCurrency.call line, contexts }
31
+
32
+ it "adds the AdditionalCurrency to the account" do
33
+ expect(transaction.additional_currency).to eq fake_additional_currency
34
+ end
35
+
36
+ it "transaction is still the current contexts" do
37
+ expect(subject.current).to be transaction
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,42 @@
1
+ require "norma43/utils/contexts"
2
+ require "norma43/line_processors"
3
+
4
+ module Norma43
5
+ module LineProcessors
6
+ RSpec.describe "AdditionalItems" do
7
+ let(:line){ double "Line", attributes: {} }
8
+ let(:transaction){ Models::Transaction.new }
9
+ let(:contexts){ Contexts.new(
10
+ [
11
+ Models::Document.new ,
12
+ Models::Account.new ,
13
+ transaction
14
+ ])
15
+ }
16
+
17
+ it "instantiates a new AdditionalItem with the line attributes" do
18
+ allow(Models::AdditionalItem).to receive :new
19
+
20
+ AdditionalItem.call line, contexts
21
+
22
+ expect(Models::AdditionalItem).to have_received(:new).with line.attributes
23
+ end
24
+
25
+ context "when AdditionalItem is called" do
26
+ let(:fake_additional_item) { double "Models::AdditionalItem" }
27
+ before do
28
+ allow(Models::AdditionalItem).to receive(:new) { fake_additional_item }
29
+ end
30
+ let!(:subject) { AdditionalItem.call line, contexts }
31
+
32
+ it "adds the AdditionalItem to the account" do
33
+ expect(transaction.additional_items).to include fake_additional_item
34
+ end
35
+
36
+ it "transaction is still the current contexts" do
37
+ expect(subject.current).to be transaction
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,30 @@
1
+ require "norma43/utils/contexts"
2
+ require "norma43/line_processors"
3
+
4
+ module Norma43
5
+ module LineProcessors
6
+ RSpec.describe "DocumentEnd" do
7
+ class Thing
8
+ end
9
+
10
+ let(:line) { double "Line", record_number: 35 }
11
+
12
+ it "moves to the nearest document context" do
13
+ document = Models::Document.new
14
+ contexts = Contexts.new [Thing.new, document, Thing.new, Thing.new]
15
+
16
+ DocumentEnd.call line, contexts
17
+
18
+ expect(contexts.current).to eq document
19
+ end
20
+
21
+ it "sets the number of lines on the document" do
22
+ document = Models::Document.new
23
+
24
+ DocumentEnd.call line, Contexts.new(document)
25
+
26
+ expect(document.number_of_lines).to eq 35
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ require "norma43/utils/contexts"
2
+ require "norma43/line_processors"
3
+
4
+ module Norma43
5
+ module LineProcessors
6
+ RSpec.describe "DocumentStart" do
7
+ let (:line) { double "Line", attributes: {} }
8
+
9
+ it "instantiates a new document with the line attributes" do
10
+ allow(Models::Document).to receive :new
11
+
12
+ DocumentStart.call line, Contexts.new
13
+
14
+ expect(Models::Document).to have_received(:new).with line.attributes
15
+ end
16
+
17
+ it "sets the document as the current context" do
18
+ fake_document = double "Models::Document"
19
+ allow(Models::Document).to receive(:new) { fake_document }
20
+
21
+ contexts = DocumentStart.call line, Contexts.new
22
+
23
+ expect(contexts.current).to be fake_document
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ require "norma43/utils/contexts"
2
+ require "norma43/line_processors"
3
+
4
+ module Norma43
5
+ module LineProcessors
6
+ RSpec.describe "Transaction" do
7
+ let(:line){ double "Line", attributes: {} }
8
+ let(:account){ Models::Account.new }
9
+ let(:contexts){ Contexts.new(
10
+ [
11
+ Models::Document.new ,
12
+ account,
13
+ Models::Transaction.new
14
+ ])
15
+ }
16
+
17
+ it "instantiates a new transaction with the line attributes" do
18
+ allow(Models::Transaction).to receive :new
19
+
20
+ Transaction.call line, contexts
21
+
22
+ expect(Models::Transaction).to have_received(:new).with line.attributes
23
+ end
24
+
25
+ context "when Transaction is called" do
26
+ let(:fake_transaction) { double "Models::Transaction" }
27
+ before do
28
+ allow(Models::Transaction).to receive(:new) { fake_transaction }
29
+ end
30
+ let!(:subject) { Transaction.call line, contexts }
31
+
32
+ it "adds the transaction to the account" do
33
+ expect(account.transactions).to include fake_transaction
34
+ end
35
+
36
+ it "sets the transaction as the current context" do
37
+ expect(subject.current).to be fake_transaction
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,23 @@
1
+ require "norma43/parser"
2
+
3
+ module Norma43
4
+ RSpec.describe Parser do
5
+ describe "ensuring the file is valid" do
6
+ it "accepts a valid file starting with document" do
7
+ valid_file = "00TI222222 2011102504133012345601TL1Norma43 "
8
+ expect{ Parser.new(valid_file) }.to_not raise_error
9
+ end
10
+
11
+ it "accepts a valid file starting with an account" do
12
+
13
+ valid_file = "119999111101234567890408040409052000000001234569783MY ACCOUNT "
14
+ expect{ Parser.new(valid_file) }.to_not raise_error
15
+ end
16
+
17
+ it "should start with 00 or 11" do
18
+ invalid_file = "99TI222222 2011102504133012345601TL1Norma43 "
19
+ expect{ Parser.new(invalid_file) }.to raise_error{ Norma43::InvalidFileFormatError }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ require "norma43"
2
+
3
+ RSpec.describe Norma43 do
4
+ describe "#parse" do
5
+ it "returns the parser results" do
6
+ text = "some total-in text"
7
+ parser = double "Parser", result: "result"
8
+ expect(Norma43::Parser).to receive(:new).with(text) { parser }
9
+
10
+ expect(Norma43.parse(text)).to eq "result"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ if ENV["CI"]
2
+ require "codeclimate-test-reporter"
3
+ CodeClimate::TestReporter.start
4
+ end
5
+
6
+ RSpec.configure do |config|
7
+ config.order = :random
8
+ Kernel.srand config.seed
9
+
10
+ config.expect_with :rspec do |expectations|
11
+ expectations.syntax = :expect
12
+ end
13
+
14
+ config.mock_with :rspec do |mocks|
15
+ mocks.syntax = :expect
16
+ mocks.verify_partial_doubles = true
17
+ end
18
+ end
19
+
20
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
@@ -0,0 +1,15 @@
1
+ RSpec.shared_examples "a values line parser" do
2
+ describe "#values" do
3
+ it "finds first value" do
4
+ line = described_class.new " 847c7f90402aa750a7e257ed149f10834cy "
5
+
6
+ expect(line.values).to eq ["847c7f90402aa750a7e257ed149f10834cy"]
7
+ end
8
+
9
+ it "finds both values" do
10
+ line = described_class.new " 847c7f90402aa750a7e257ed149f10834cyf410cae4e8d1a5574y20c78702907a47f39 "
11
+
12
+ expect(line.values).to eq ["847c7f90402aa750a7e257ed149f10834cy", "f410cae4e8d1a5574y20c78702907a47f39"]
13
+ end
14
+ end
15
+ end