# frozen_string_literal: true module SecId # https://en.wikipedia.org/wiki/International_Securities_Identification_Number class ISIN < Base ID_REGEX = /\A (? (?[A-Z]{2}) (?[A-Z0-9]{9})) (?\d)? \z/x.freeze attr_reader :country_code, :nsin def initialize(isin) isin_parts = parse isin @identifier = isin_parts[:identifier] @country_code = isin_parts[:country_code] @nsin = isin_parts[:nsin] @check_digit = isin_parts[:check_digit]&.to_i end def calculate_check_digit return mod_10(luhn_sum) if valid_format? raise InvalidFormatError, "ISIN '#{full_number}' is invalid and check-digit cannot be calculated!" end private # https://en.wikipedia.org/wiki/Luhn_algorithm def luhn_sum sum = 0 digitized_identifier.reverse.each_slice(2) do |even, odd| double_even = (even || 0) * 2 double_even -= 9 if double_even > 9 sum += double_even + (odd || 0) end sum end def digitized_identifier @digitized_identifier ||= identifier.each_char.flat_map(&method(:char_to_digits)) end end end