ibandit 0.8.1 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,150 +0,0 @@
1
- module Ibandit
2
- class SwedishDetailsConverter
3
- def initialize(branch_code: nil, account_number: nil)
4
- @branch_code = branch_code
5
- @account_number = account_number
6
- end
7
-
8
- def convert
9
- if bank_info.nil?
10
- return { swift_bank_code: nil,
11
- swift_account_number: cleaned_account_number.rjust(17, '0') }
12
- end
13
-
14
- {
15
- account_number: serial_number,
16
- branch_code: clearing_code,
17
- swift_bank_code: bank_info.fetch(:bank_code).to_s,
18
- swift_account_number: swift_account_number
19
- }
20
- end
21
-
22
- private
23
-
24
- def cleaned_account_number
25
- # Don't trim leading zeroes if the account number we are given is a
26
- # serial number (i.e. if the clearing code is separate).
27
- @cleaned_account_number ||= if @branch_code.nil?
28
- clean_account_number(@account_number)
29
- else
30
- remove_bad_chars(@account_number)
31
- end
32
- end
33
-
34
- def cleaned_branch_code
35
- @cleaned_branch_code ||= remove_bad_chars(@branch_code)
36
- end
37
-
38
- def remove_bad_chars(number)
39
- return if number.nil?
40
- number.gsub(/[-.\s]/, '')
41
- end
42
-
43
- def clean_account_number(number)
44
- return if number.nil?
45
- remove_bad_chars(number).gsub(/\A0+/, '')
46
- end
47
-
48
- def bank_info
49
- @bank_info ||= self.class.bank_info_for(bank_info_key)
50
- end
51
-
52
- def bank_info_key
53
- (cleaned_branch_code || cleaned_account_number).slice(0, 4)
54
- end
55
-
56
- def clearing_code_length
57
- bank_info.fetch(:clearing_code_length)
58
- end
59
-
60
- def serial_number_length
61
- bank_info.fetch(:serial_number_length)
62
- end
63
-
64
- def clearing_code
65
- cleaned_branch_code ||
66
- cleaned_account_number.slice(0, clearing_code_length)
67
- end
68
-
69
- def serial_number
70
- serial_number = if @branch_code.nil?
71
- cleaned_account_number[clearing_code_length..-1]
72
- else
73
- cleaned_account_number
74
- end
75
-
76
- return serial_number unless bank_info.fetch(:zerofill_serial_number)
77
-
78
- serial_number.rjust(serial_number_length, '0')
79
- end
80
-
81
- def swift_account_number
82
- if bank_info.fetch(:include_clearing_code)
83
- (clearing_code + serial_number).rjust(17, '0')
84
- else
85
- serial_number.rjust(17, '0')
86
- end
87
- end
88
-
89
- def self.valid_bank_code?(bank_code: nil, account_number: nil)
90
- possible_bank_infos = possible_bank_info_for(
91
- bank_code: bank_code, account_number: account_number
92
- )
93
-
94
- possible_bank_infos.any?
95
- end
96
-
97
- def self.valid_length?(bank_code: nil, account_number: nil)
98
- return unless valid_bank_code?(
99
- bank_code: bank_code, account_number: account_number
100
- )
101
-
102
- cleaned_account_number = account_number.gsub(/\A0+/, '')
103
-
104
- possible_bank_infos = possible_bank_info_for(
105
- bank_code: bank_code, account_number: account_number
106
- )
107
-
108
- possible_bank_infos.any? do |bank|
109
- length = bank.fetch(:serial_number_length)
110
- length += bank[:clearing_code_length] if bank[:include_clearing_code]
111
-
112
- if bank[:zerofill_serial_number] && !bank[:include_clearing_code]
113
- serial_number_length = bank.fetch(:serial_number_length)
114
- cleaned_account_number =
115
- cleaned_account_number.rjust(serial_number_length, '0')
116
- end
117
-
118
- cleaned_account_number.length == length
119
- end
120
- end
121
-
122
- def self.bank_info_for(clearing_code)
123
- bank_info_table.find { |bank| bank[:range].include?(clearing_code.to_i) }
124
- end
125
-
126
- def self.possible_bank_info_for(bank_code: nil, account_number: nil)
127
- clearing_number = account_number.gsub(/\A0+/, '').slice(0, 4).to_i
128
-
129
- possible_bank_infos = bank_info_table.select do |bank|
130
- bank.fetch(:bank_code).to_s == bank_code
131
- end
132
-
133
- possible_bank_infos.select do |bank|
134
- !bank[:include_clearing_code] || bank[:range].include?(clearing_number)
135
- end
136
- end
137
- private_class_method :possible_bank_info_for
138
-
139
- def self.bank_info_table
140
- @swedish_bank_lookup ||=
141
- begin
142
- relative_path = '../../../data/raw/swedish_bank_lookup.yml'
143
- raw_info = YAML.load_file(File.expand_path(relative_path, __FILE__))
144
-
145
- raw_info.map { |bank| bank.merge(range: Range.new(*bank[:range])) }
146
- end
147
- end
148
- private_class_method :bank_info_table
149
- end
150
- end