norma43_parser 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE.md +13 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +5 -0
  4. data/.rubocop.yml +234 -0
  5. data/Gemfile +7 -1
  6. data/Gemfile.lock +73 -0
  7. data/README.md +1 -1
  8. data/Rakefile +2 -0
  9. data/lib/norma43.rb +9 -3
  10. data/lib/norma43/line_handlers.rb +2 -3
  11. data/lib/norma43/line_parsers/account_end.rb +18 -0
  12. data/lib/norma43/line_parsers/account_start.rb +18 -0
  13. data/lib/norma43/line_parsers/additional_currency.rb +12 -0
  14. data/lib/norma43/line_parsers/additional_item.rb +11 -0
  15. data/lib/norma43/line_parsers/document_end.rb +9 -0
  16. data/lib/norma43/line_parsers/document_start.rb +13 -0
  17. data/lib/norma43/line_parsers/file_format_validator.rb +15 -12
  18. data/lib/norma43/line_parsers/line_parser.rb +29 -28
  19. data/lib/norma43/line_parsers/transaction.rb +18 -0
  20. data/lib/norma43/line_processors.rb +2 -2
  21. data/lib/norma43/models.rb +5 -4
  22. data/lib/norma43/parser.rb +38 -42
  23. data/lib/norma43/utils/contexts.rb +40 -37
  24. data/lib/norma43/utils/string_helpers.rb +10 -6
  25. data/lib/norma43/utils/typecaster.rb +16 -12
  26. data/lib/norma43/version.rb +3 -1
  27. data/norma43_parser.gemspec +11 -9
  28. data/spec/example1_parse_spec.rb +12 -11
  29. data/spec/norma43/line_parsers/account_end_spec.rb +1 -1
  30. data/spec/norma43/line_parsers/account_start_spec.rb +1 -2
  31. data/spec/norma43/line_parsers/additional_currency_spec.rb +1 -1
  32. data/spec/norma43/line_parsers/additional_items_spec.rb +1 -1
  33. data/spec/norma43/line_parsers/document_end_spec.rb +1 -2
  34. data/spec/norma43/line_parsers/document_start_spec.rb +1 -1
  35. data/spec/norma43/line_parsers/transaction_spec.rb +1 -3
  36. data/spec/norma43/line_processors/account_end_spec.rb +6 -8
  37. data/spec/norma43/line_processors/account_start_spec.rb +8 -9
  38. data/spec/norma43/line_processors/additional_currency_spec.rb +6 -7
  39. data/spec/norma43/line_processors/additional_items_spec.rb +6 -7
  40. data/spec/norma43/line_processors/document_end_spec.rb +5 -6
  41. data/spec/norma43/line_processors/document_start_spec.rb +3 -4
  42. data/spec/norma43/line_processors/transaction_spec.rb +6 -7
  43. data/spec/norma43/parser_spec.rb +5 -4
  44. data/spec/norma43_spec.rb +2 -0
  45. data/spec/spec_helper.rb +1 -4
  46. data/spec/support/shared_examples_for_values_line_parsers.rb +2 -0
  47. metadata +41 -32
  48. data/lib/norma43/line_parsers/line_parsers.rb +0 -69
@@ -1,19 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "time"
2
4
 
3
5
  module Norma43
4
- module Typecaster
5
- def self.cast value, type
6
- casters.fetch(type).call(value) unless value == ""
7
- end
6
+ module Utils
7
+ module Typecaster
8
+ def self.cast(value, type)
9
+ casters.fetch(type).call(value) unless value == ""
10
+ end
8
11
 
9
- def self.casters
10
- {
11
- integer: ->(value) { value.to_i },
12
- time: ->(value) { Time.strptime(value, "%Y%m%d%H%M%S%N") },
13
- date: ->(value) { Date.strptime(value, "%y%m%d") },
14
- string: ->(value) { value unless value.match(/\A0+\Z/) },
15
- raw: ->(value) { value }
16
- }
12
+ def self.casters
13
+ {
14
+ integer: ->(value) { value.to_i },
15
+ time: ->(value) { Time.strptime(value, "%Y%m%d%H%M%S%N") },
16
+ date: ->(value) { Date.strptime(value, "%y%m%d") },
17
+ string: ->(value) { value unless value.match?(/\A0+\Z/) },
18
+ raw: ->(value) { value }
19
+ }
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Norma43
2
- VERSION = "1.0.1"
4
+ VERSION = "2.0.0"
3
5
  end
@@ -1,5 +1,6 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require "norma43/version"
5
6
 
@@ -8,20 +9,21 @@ Gem::Specification.new do |spec|
8
9
  spec.version = Norma43::VERSION
