jyske_bank_record 0.0.2 → 0.0.3

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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -0
  3. data/lib/jyske_bank_record.rb +95 -16
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 467a5a01905f99c60469c9e831a61b81fd2cd529
4
- data.tar.gz: 720ededf62f83da2363e204d230baa96a3a4e395
3
+ metadata.gz: da2285b140349c32da2af5bee28ee2bc9caf90fa
4
+ data.tar.gz: bec76bdc3edb461d1fd1286eb6524de423becc45
5
5
  SHA512:
6
- metadata.gz: 5debcfc09d0ec94dfee0a0aa703f47ed42269b3ce3119bd7b822ba3e32562987fad438864730bf353f8e95fb91e54863b4fc737da51391201017fdbf887e8b2b
7
- data.tar.gz: 95714cba058b55cce6c245ef7ce4bc4cbfa261ad4ef0045410d649855073fa6bf237b31bed57479411c8837e10d7bb621b2c277df9866bbd630e5182141542db
6
+ metadata.gz: daa9fb4f47c7ca54a74d60217579b10e116f57907e504d85aee1521ee8a2b97e0a8278eed0bbeadfab06a9dd5dd450deed64f427dad91fa46fed003be5f5cea5
7
+ data.tar.gz: 6550254c585f9fea7e42856556b033428f0c2e140303db930fc0832e94274e69d1d85da80cd691d172c95e616f2767b606c7553c1151549163910ebfea798675
data/README.md CHANGED
@@ -6,6 +6,11 @@ ruby library for generating bank record files for Jyske Bank
6
6
 
7
7
  - Validate inputs (length, etc)
8
8
 
9
+ # RECORD FORMAT
10
+
11
+ - English: http://www.jyskebank.dk/wps/wcm/connect/274e6433-8d6f-4369-9c8a-ad86e1254c2a/Record+description_Accounts+and+payments.pdf?MOD=AJPERES
12
+ - Danish: http://www.jyskebank.dk/wps/wcm/connect/1ccdeb0d-9587-44cd-87bb-f5b245e2cceb/Recordbeskrivelser_indl%C3%A6s.pdf?MOD=AJPERES
13
+
9
14
  # LICENSE
10
15
 
11
16
  The MIT License (MIT)
@@ -1,3 +1,5 @@
1
+ require 'active_model'
2
+
1
3
  module JyskeBankRecord
2
4
  DATE_FORMAT = "%Y%m%d"
3
5
 
@@ -27,6 +29,18 @@ module JyskeBankRecord
27
29
  CHEQUE = '2'
28
30
  end
29
31
 
32
+ # Encode variables given binding and set as instance variables
33
+ module EncodeCp1252AndSet
34
+ # @param [Binding] _binding binding for getting value of instance variables
35
+ # @param [Enumerable] names parameter names to encode and set
36
+ def encode_and_set(_binding, names)
37
+ names.each do |name|
38
+ value = _binding.local_variable_get name
39
+ instance_variable_set ('@' + name.to_s).to_sym, value.to_s.encode!(Encoding::CP1252)
40
+ end
41
+ end
42
+ end
43
+
30
44
  NEWLINE = "\r\n"
31
45
 
32
46
  # Format a string of bank records, with correct encoding
@@ -69,12 +83,12 @@ module JyskeBankRecord
69
83
  PaymentStartRecord = Struct.new(:creation_date) do
70
84
  def fields
71
85
  [
72
- 'IB000000000000',
73
- creation_date.strftime(DATE_FORMAT),
74
- ' ' * 90,
75
- ' ' * 255,
76
- ' ' * 255,
77
- ' ' * 255,
86
+ 'IB000000000000',
87
+ creation_date.strftime(DATE_FORMAT),
88
+ ' ' * 90,
89
+ ' ' * 255,
90
+ ' ' * 255,
91
+ ' ' * 255,
78
92
  ]
79
93
  end
80
94
 
@@ -86,14 +100,14 @@ module JyskeBankRecord
86
100
  PaymentEndRecord = Struct.new(:creation_date, :transactions) do
87
101
  def fields
