Module: IbanBic
- Defined in:
- lib/iban_bic/core.rb,
lib/iban_bic/engine.rb,
lib/iban_bic/version.rb,
lib/iban_bic/configuration.rb,
lib/generators/iban_bic/install_generator.rb
Defined Under Namespace
Classes: Configuration, Engine, InstallGenerator
Constant Summary
collapse
- VERSION =
"0.1.0"
Class Method Summary
collapse
Class Method Details
.bics ⇒ Object
71
72
73
|
# File 'lib/iban_bic/core.rb', line 71
def bics
configuration.static_bics? ? static_bics : dynamic_bics
end
|
.calculate_bic(iban) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/iban_bic/core.rb', line 43
def calculate_bic(iban)
country = iban[0..1]
parts = parse(iban)
return nil unless parts&.fetch(:bank, nil)
bics.dig(country, parts[:bank])
end
|
.calculate_check(iban) ⇒ Object
23
24
25
26
27
28
29
30
31
|
# File 'lib/iban_bic/core.rb', line 23
def calculate_check(iban)
98 - "#{iban[4..-1]}#{iban[0..3]}".each_char.map do |char|
case char
when "0".."9" then char
when "A".."Z" then (char.ord - 55).to_s
else raise ArgumentError, "Invalid IBAN format"
end
end .join.to_i % 97
end
|
.clear_cache ⇒ Object
51
52
53
|
# File 'lib/iban_bic/core.rb', line 51
def clear_cache
@parser = @static_bics = @dynamic_bics = nil
end
|
.configuration ⇒ Object
6
7
8
|
# File 'lib/iban_bic/core.rb', line 6
def configuration
@configuration ||= Configuration.new
end
|
10
11
12
|
# File 'lib/iban_bic/core.rb', line 10
def configure
configuration.instance_eval(&Proc.new)
end
|
.dynamic_bics ⇒ Object
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/iban_bic/core.rb', line 81
def dynamic_bics
@dynamic_bics ||= begin
ret = {}
Bic.find_each do |bic|
ret[bic.country] = {} unless ret[bic.country]
ret[bic.country][bic.bank_code] = bic.bic
end
ret.freeze
end
end
|
19
20
21
|
# File 'lib/iban_bic/core.rb', line 19
def has_tags?(iban, searched_tags)
(tags[iban[0..1]] & searched_tags).any?
end
|
.parse(iban) ⇒ Object
14
15
16
17
|
# File 'lib/iban_bic/core.rb', line 14
def parse(iban)
parts = parser[iban[0..1]]&.match(iban)
parts ? ActiveSupport::HashWithIndifferentAccess[parts.names.zip(parts.captures)] : nil
end
|
.parser ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/iban_bic/core.rb', line 55
def parser
@parser ||= Hash[
::YAML.load_file(configuration.iban_meta_path).map do |country, meta|
[country, /^(?<country>#{country})#{meta["parts"].delete(" ")}$/]
end
].freeze
end
|
.static_bics ⇒ Object
75
76
77
78
79
|
# File 'lib/iban_bic/core.rb', line 75
def static_bics
@static_bics ||= Hash[Dir.glob(File.join(configuration.static_bics_path, "*.yml")).map do |file|
[File.basename(file).delete(".yml").upcase, YAML.load_file(file)]
end].freeze
end
|
63
64
65
66
67
68
69
|
# File 'lib/iban_bic/core.rb', line 63
def tags
@tags ||= Hash[
::YAML.load_file(configuration.iban_meta_path).map do |country, meta|
[country, meta["tags"]&.split&.map(&:to_sym) || []]
end
].freeze
end
|
.valid_check?(iban) ⇒ Boolean
33
34
35
|
# File 'lib/iban_bic/core.rb', line 33
def valid_check?(iban)
calculate_check(iban) == 97
end
|
.valid_country_check?(iban) ⇒ Boolean
37
38
39
40
41
|
# File 'lib/iban_bic/core.rb', line 37
def valid_country_check?(iban)
parts = parse(iban)
validator = IbanBic.configuration.country_validators[parts[:country]]
validator.nil? || validator.call(parts)
end
|