Sha256: e0f57eba5157c9e227859733a64417151fa0928621bcc9d18ffab53300abe476

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

# http://sv.wikipedia.org/wiki/Bankgirot#Bankgironummer

require "banktools-se/bankgiro/ocr"

module BankTools
  module SE
    class Bankgiro
      attr_reader :number

      def self.number_to_ocr(number, opts = {})
        OCR.number_to_ocr(number, opts)
      end

      def self.number_from_ocr(number, opts = {})
        OCR.number_from_ocr(number, opts)
      end

      def initialize(number)
        @number = number
      end

      def valid?
        errors.empty?
      end

      def errors
        errors = []

        errors << Errors::TOO_SHORT if digits.length < 7
        errors << Errors::TOO_LONG if digits.length > 8
        errors << Errors::INVALID_CHARACTERS if number.to_s.match(/[^0-9 -]/)
        errors << Errors::BAD_CHECKSUM unless Utils.valid_luhn?(number)

        errors
      end

      def normalize
        if valid?
          digits.split(/(\d{4})$/).join("-")
        else
          number
        end
      end

      def fundraising?
        valid? && digits.match(/\A90[0-4]/)
      end

      private

      def digits
        number.to_s.gsub(/\D/, '')
      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.rb
banktools-se-0.10.0 lib/banktools-se/bankgiro.rb