nacha 0.1.0

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 (84) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.gitlab-ci.yml +13 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +42 -0
  6. data/.ruby-gemset +1 -0
  7. data/.ruby-version +1 -0
  8. data/.travis.yml +5 -0
  9. data/Gemfile +9 -0
  10. data/Guardfile +20 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +75 -0
  13. data/ROADMAP.md +56 -0
  14. data/Rakefile +6 -0
  15. data/bin/console +14 -0
  16. data/bin/setup +8 -0
  17. data/lib/nacha/aba_number.rb +44 -0
  18. data/lib/nacha/ach_date.rb +24 -0
  19. data/lib/nacha/field.rb +139 -0
  20. data/lib/nacha/numeric.rb +68 -0
  21. data/lib/nacha/parser.rb +67 -0
  22. data/lib/nacha/record/addenda_record_type.rb +29 -0
  23. data/lib/nacha/record/adv_batch_control.rb +21 -0
  24. data/lib/nacha/record/adv_entry_detail.rb +27 -0
  25. data/lib/nacha/record/adv_file_control.rb +24 -0
  26. data/lib/nacha/record/adv_file_header.rb +26 -0
  27. data/lib/nacha/record/arc_entry_detail.rb +24 -0
  28. data/lib/nacha/record/base.rb +170 -0
  29. data/lib/nacha/record/batch_control.rb +24 -0
  30. data/lib/nacha/record/batch_control_record_type.rb +21 -0
  31. data/lib/nacha/record/batch_header.rb +26 -0
  32. data/lib/nacha/record/batch_header_record_type.rb +25 -0
  33. data/lib/nacha/record/boc_entry_detail.rb +23 -0
  34. data/lib/nacha/record/ccd_addenda.rb +19 -0
  35. data/lib/nacha/record/ccd_entry_detail.rb +23 -0
  36. data/lib/nacha/record/cie_addenda.rb +19 -0
  37. data/lib/nacha/record/cie_entry_detail.rb +23 -0
  38. data/lib/nacha/record/ctx_addenda.rb +17 -0
  39. data/lib/nacha/record/ctx_corporate_entry_detail.rb +26 -0
  40. data/lib/nacha/record/detail_record_type.rb +29 -0
  41. data/lib/nacha/record/dne_addenda.rb +19 -0
  42. data/lib/nacha/record/dne_entry_detail.rb +24 -0
  43. data/lib/nacha/record/enr_addenda.rb +19 -0
  44. data/lib/nacha/record/enr_entry_detail.rb +26 -0
  45. data/lib/nacha/record/fifth_iat_addenda.rb +19 -0
  46. data/lib/nacha/record/file_control.rb +21 -0
  47. data/lib/nacha/record/file_control_record_type.rb +21 -0
  48. data/lib/nacha/record/file_header.rb +26 -0
  49. data/lib/nacha/record/file_header_record_type.rb +31 -0
  50. data/lib/nacha/record/filler.rb +13 -0
  51. data/lib/nacha/record/filler_record_type.rb +21 -0
  52. data/lib/nacha/record/first_iat_addenda.rb +22 -0
  53. data/lib/nacha/record/fourth_iat_addenda.rb +22 -0
  54. data/lib/nacha/record/iat_batch_header.rb +30 -0
  55. data/lib/nacha/record/iat_entry_detail.rb +26 -0
  56. data/lib/nacha/record/iat_foreign_coorespondent_bank_information_addenda.rb +23 -0
  57. data/lib/nacha/record/iat_remittance_information_addenda.rb +19 -0
  58. data/lib/nacha/record/mte_addenda.rb +26 -0
  59. data/lib/nacha/record/mte_entry_detail.rb +24 -0
  60. data/lib/nacha/record/pop_entry_detail.rb +26 -0
  61. data/lib/nacha/record/pos_addenda.rb +25 -0
  62. data/lib/nacha/record/pos_entry_detail.rb +24 -0
  63. data/lib/nacha/record/ppd_addenda.rb +19 -0
  64. data/lib/nacha/record/ppd_entry_detail.rb +24 -0
  65. data/lib/nacha/record/rck_entry_detail.rb +24 -0
  66. data/lib/nacha/record/second_iat_addenda.rb +20 -0
  67. data/lib/nacha/record/seventh_iat_addenda.rb +20 -0
  68. data/lib/nacha/record/shr_addenda.rb +26 -0
  69. data/lib/nacha/record/shr_entry_detail.rb +25 -0
  70. data/lib/nacha/record/sixth_iat_addenda.rb +20 -0
  71. data/lib/nacha/record/tel_entry_detail.rb +24 -0
  72. data/lib/nacha/record/third_iat_addenda.rb +20 -0
  73. data/lib/nacha/record/trc_entry_detail.rb +25 -0
  74. data/lib/nacha/record/trx_addenda.rb +19 -0
  75. data/lib/nacha/record/trx_entry_detail.rb +26 -0
  76. data/lib/nacha/record/validations/field_validations.rb +33 -0
  77. data/lib/nacha/record/validations/record_validations.rb +15 -0
  78. data/lib/nacha/record/web_addenda.rb +19 -0
  79. data/lib/nacha/record/web_entry_detail.rb +24 -0
  80. data/lib/nacha/record/xck_entry_detail.rb +25 -0
  81. data/lib/nacha/version.rb +5 -0
  82. data/lib/nacha.rb +45 -0
  83. data/nacha.gemspec +38 -0
  84. metadata +304 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1411753b1afc7c81a9e3875ec1541986d8b461afd5d0daae835879d17776ea7c
