lib/bic_validation/bic.rb in bic_validation-0.0.2 vs lib/bic_validation/bic.rb in bic_validation-0.1.0

- old
+ new

@@ -1,5 +1,7 @@ +require 'active_support/core_ext/object/try' + module BicValidation class Bic def initialize(code) @code = code.strip.upcase @@ -20,14 +22,20 @@ def has_valid_branch_code? # WTF? http://de.wikipedia.org/wiki/ISO_9362 country[0] =~ /[^01]/ && country[1] =~ /[^O]/ end + def known? + !known_bics.include?(country.to_sym) || + known_bics[country.to_sym].include?(@code.try(:gsub, /XXX$/, '')) + end + def valid? - of_valid_length? and - of_valid_format? and - has_valid_country_code? + of_valid_length? && + of_valid_format? && + has_valid_country_code? && + has_valid_branch_code? end def bank match[1] end @@ -44,19 +52,35 @@ match[4] end private - def format - /([A-Z]{4})([A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{3})?/ - end + def format + /([A-Z]{4})([A-Z]{2})([0-9A-Z]{2})([0-9A-Z]{3})?/ + end - def match - format.match(@code) - end + def match + format.match(@code) + end - def country_codes - # http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_names_and_code_elements.htm - YAML.load(File.read(File.dirname(__FILE__) + '/country_codes.yml')) - end + def known_bics + { + DE: bics(:de), + AT: bics(:at), + CH: bics(:ch) + } + end + + def bics(country) + BankingData::Bank.where(locale: country).only(:bic) + .map { |bic| bic.first.gsub(/XXX$/, '') } + .reject(&:blank?) + .uniq + end + + def country_codes + # http://www.iso.org/iso/country_codes/iso_3166_code_lists/country_\ + # names_and_code_elements.htm + YAML.load(File.read(File.dirname(__FILE__) + '/country_codes.yml')) + end end end