9
10
  spec.authors = ["Sequra engineering"]
10
11
  spec.email = ["dev@sequra.es"]
11
- spec.summary = %q{Parses banks transactions files specified in rule 43}
12
+ spec.summary = "Parses banks transactions files specified in rule 43"
12
13
  spec.homepage = "https://github.com/sequra/norma43_parser"
13
14
  spec.license = "MIT"
14
15
 
15
- spec.required_ruby_version = "~> 2.0"
16
+ spec.required_ruby_version = "~> 2.4"
16
17
 
17
18
  spec.files = `git ls-files -z`.split("\x0")
18
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
21
  spec.require_paths = ["lib"]
21
22
 
22
- spec.add_development_dependency "bundler", "~> 1.5"
23
- spec.add_development_dependency "rake"
24
- spec.add_development_dependency "byebug"
25
- spec.add_development_dependency "rspec"
26
- spec.add_runtime_dependency "virtus"
23
+ spec.add_runtime_dependency "virtus", "~> 1.0"
24
+ spec.add_runtime_dependency "zeitwerk", "~> 2.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.0"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
27
29
  end
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "norma43"
2
4
 
3
5
  RSpec.describe Norma43 do
4
6
  describe "parse" do
5
-
6
7
  let(:document) do
7
- file = File.open( File.join(__dir__, "fixtures/example1.n43"),
8
+ file = File.open(File.join(__dir__, "fixtures/example1.n43"),
8
9
  encoding: "iso-8859-1")
9
10
  Norma43.parse file
10
11
  end
@@ -17,7 +18,7 @@ RSpec.describe Norma43 do
17
18
  let(:account) { document.accounts.first }
18
19
 
19
20
  it "stores expected attributes from AccountStart parser" do
20
- expect(account).to have_attributes({
21
+ expect(account).to have_attributes(
21
22
  "bank_code" => 9999,
22
23
  "branch_code" => 1111,
23
24
  "account_number" => 123456789,
@@ -26,18 +27,18 @@ RSpec.describe Norma43 do
26
27
  "currency_code" => 1,
27
28
  "information_mode_code" => 3,
28
29
  "abbreviated_name" => "MY ACCOUNT"
29
- })
30
+ )
30
31
  end
31
32
 
32
33
  it "stores expected attributes from AccountEnd parser" do
33
- expect(account).to have_attributes({
34
+ expect(account).to have_attributes(
34
35
  "balance_code" => 2,
35
36
  "balance_amount" => 78889999999999,
36
37
  "debit_entries" => 4,
37
38
  "debit_amount" => 4936,
38
39
  "credit_entries" => 2,
39
40
  "credit_amount" => 999999,
40
- })
41
+ )
41
42
  end
42
43
 
43
44
  describe "transactions" do
@@ -48,7 +49,7 @@ RSpec.describe Norma43 do
48
49
  describe "first transaction" do
49
50
  let(:transaction) { account.transactions[0] }
50
51
  it "stores expected attributes" do
51
- expect(transaction).to have_attributes({
52
+ expect(transaction).to have_attributes(
52
53
  "origin_branch_code" => 6700,
53
54
  "transaction_date" => Date.parse("2004-04-08"),
54
55
  "value_date" => Date.parse("2004-04-08"),
@@ -59,7 +60,7 @@ RSpec.describe Norma43 do
59
60
  "document_number" => 0,
60
61
  "reference_1" => 0,
61
62
  "reference_2" => nil,
62
- })
63
+ )
63
64
  end
64
65
  end
65
66
 
@@ -70,13 +71,13 @@ RSpec.describe Norma43 do
70
71
  end
71
72
 
72
73
  describe "first additional item" do
73
- let(:additional_item) { account.transactions[0].additional_items[0]}
74
+ let(:additional_item) { account.transactions[0].additional_items[0] }
74
75
  it "stores expected attributes" do
75
- expect(additional_item).to have_attributes({
76
+ expect(additional_item).to have_attributes(
76
77
  "data_code" => 1,
77
78
  "item_1" => "XXXXXXXXX",
78
79
  "item_2" => nil
79
- })
80
+ )
80
81
  end
81
82
  end
82
83
 
@@ -1,4 +1,4 @@
1
- require "norma43/line_parsers/line_parsers"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
4
  module LineParsers
@@ -1,4 +1,4 @@
1
- require "norma43/line_parsers/line_parsers"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
4
  module LineParsers
@@ -49,7 +49,6 @@ module Norma43
49
49
  it "parses the abbreviated name" do
50
50
  expect(account_start.abbreviated_name).to eq "MY ACCOUNT"
51
51
  end
52
-
53
52
  end
54
53
  end
55
54
  end
@@ -1,4 +1,4 @@
1
- require "norma43/line_parsers/line_parsers"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
4
  module LineParsers
@@ -1,4 +1,4 @@
1
- require "norma43/line_parsers/line_parsers"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
4
  module LineParsers
@@ -1,4 +1,4 @@
1
- require "norma43/line_parsers/line_parsers"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
4
  module LineParsers
@@ -10,7 +10,6 @@ module Norma43
10
10
  it "parses the record number" do
11
11
  expect(document_end.record_number).to eq 48
12
12
  end
13
-
14
13
  end
15
14
  end
16
15
  end
@@ -1,4 +1,4 @@
1
- require "norma43/line_parsers/line_parsers"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
4
  module LineParsers
@@ -1,10 +1,9 @@
1
- require "norma43/line_parsers/line_parsers"
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Norma43
4
4
  module LineParsers
5
5
  RSpec.describe Transaction do
6
6
  let :transaction do
7
-
8
7
  Transaction.new "2256781127040805040805020092000000000012340000000000000000000000 REF 2"
9
8
  end
10
9
 
@@ -52,4 +51,3 @@ module Norma43
52
51
  end
53
52
  end
54
53
  end
55
-
@@ -1,19 +1,18 @@
1
- require "norma43/utils/contexts"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineProcessors
6
- RSpec.describe "AccountEnd" do
5
+ RSpec.describe AccountEnd do
7
6
  let :line do
8
7
  double "Line", attributes: {}
9
8
  end
10
9
 
11
- let(:account){ Models::Account.new }
12
- let(:contexts){ Contexts.new(
10
+ let(:account) { Norma43::Models::Account.new }
11
+ let(:contexts) { Norma43::Utils::Contexts.new(
13
12
  [
14
- Models::Document.new,
13
+ Norma43::Models::Document.new,
15
14
  account,
16
- Models::Transaction.new
15
+ Norma43::Models::Transaction.new
17
16
  ])
18
17
  }
19
18
 
@@ -31,4 +30,3 @@ module Norma43
31
30
  end
32
31
  end
33
32
  end
34
-
@@ -1,29 +1,28 @@
1
- require "norma43/utils/contexts"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineProcessors
6
5
  RSpec.describe "AccountStart" do
7
- let(:line){ double "Line", attributes: {} }
8
- let(:document) { Models::Document.new }
9
- let(:contexts){ Contexts.new}
6
+ let(:line) { double "Line", attributes: {} }
7
+ let(:document) { Norma43::Models::Document.new }
8
+ let(:contexts) { Norma43::Utils::Contexts.new }
10
9
 
11
10
  before do
12
11
  contexts.add document
13
12
  end
14
13
 
15
14
  it "instantiates a new account with the line attributes" do
16
- allow(Models::Account).to receive :new
15
+ allow(Norma43::Models::Account).to receive :new
17
16
 
18
17
  AccountStart.call line, contexts
19
18
 
20
- expect(Models::Account).to have_received(:new).with line.attributes
19
+ expect(Norma43::Models::Account).to have_received(:new).with line.attributes
21
20
  end
22
21
 
23
22
  context "when AccountStart is called" do
24
- let(:fake_account) { double "Models::Account" }
23
+ let(:fake_account) { double "Norma43::Models::Account" }
25
24
  before do
26
- allow(Models::Account).to receive(:new) { fake_account }
25
+ allow(Norma43::Models::Account).to receive(:new) { fake_account }
27
26
  end
28
27
  let!(:subject) { AccountStart.call line, contexts }
29
28
 
@@ -1,15 +1,14 @@
1
- require "norma43/utils/contexts"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineProcessors
6
5
  RSpec.describe "AdditionalCurrency" do
7
- let(:line){ double "Line", attributes: {} }
8
- let(:transaction){ Models::Transaction.new }
9
- let(:contexts){ Contexts.new(
6
+ let(:line) { double "Line", attributes: {} }
7
+ let(:transaction) { Norma43::Models::Transaction.new }
8
+ let(:contexts) { Norma43::Utils::Contexts.new(
10
9
  [
11
- Models::Document.new ,
12
- Models::Account.new ,
10
+ Norma43::Models::Document.new,
11
+ Norma43::Models::Account.new,
13
12
  transaction
14
13
  ])
15
14
  }
@@ -1,15 +1,14 @@
1
- require "norma43/utils/contexts"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineProcessors
6
5
  RSpec.describe "AdditionalItems" do
7
- let(:line){ double "Line", attributes: {} }
8
- let(:transaction){ Models::Transaction.new }
9
- let(:contexts){ Contexts.new(
6
+ let(:line) { double "Line", attributes: {} }
7
+ let(:transaction) { Norma43::Models::Transaction.new }
8
+ let(:contexts) { Norma43::Utils::Contexts.new(
10
9
  [
11
- Models::Document.new ,
12
- Models::Account.new ,
10
+ Norma43::Models::Document.new,
11
+ Norma43::Models::Account.new,
13
12
  transaction
14
13
  ])
15
14
  }
@@ -1,5 +1,4 @@
1
- require "norma43/utils/contexts"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineProcessors
@@ -10,8 +9,8 @@ module Norma43
10
9
  let(:line) { double "Line", record_number: 35 }
11
10
 
12
11
  it "moves to the nearest document context" do
13
- document = Models::Document.new
14
- contexts = Contexts.new [Thing.new, document, Thing.new, Thing.new]
12
+ document = Norma43::Models::Document.new
13
+ contexts = Norma43::Utils::Contexts.new [Thing.new, document, Thing.new, Thing.new]
15
14
 
16
15
  DocumentEnd.call line, contexts
17
16
 
@@ -19,9 +18,9 @@ module Norma43
19
18
  end
20
19
 
21
20
  it "sets the number of lines on the document" do
22
- document = Models::Document.new
21
+ document = Norma43::Models::Document.new
23
22
 
24
- DocumentEnd.call line, Contexts.new(document)
23
+ DocumentEnd.call line, Norma43::Utils::Contexts.new(document)
25
24
 
26
25
  expect(document.number_of_lines).to eq 35
27
26
  end
@@ -1,5 +1,4 @@
1
- require "norma43/utils/contexts"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineProcessors
@@ -9,7 +8,7 @@ module Norma43
9
8
  it "instantiates a new document with the line attributes" do
10
9
  allow(Models::Document).to receive :new
11
10
 
12
- DocumentStart.call line, Contexts.new
11
+ DocumentStart.call line, Norma43::Utils::Contexts.new
13
12
 
14
13
  expect(Models::Document).to have_received(:new).with line.attributes
15
14
  end
@@ -18,7 +17,7 @@ module Norma43
18
17
  fake_document = double "Models::Document"
19
18
  allow(Models::Document).to receive(:new) { fake_document }
20
19
 
21
- contexts = DocumentStart.call line, Contexts.new
20
+ contexts = DocumentStart.call line, Norma43::Utils::Contexts.new
22
21
 
23
22
  expect(contexts.current).to be fake_document
24
23
  end
@@ -1,16 +1,15 @@
1
- require "norma43/utils/contexts"
2
- require "norma43/line_processors"
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Norma43
5
4
  module LineProcessors
6
5
  RSpec.describe "Transaction" do
7
- let(:line){ double "Line", attributes: {} }
8
- let(:account){ Models::Account.new }
9
- let(:contexts){ Contexts.new(
6
+ let(:line) { double "Line", attributes: {} }
7
+ let(:account) { Norma43::Models::Account.new }
8
+ let(:contexts) { Norma43::Utils::Contexts.new(
10
9
  [
11
- Models::Document.new ,
10
+ Norma43::Models::Document.new,
12
11
  account,
13
- Models::Transaction.new
12
+ Norma43::Models::Transaction.new
14
13
  ])
15
14
  }
16
15
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "norma43/parser"
2
4
 
3
5
  module Norma43
@@ -5,18 +7,17 @@ module Norma43
5
7
  describe "ensuring the file is valid" do
6
8
  it "accepts a valid file starting with document" do
7
9
  valid_file = "00TI222222 2011102504133012345601TL1Norma43 "
8
- expect{ Parser.new(valid_file) }.to_not raise_error
10
+ expect { Parser.new(valid_file) }.to_not raise_error
9
11
  end
10
12
 
11
13
  it "accepts a valid file starting with an account" do
12
-
13
14
  valid_file = "119999111101234567890408040409052000000001234569783MY ACCOUNT "
14
- expect{ Parser.new(valid_file) }.to_not raise_error
15
+ expect { Parser.new(valid_file) }.to_not raise_error
15
16
  end
16
17
 
17
18
  it "should start with 00 or 11" do
18
19
  invalid_file = "99TI222222 2011102504133012345601TL1Norma43 "
19
- expect{ Parser.new(invalid_file) }.to raise_error{ Norma43::InvalidFileFormatError }
20
+ expect { Parser.new(invalid_file) }.to raise_error { Norma43::InvalidFileFormatError }
20
21
  end
21
22
  end
22
23
  end