4
+ data.tar.gz: 38fbcd4d7e222177dfc8bdce195991d13716ae669ca4649a7cf4e3428ab646d0
5
+ SHA512:
6
+ metadata.gz: 34e0fe3b791e0a47f9bef29898815b5d7fccac762ec21cbf227e3896a6dc8933d0a68349ff4cd661a65bff4f886b68fd22a31236dfdc6984d21c27b83d12d295
7
+ data.tar.gz: 2f53f2b8ed728dc2778a1c78461fefb8dd22ddd8fe5d8ccefad1deff50931924e11af55b4a5edcbe1880455de7019b3434a0a42ceff8c12f99a3d8bdc75ac14e
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .byebug_history
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,13 @@
1
+ image: "ruby:2.5"
2
+
3
+ before_script:
4
+ - apt-get update -qq && apt-get install -y -qq sqlite3 libsqlite3-dev
5
+ - ruby -v
6
+ - which ruby
7
+ - gem install bundler --no-document
8
+ - bundle install --jobs $(nproc) "${FLAGS[@]}"
9
+
10
+ rspec:
11
+ script:
12
+ - bundle exec rspec
13
+ - bundle exec rubocop
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,42 @@
1
+ inherit_gem:
2
+ gitlab-styles:
3
+ - rubocop-default.yml
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.5
7
+ Exclude:
8
+ - 'tmp/**/*'
9
+ - 'bin/**/*'
10
+ CacheRootDirectory: tmp
11
+
12
+ # This cop checks whether some constant value isn't a
13
+ # mutable literal (e.g. array or hash).
14
+ Style/MutableConstant:
15
+ Enabled: true
16
+
17
+ Style/SafeNavigation:
18
+ Enabled: false
19
+
20
+ # Frozen String Literal
21
+ Style/FrozenStringLiteralComment:
22
+ Enabled: true
23
+ Exclude:
24
+ - 'Gemfile'
25
+ - 'Rakefile'
26
+ - 'spec/**/*'
27
+
28
+ Metrics/LineLength:
29
+ Max: 92
30
+
31
+ Naming/FileName:
32
+ ExpectMatchingDefinition: true
33
+ Exclude:
34
+ - 'spec/**/*'
35
+
36
+ IgnoreExecutableScripts: true
37
+
38
+ Layout/LeadingCommentSpace:
39
+ Enabled: false
40
+
41
+ Style/Encoding:
42
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ nacha
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nacha.gemspec
4
+ gemspec
5
+
6
+ gem 'pry-byebug', group: %i[development test]
7
+ gem 'pry-highlight', group: %i[development test]
8
+ gem 'pry-remote', group: %i[development test]
9
+ gem 'pry-stack_explorer', group: %i[development test]
data/Guardfile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # Feel free to open issues for suggestions and improvements
8
+
9
+ # RSpec files
10
+ rspec = dsl.rspec
11
+ watch(rspec.spec_helper) { rspec.spec_dir }
12
+ watch(rspec.spec_support) { rspec.spec_dir }
13
+ watch(rspec.spec_files)
14
+ watch(%r{^lib/nacha/base_record\.rb}) { Dir.glob('spec/nacha/record_types/*_spec.rb') }
15
+ watch(%r{^lib/config/definitions/(.+)\.yml}) { |m| "spec/nacha/record_types/#{m[1]}_spec.rb" }
16
+
17
+ # Ruby files
18
+ ruby = dsl.ruby
19
+ dsl.watch_spec_files_for(ruby.lib_files)
20
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 David H. Wilkins
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Nacha
2
+
3
+ Validating Ruby ACH parser and generator
4
+
5
+ Format documentation here: http://achrulesonline.org/
6
+
7
+ The definition of the records exactly mirrors the NACHA documentation so that
8
+ development and business can use the same terminology.
9
+
10
+ Work in progress - contributors welcome.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'nacha'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install nacha
27
+
28
+ ## Usage
29
+
30
+ API may change at any time. Pull requests welcomed
31
+
32
+
33
+ `"101 124000054 1240000540907021214A094101ZIONS FIRST NATIONAL BAZIONS FIRST NATIONAL BA 1"`
34
+
35
+ ```ruby
36
+ ach_records = Nacha.parse(ach_file)
37
+
38
+ ach_records[0].class # => Nacha::Record::FileHeaderRecord
39
+
40
+ ach_records[0].to_json
41
+ ```
42
+ ```json
43
+ {
44
+ "record_type_code": "1",
45
+ "priority_code": 1,
46
+ "immediate_destination": "124000054",
47
+ "immediate_origin": "124000054",
48
+ "file_creation_date": "2009-07-02",
49
+ "file_creation_time": "1214",
50
+ "file_id_modifier": "A",
51
+ "record_size": "094",
52
+ "blocking_factor": "10",
53
+ "format_code": "1",
54
+ "immediate_destination_name": "ZIONS FIRST NATIONAL BA",
55
+ "immediate_origin_name": "ZIONS FIRST NATIONAL BA",
56
+ "reference_code": " 1"
57
+ }
58
+ ```
59
+
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dwilkins/nacha.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
data/ROADMAP.md ADDED
@@ -0,0 +1,56 @@
1
+ - [x] Define All Record Types
2
+ - [x] AdvBatchControlRecord
3
+ - [x] AdvEntryDetailRecord
4
+ - [x] AdvFileControlRecord
5
+ - [x] AdvFileHeaderRecord
6
+ - [x] ArcEntryDetailRecord
7
+ - [x] BatchControlRecord
8
+ - [x] BatchHeaderRecord
9
+ - [x] BocEntryDetailRecord
10
+ - [x] CcdAddendaRecord
11
+ - [x] CcdEntryDetailRecord
12
+ - [x] CieAddendaRecord
13
+ - [x] CieEntryDetailRecord
14
+ - [x] CtxAddendaRecord
15
+ - [x] CtxCorporateEntryDetailRecord
16
+ - [x] DneAddendaRecord
17
+ - [x] DneEntryDetailRecord
18
+ - [x] EnrAddendaRecord
19
+ - [x] EnrEntryDetailRecord
20
+ - [x] FileControlRecord
21
+ - [x] FileHeaderRecord
22
+ - [x] IatBatchHeaderRecord
23
+ - [x] IatEntryDetailRecord
24
+ - [x] FirstIatAddendaRecord
25
+ - [x] SecondIatAddendaRecord
26
+ - [x] ThirdIatAddendaRecord
27
+ - [x] FourthIatAddendaRecord
28
+ - [x] FifthIatAddendaRecord
29
+ - [x] SixthIatAddendaRecord
30
+ - [x] SeventhIatAddendaRecord
31
+ - [x] IatForeignCoorespondentBankInformationAddendaRecord
32
+ - [x] IatRemittanceInformationAddendaRecord
33
+ - [x] IatBatchHeaderRecord
34
+ - [x] MteAddendaRecord
35
+ - [x] MteEntryDetailRecord
36
+ - [x] PopEntryDetailRecord
37
+ - [x] PosAddendaRecord
38
+ - [x] PosEntryDetailRecord
39
+ - [x] PpdAddendaRecord
40
+ - [x] PpdEntryDetailRecord
41
+ - [x] RckEntryDetailRecord
42
+ - [x] ShrAddendaRecord
43
+ - [x] ShrEntryDetailRecord
44
+ - [x] TelEntryDetailRecord
45
+ - [x] TrcEntryDetailRecord
46
+ - [x] TrxAddendaRecord
47
+ - [x] TrxEntryDetailRecord
48
+ - [x] WebAddendaRecord
49
+ - [x] WebEntryDetailRecord
50
+ - [x] XckEntryDetailRecord
51
+ - [ ] Debug / Inspection
52
+ - [ ] Field Validations
53
+ - [ ] Record Validations
54
+ - [ ] Implement record order logic
55
+ - [ ] Establish child record types
56
+ - [ ] Establish possible next record type
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nacha"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Nacha::AbaNumber
4
+ attr_reader :routing_number
5
+ attr_reader :aba_number
6
+
7
+ def initialize(routing_number)
8
+ self.routing_number = routing_number
9
+ end
10
+
11
+ def compute_check_digit
12
+ n = @routing_number.to_s.strip.ljust(8, '0').chars.collect(&:to_i)
13
+ sum = (3 * (n[0] + n[3] + n[6])) +
14
+ (7 * (n[1] + n[4] + n[7])) +
15
+ (n[2] + n[5])
16
+ intermediate = (sum % 10)
17
+ intermediate.zero? ? '0' : (10 - intermediate).to_s
18
+ end
19
+
20
+ def routing_number=(val)
21
+ @valid = nil
22
+ @routing_number = val.strip
23
+ end
24
+
25
+ def add_check_digit
26
+ @routing_number = @routing_number[0..7] + compute_check_digit
27
+ end
28
+
29
+ def check_digit
30
+ @routing_number.chars[8] if @routing_number.length == 9 && compute_check_digit == @routing_number.chars[8]
31
+ end
32
+
33
+ def valid?
34
+ @valid ||= (@routing_number.length == 9 && compute_check_digit == @routing_number.chars[8])
35
+ end
36
+
37
+ def to_s(with_checkdigit = true)
38
+ if with_checkdigit
39
+ @routing_number
40
+ else
41
+ @routing_number[0..7]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ # A Date object to handle some of the Ach formatted dates
5
+
6
+ class Nacha::AchDate < Date
7
+ def self.new(date_str)
8
+ if date_str.is_a? String
9
+ strptime(date_str, '%y%m%d')
10
+ elsif date_str.is_a? Date
11
+ date_str.dup
12
+ else
13
+ super(date_str)
14
+ end
15
+ end
16
+
17
+ def to_s
18
+ strftime('%y%m%d')
19
+ end
20
+
21
+ def valid?
22
+ !nil?
23
+ end
24
+ end
@@ -0,0 +1,139 @@
1
+ # coding: utf-8
2
+ require 'pry'
3
+ require 'bigdecimal'
4
+ require 'nacha/numeric'
5
+ require 'nacha/aba_number'
6
+ require 'nacha/ach_date'
7
+
8
+ class Nacha::Field
9
+
10
+ attr_accessor :inclusion, :contents, :position
11
+ attr_accessor :data, :name, :errors
12
+ attr_reader :input_data
13
+ attr_reader :data_type
14
+ attr_reader :validator
15
+ attr_reader :justification
16
+ attr_reader :fill_character
17
+ attr_reader :output_conversion
18
+ attr_reader :json_output
19
+
20
+ def initialize opts = {}
21
+ @data_type = String
22
+ @errors = []
23
+ @name = 'Unknown'.freeze
24
+ @justification = :ljust
25
+ @fill_character = ' '.freeze
26
+ @json_output = [:to_s]
27
+ @output_conversion = [:to_s]
28
+ opts.each do |k,v|
29
+ setter = "#{k}="
30
+ if(respond_to?(setter))
31
+ send(setter, v) unless v.nil?
32
+ end
33
+ end
34
+ end
35
+
36
+ # CXXX Constant
37
+ #
38
+ def contents= val
39
+ @contents = val
40
+ case @contents
41
+ when /\AC(.*)\z/ # Constant
42
+ @data = $1
43
+ when /Numeric/
44
+ @data_type = Nacha::Numeric
45
+ @justification = :rjust
46
+ @json_output = [[:to_i]]
47
+ @output_conversion = [:to_i]
48
+ @fill_character = '0'.freeze
49
+ when /.?TTTTAAAAC/
50
+ @data_type = Nacha::AbaNumber
51
+ @validator = :valid?
52
+ @justification = :rjust
53
+ @output_conversion = [:to_s]
54
+ @fill_character = ' '.freeze
55
+ when 'YYMMDD'
56
+ @data_type = Nacha::AchDate
57
+ @justification = :rjust
58
+ @validator = :valid?
59
+ @json_output = [[:iso8601]]
60
+ @output_conversion = [:to_s]
61
+ @fill_character = ' '.freeze
62
+ when 'Alphameric'
63
+ @data_type = String
64
+ @justification = :ljust
65
+ @output_conversion = [:to_s]
66
+ @fill_character = ' '.freeze
67
+ when /\$+\¢\¢/
68
+ @data_type = Nacha::Numeric
69
+ @justification = :rjust
70
+ @json_output = [[:to_i],[:/,100.0]]
71
+ @output_conversion = [:to_i]
72
+ @fill_character = '0'.freeze
73
+ end
74
+ end
75
+
76
+ def data= val
77
+ @data = @data_type.new(val)
78
+ @input_data = val
79
+ end
80
+
81
+ def mandatory?
82
+ @inclusion == 'M'.freeze
83
+ end
84
+
85
+ def required?
86
+ @inclusion == 'R'.freeze
87
+ end
88
+
89
+ def optional?
90
+ @inclusion == 'O'.freeze
91
+ end
92
+
93
+ def valid?
94
+ @valid = inclusion && contents && position
95
+ if(@validator && @data)
96
+ @valid &&= @data.send(@validator)
97
+ end
98
+ @valid
99
+ end
100
+
101
+ def add_error err_string
102
+ errors << err_string
103
+ end
104
+
105
+ def self.unpack_str(definition = { })
106
+ if(definition[:contents] =~ /(Numeric|\$+\¢\¢)/)
107
+ 'a'.freeze
108
+ else
109
+ 'A'.freeze
110
+ end + definition[:position].size.to_s
111
+ end
112
+
113
+ def to_ach
114
+ str = to_s
115
+ fill_char = @fill_character
116
+ fill_char = ' '.freeze unless str
117
+ str ||= ''.freeze
118
+ str.send(justification,position.count, fill_char)
119
+ end
120
+
121
+ def to_json_output
122
+ if(@json_output)
123
+ @json_output.reduce(@data) { |output, operation|
124
+ output = output.send(*operation) if output
125
+ }
126
+ else
127
+ to_s
128
+ end
129
+ end
130
+
131
+ def to_s
132
+ @data.send(*output_conversion).to_s
133
+ end
134
+
135
+ def raw
136
+ @data
137
+ end
138
+
139
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+ class Nacha::Numeric
3
+ attr_accessor :value
4
+ def initialize(val = nil)
5
+ self.value = val
6
+ end
7
+
8
+ def to_i
9
+ if @value
10
+ if @value.is_a?(String) && @value.match(/ */)
11
+ self
12
+ else
13
+ @value.to_i
14
+ end
15
+ else
16
+ self
17
+ end
18
+ end
19
+
20
+ def value=(val)
21
+ if val.is_a?(String)
22
+ @value = val.dup
23
+ if !val.strip.empty?
24
+ @value = BigDecimal(val.strip)
25
+ @op_value = @value
26
+ else
27
+ @op_value = BigDecimal(0)
28
+ end
29
+ else
30
+ @value = BigDecimal(val)
31
+ @op_value = @value
32
+ end
33
+ @value
34
+ end
35
+
36
+ def to_s
37
+ @value ? @value.to_s : nil
38
+ end
39
+
40
+ def respond_to?(method_name, include_private = false)
41
+ puts "In respond_to? #{method_name}"
42
+ @op_value.respond_to? method_name
43
+ end
44
+
45
+ # @op_value is the value for operations. @value may still be a string
46
+ # from the input data. If an operation is attempted, then @op_value
47
+ # should be checked to see if the operation is valid for it, not
48
+ # necessarily the potentially string @value
49
+ def method_missing(method_name, *args, &block)
50
+ puts "In method_missing for #{method_name} value_class = #{@value.class}, op_value_class = #{@op_value.class}"
51
+ if @op_value.respond_to? method_name
52
+ old_op_value = @op_value.dup
53
+ if /!\z/.match?(method_name.to_s)
54
+ @op_value.send(method_name, *args, &block)
55
+ return_value = @op_value
56
+ else
57
+ return_value = @op_value.send(method_name, *args, &block)
58
+ end
59
+ if old_op_value != return_value
60
+ @value = return_value
61
+ @op_value = return_value
62
+ end
63
+ @value
64
+ else
65
+ super
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nacha'
4
+
5
+ class Nacha::Parser
6
+ DEFAULT_RECORD_TYPES = ['Nacha::Record::FileHeader'].freeze
7
+ def initialize
8
+ reset!
9
+ end
10
+
11
+ def reset!; end
12
+
13
+ def parse_file(file)
14
+ parent = nil
15
+ records = []
16
+ File.foreach(file).with_index do |line, line_num|
17
+ records << process(line, line_num, records.last)
18
+ parent = records.last if records.lasts.class.child_record_types.any?
19
+ end
20
+ end
21
+
22
+ def parse_string(str)
23
+ line_num = -1
24
+ records = []
25
+ str.scan(/.{94}/).each do |line|
26
+ line_num += 1
27
+ records << process(line, line_num, records.last)
28
+ end.compact
29
+ records
30
+ end
31
+
32
+ def process(line, line_num, previous = nil)
33
+ parent = previous
34
+ record = nil
35
+
36
+ record_types = valid_record_types(parent)
37
+ while record_types
38
+ record = parse_by_types(line, record_types)
39
+ break if record || !parent
40
+
41
+ parent = parent.parent
42
+ record_types = valid_record_types(parent)
43
+ end
44
+ add_child(parent, record)
45
+ record
46
+ end
47
+
48
+ def valid_record_types(parent)
49
+ return DEFAULT_RECORD_TYPES unless parent && parent.respond_to?(:child_record_types)
50
+
51
+ parent.child_record_types
52
+ end
53
+
54
+ def add_child(parent, child)
55
+ return unless parent && child
56
+
57
+ parent.children << child
58
+ child.parent = parent
59
+ end
60
+
61
+ def parse_by_types(line, record_types)
62
+ record_types.detect do |rt|
63
+ record_type = Object.const_get(rt)
64
+ return record_type.parse(line) if record_type.matcher =~ line
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nacha
4
+ module Record
5
+ module AddendaRecordType
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def child_record_types
12
+ []
13
+ end
14
+
15
+ def self.next_record_types
16
+ []
17
+ end
18
+ end
19
+
20
+ def child_record_types
21
+ self.class.child_record_types
22
+ end
23
+
24
+ def next_record_types
25
+ self.class.next_record_types
26
+ end
27
+ end
28
+ end
29
+ end