Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit ac725c6

Browse files
Merge pull request #5 from zeptofs/zi/ZEP-1944-fixy-gem
Add header record using Fixy and DryRB Validation gems
2 parents b26eab0 + 7fd5cd1 commit ac725c6

File tree

13 files changed

+246
-12
lines changed

13 files changed

+246
-12
lines changed

.rubocop.yml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@ require:
33

44
AllCops:
55
NewCops: enable
6-
TargetRubyVersion: 2.6
6+
TargetRubyVersion: 2.7
7+
8+
Layout/LineLength:
9+
Max: 120
10+
11+
Lint/MissingSuper:
12+
Enabled: false
13+
14+
Metrics/BlockLength:
15+
Enabled: false
16+
17+
Style/Documentation:
18+
Enabled: false
719

820
Style/StringLiterals:
921
Enabled: true
1022
EnforcedStyle: double_quotes
1123

12-
Style/StringLiteralsInInterpolation:
13-
Enabled: true
14-
EnforcedStyle: double_quotes
24+
RSpec/ExampleLength:
25+
Enabled: false
1526

16-
Layout/LineLength:
17-
Max: 120
27+
RSpec/MultipleExpectations:
28+
Enabled: false

lib/mt9.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
# frozen_string_literal: true
22

3-
require_relative "mt9/version"
3+
require "dry-validation"
4+
require "fixy"
5+
6+
require "mt9/values"
7+
require "mt9/validators/base_contract"
8+
require "mt9/validators/header_record_contract"
9+
require "mt9/base_record"
10+
require "mt9/header_record"
11+
require "mt9/version"
412

513
module MT9
6-
class Error < StandardError; end
7-
# Your code goes here...
14+
class ValidationError < StandardError
15+
attr_reader :result
16+
17+
def initialize(result)
18+
@result = result
19+
errors = result.errors(full: true).messages
20+
super("Validation failed: #{errors.join(', ')}")
21+
end
22+
end
823
end

lib/mt9/base_record.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module MT9
4+
class BaseRecord < Fixy::Record
5+
include Fixy::Formatter::Alphanumeric
6+
7+
SPACE = " "
8+
9+
set_record_length 160
10+
end
11+
end

lib/mt9/header_record.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module MT9
4+
class HeaderRecord < BaseRecord
5+
set_line_ending Fixy::Record::LINE_ENDING_CRLF
6+
7+
attr_reader :file_type, :account_number, :due_date, :client_short_name
8+
9+
field :file_type, 2, "1-2", :alphanumeric
10+
field :account_number, 16, "3-18", :alphanumeric
11+
field :due_date, 8, "19-26", :alphanumeric
12+
field :filler5, 5, "27-31", :alphanumeric
13+
field :client_short_name, 20, "32-51", :alphanumeric
14+
field :filler109, 109, "52-160", :alphanumeric
15+
16+
field_value :filler5, SPACE * 5
17+
field_value :filler109, SPACE * 109
18+
19+
def initialize(...)
20+
validator = Validators::HeaderRecordContract.new
21+
result = validator.call(...)
22+
raise MT9::ValidationError, result unless result.success?
23+
24+
result.to_h.each do |key, value|
25+
instance_variable_set("@#{key}", value)
26+
end
27+
end
28+
end
29+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module MT9
4+
module Validators
5+
class BaseContract < Dry::Validation::Contract
6+
register_macro(:is_account_number?) do
7+
key.failure("must be 15 or 16 numeric characters") unless /^(\d{15}|\d{16})$/.match(value)
8+
key.failure("must not start with a reserved bank code") if value.start_with?(*MT9::Values::RESERVED_BANK_CODES)
9+
end
10+
end
11+
end
12+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module MT9
4+
module Validators
5+
class HeaderRecordContract < BaseContract
6+
schema do
7+
required(:file_type).filled(:string, included_in?: MT9::Values::FILE_TYPES)
8+
required(:account_number).filled(:string)
9+
required(:due_date).filled(:string)
10+
optional(:client_short_name).value(:string, size?: 0..20)
11+
end
12+
13+
rule(:account_number).validate(:is_account_number?)
14+
15+
rule(:due_date) do
16+
key.failure("must be 6 or 8 numeric characters") unless /^(\d{6}|\d{8})$/.match(value)
17+
end
18+
end
19+
end
20+
end

lib/mt9/values.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module MT9
4+
module Values
5+
RESERVED_BANK_CODES = %w[99].freeze
6+
7+
DIRECT_CREDIT = "12"
8+
DIRECT_DEBIT = "20"
9+
FILE_TYPES = [DIRECT_CREDIT, DIRECT_DEBIT].freeze
10+
end
11+
end

mt9.gemspec

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
1212
"MT9 is a data standard used within the NZ banking industry for the creation of Bulk Payments and Receipts."
1313
spec.homepage = "https://github.com/zeptofs/mt9"
1414
spec.license = "MIT"
15-
spec.required_ruby_version = ">= 2.6.0"
15+
spec.required_ruby_version = ">= 2.7.0"
1616

1717
# Specify which files should be added to the gem when it is released.
1818
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -25,9 +25,10 @@ Gem::Specification.new do |spec|
2525
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
2626
spec.require_paths = ["lib"]
2727

28-
# Uncomment to register a new dependency of your gem
29-
# spec.add_dependency "example-gem", "~> 1.0"
28+
spec.add_dependency "dry-validation", "~> 1.8.0"
29+
spec.add_dependency "fixy", "~> 0.0.9"
3030

31+
spec.add_development_dependency "pry-byebug", "~> 3.9.0"
3132
# For more information and examples about making a new gem, check out our
3233
# guide at: https://bundler.io/guides/creating_gem.html
3334
spec.metadata["rubygems_mfa_required"] = "true"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
12123113000631400 071020 asbd 2
2+
13123113000214500 0510000000102PayeeName 0 000000000000Payeecde0 PayeeRef0 PayeePrt0 PayerName Payercde PayerRef PayerPrt
3+
13123113000214500 0510000000201PayeeName 1 000000000000Payeecde1 PayeeRef1 PayeePrt1 PayerName Payercde PayerRef PayerPrt
4+
139962260004290 303
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
20123113000214500 031121 test co
2+
13123009000012300 0000000000100PayerName 0 000000000000Payercde0 PayerRef0 PayerPrt0 PayeeName Payeecde PayeeRef PayeePrt
3+
13123009000012300 0000000000200PayerName 1 000000000000Payercde1 PayerRef1 PayerPrt1 PayeeName Payeecde PayeeRef PayeePrt
4+
13123009000012300 0000000000300PayerName 2 000000000000Payercde2 PayerRef2 PayerPrt2 PayeeName Payeecde PayeeRef PayeePrt
5+
139990270000369 600

0 commit comments

Comments
 (0)