Sha256: 2e1fce6e3ed62107104e645d61a5ac6121ad720159cea7a200dfb6d21b0bd546
Contents?: true
Size: 1.7 KB
Versions: 2
Compression:
Stored size: 1.7 KB
Contents
# http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2 module BankTools module SE class Bankgiro class InvalidOCR < StandardError; end class OverlongOCR < InvalidOCR; end class BadChecksum < InvalidOCR; end class MustBeNumeric < InvalidOCR; end class OCR MIN_LENGTH = 2 MAX_LENGTH = 25 def self.number_to_ocr(number, opts = {}) number = number.to_s add_length_digit = opts.fetch(:length_digit, false) pad = opts.fetch(:pad, "").to_s raise MustBeNumeric unless number.match(/\A\d+\z/) # Padding isn't something BGC specifies, but we needed it to support a legacy scheme. number += pad # Adding 2: 1 length digit, 1 check digit. number += ((number.length + 2) % 10).to_s if add_length_digit number_with_ocr = number + Utils.luhn_checksum(number).to_s length = number_with_ocr.length if length > MAX_LENGTH raise OverlongOCR, "Bankgiro OCR must be #{MIN_LENGTH} - #{MAX_LENGTH} characters (this one would be #{length} characters)" end number_with_ocr end def self.number_from_ocr(number, opts = {}) number = number.to_s strip_length_digit = opts.fetch(:length_digit, false) strip_padding = opts.fetch(:pad, "").to_s raise MustBeNumeric unless number.match(/\A\d+\z/) raise BadChecksum unless Utils.valid_luhn?(number) digits_to_chop = 1 # Checksum. digits_to_chop += 1 if strip_length_digit digits_to_chop += strip_padding.length number[0...-digits_to_chop] end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
banktools-se-0.11.0 | lib/banktools-se/bankgiro/ocr.rb |
banktools-se-0.10.0 | lib/banktools-se/bankgiro/ocr.rb |