Sha256: f95bbf1e04bdceac5d4e44513fdf40ae2ecbedc35ffe421c119841e782e0f406

Contents?: true

Size: 1.19 KB

Versions: 15

Compression:

Stored size: 1.19 KB

Contents

module BankTools
  module SE
    class Plusgiro

      # Could sadly not find anything more authoritative than
      #   http://pellesoft.se/communicate/forum/view.aspx?msgid=267449&forumid=63&sum=0

      attr_reader :number

      def initialize(number)
        @number = number
      end

      def valid?
        errors.empty?
      end

      def errors
        errors = []

        errors << Errors::TOO_SHORT if digits.length < 2
        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?
          pre, pairs, post = digits.split(/(\d{2}*)(\d)$/)
          pairs = pairs.split(/(\d\d)/).reject { |x| x.empty? }
          [ pre, pairs.join(" "), "-", post ].join
        else
          number
        end
      end

      # http://www.plusgirot.se/Om+PlusGirot/90-konton/508552.html
      # http://www.insamlingskontroll.se/
      def fundraising?
        valid? && digits.match(/\A90\d{5}$/)
      end

      private

      def digits
        number.to_s.gsub(/\D/, '')
      end

    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
banktools-se-3.1.0 lib/banktools-se/plusgiro.rb
banktools-se-3.0.1 lib/banktools-se/plusgiro.rb
banktools-se-3.0.0 lib/banktools-se/plusgiro.rb
banktools-se-2.6.3 lib/banktools-se/plusgiro.rb
banktools-se-2.6.2 lib/banktools-se/plusgiro.rb
banktools-se-2.6.1 lib/banktools-se/plusgiro.rb
banktools-se-2.6.0 lib/banktools-se/plusgiro.rb
banktools-se-2.5.0 lib/banktools-se/plusgiro.rb
banktools-se-2.4.0 lib/banktools-se/plusgiro.rb
banktools-se-2.3.0 lib/banktools-se/plusgiro.rb
banktools-se-2.1.0 lib/banktools-se/plusgiro.rb
banktools-se-2.0.0 lib/banktools-se/plusgiro.rb
banktools-se-1.0.0 lib/banktools-se/plusgiro.rb
banktools-se-0.11.0 lib/banktools-se/plusgiro.rb
banktools-se-0.10.0 lib/banktools-se/plusgiro.rb