88
102
  [
89
- 'IB999999999999',
90
- creation_date.strftime(DATE_FORMAT),
91
- transactions.length.to_s.rjust(6, '0'),
92
- transactions.map(&:amount).reduce(:+).to_s.rjust(13, '0') + '+',
93
- ' ' * 64,
94
- ' ' * 255,
95
- ' ' * 255,
96
- ' ' * 255,
103
+ 'IB999999999999',
104
+ creation_date.strftime(DATE_FORMAT),
105
+ transactions.length.to_s.rjust(6, '0'),
106
+ transactions.map(&:amount).reduce(:+).to_s.rjust(13, '0') + '+',
107
+ ' ' * 64,
108
+ ' ' * 255,
109
+ ' ' * 255,
110
+ ' ' * 255,
97
111
  ]
98
112
  end
99
113
 
@@ -102,9 +116,74 @@ module JyskeBankRecord
102
116
  end
103
117
  end
104
118
 
105
- Recipient = Struct.new(:registration_number, :account_number, :name, :address, :address2, :zip_code, :city)
119
+ # Recipient = Struct.new(:registration_number, :account_number, :name, :address, :address2, :zip_code, :city)
120
+ class Recipient
121
+ include ActiveModel::Validations
122
+ include JyskeBankRecord::EncodeCp1252AndSet
123
+
124
+ @@parameters = [:registration_number, :account_number, :name, :address, :address2, :zip_code, :city]
125
+
126
+ attr_reader *@@parameters
127
+
128
+ validates_presence_of *@@parameters, {allow_blank: true, allow_nil: false}
129
+ validates_length_of :registration_number, is: 4
130
+ validates :account_number, presence: true, length: {is: 10}
131
+
132
+ validates_length_of :name, :address, :address2, :city, {maximum: 32}
133
+ validates_length_of :zip_code, maximum: 4
134
+
135
+ # @param [String] registration_number 4 characters
136
+ # @param [String] account_number 10 characters
137
+ # @param [String] name
138
+ # @param [String] address
139
+ # @param [String] address2
140
+ # @param [String] zip_code
141
+ # @param [String] city
142
+ def initialize(registration_number, account_number, name: '', address: '', address2: '', zip_code: '', city: '')
143
+
144
+ # Encode and save parameters
145
+ encode_and_set binding, @@parameters
146
+ end
147
+ end
148
+
149
+ class PaymentRecord
150
+ include ActiveModel::Validations
151
+ include JyskeBankRecord::EncodeCp1252AndSet
152
+
153
+ @@parameters = [:sender_account_number, :entry_text, :reference, :notice]
154
+
155
+ attr_reader *@@parameters
156
+ attr_reader :processing_date, :amount, :recipient
157
+
158
+ validates_presence_of *@@parameters, {allow_blank: true, allow_nil: false}
159
+ validates_each :processing_date do |record, attr, value|
160
+ record.errors.add(attr, 'must respond to strftime') unless value.respond_to? :strftime
161
+ end
162
+ validates_numericality_of :amount
163
+ validates_each :amount do |record, attr, value|
164
+ record.errors.add(attr, 'must be a Fixnum') unless value.instance_of? Fixnum
165
+ end
166
+ validates_length_of :sender_account_number, is: 15
167
+ validates_length_of :entry_text, :reference, maximum: 35
168
+
169
+ # Validate notice for 9 lines, each less than or equal to 35 chars
170
+ validates_each :notice do |record, attr, value|
171
+ lines = value.lines
172
+ record.errors.add(attr, 'must have a maximum of 9 lines') if lines.length > 9
173
+ lines.each_with_index do |line, i|
174
+ if line.length > 35
175
+ record.errors.add(attr, "must have lines at most 35 (cp1252) chars of length, line #{i+1} is #{line.length} chars long")
176
+ end
177
+ end
178
+ end
179
+
180
+ def initialize(processing_date, amount, sender_account_number, recipient, entry_text: '', reference: '', notice: '')
181
+ encode_and_set binding, @@parameters
182
+ @processing_date = processing_date
183
+ @amount = amount
184
+ @recipient = recipient
185
+ end
106
186
 
107
- PaymentRecord = Struct.new(:processing_date, :amount, :sender_account_number, :recipient, :entry_text, :reference, :notice) do
108
187
  def fields
109
188
  start_fields = [
110
189
  Type::DOMESTIC_RECORD,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jyske_bank_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas B Homburg