module IbanBic
Constants
- VERSION
Public Instance Methods
bics()
click to toggle source
# File lib/iban_bic/core.rb, line 141 def bics configuration.static_bics? ? static_bics : dynamic_bics end
calculate_bic(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 90 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)
click to toggle source
# File lib/iban_bic/core.rb, line 59 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
calculate_valid_country_check_iban(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 77 def calculate_valid_country_check_iban(iban) validator = country_validators[iban[0..1]] return iban unless validator parts = parse(iban) validator.call(parts) parts.values.join end
clear_cache()
click to toggle source
# File lib/iban_bic/core.rb, line 110 def clear_cache @cached_variables.each { |variable| instance_variable_set(variable, nil) } @cached_variables.clear end
configuration()
click to toggle source
# File lib/iban_bic/core.rb, line 14 def configuration @configuration ||= Configuration.new end
configure(&block)
click to toggle source
# File lib/iban_bic/core.rb, line 18 def configure(&block) configuration.instance_eval(&block) end
country_validators()
click to toggle source
# File lib/iban_bic/core.rb, line 10 def country_validators @country_validators ||= {} end
dynamic_bics()
click to toggle source
# File lib/iban_bic/core.rb, line 154 def dynamic_bics @dynamic_bics ||= begin @cached_variables << :@dynamic_bics 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
fix(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 27 def fix(iban) # Fixed check IBAN parts (countries where IBAN check is always the same) must be fixed before parsing iban, fixed_iban_check = fix_fixed_iban_check(iban) parts = parse(iban) return unless parts iban = calculate_valid_country_check_iban(iban) iban = fix_iban_check(iban) unless fixed_iban_check iban end
fix_fixed_iban_check(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 40 def fix_fixed_iban_check(iban) return [iban, false] unless tags[iban[0..1]].member?(:fixed_iban_check) result = iban.dup result[2..3] = /\(\?\<iban_check>([^\)]*)\)/.match(iban_meta[iban[0..1]]["parts"])[1] [result, true] end
fix_iban_check(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 48 def fix_iban_check(iban) result = iban.dup result[2..3] = "00" result[2..3] = calculate_check(result).to_s.rjust(2, "0") result.freeze end
iban_meta()
click to toggle source
# File lib/iban_bic/core.rb, line 115 def iban_meta @iban_meta ||= ::YAML.load_file(configuration.iban_meta_path) end
like_pattern(iban, *keep_parts)
click to toggle source
# File lib/iban_bic/core.rb, line 98 def like_pattern(iban, *keep_parts) parts = IbanBic.parse(iban) parts.map { |part, value| keep_parts.include?(part.to_sym) ? value : value.tr("^_", "_") } .join end
like_pattern_from_parts(country:, **parts)
click to toggle source
# File lib/iban_bic/core.rb, line 103 def like_pattern_from_parts(country:, **parts) ia_parts = parts.with_indifferent_access parser[country].to_s.scan(/\(?\<([^\>]+)\>([^\)]*)\)/).map do |part, regex| (part == "country" && country) || ia_parts[part] || Regexp.new(regex).examples.first.tr("^_", "_") end.join end
parse(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 22 def parse(iban) parts = parser[iban[0..1]]&.match(iban) parts ? ActiveSupport::HashWithIndifferentAccess[parts.names.zip(parts.captures)] : nil end
parser()
click to toggle source
# File lib/iban_bic/core.rb, line 119 def parser @parser ||= begin @cached_variables << :@parser Hash[ iban_meta.map do |country, meta| [country, /^(?<country>#{country})#{meta["parts"].delete(" ")}$/] end ].freeze end end
random_generator()
click to toggle source
# File lib/iban_bic/random.rb, line 20 def random_generator @random_generator ||= begin @cached_variables << :@random_generator Hash[ iban_meta.map do |country, meta| [country, /^#{country}#{meta["parts"].delete(" ").gsub(/\(\?\<\w+\>([^\)]*)\)/, "\\1")}$/] end ].freeze end end
random_iban(options = {})
click to toggle source
# File lib/iban_bic/random.rb, line 6 def random_iban(options = {}) country = options[:country] searched_tags = options[:tags] non_searched_tags = options[:not_tags] unless country possible_countries = random_generator.keys possible_countries -= IbanBic.tags.select { |_country, country_tags| (searched_tags - country_tags).any? } .keys if searched_tags.present? possible_countries -= IbanBic.tags.select { |_country, country_tags| (non_searched_tags & country_tags).any? } .keys if non_searched_tags.present? country = possible_countries.sample end IbanBic.fix(random_generator[country].random_example) end
static_bics()
click to toggle source
# File lib/iban_bic/core.rb, line 145 def static_bics @static_bics ||= begin @cached_variables << :@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 end
valid?(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 69 def valid?(iban) valid_check?(iban) && valid_country_check?(iban) end
valid_check?(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 73 def valid_check?(iban) calculate_check(iban) == 97 end
valid_country_check?(iban)
click to toggle source
# File lib/iban_bic/core.rb, line 86 def valid_country_check?(iban) calculate_valid_country_check_iban(iban) == iban end