Class: BankTools::SE::Plusgiro

Inherits:
Object
  • Object
show all
Defined in:
lib/banktools-se/plusgiro.rb

Constant Summary collapse

MAX_LENGTH =

Could sadly not find anything more authoritative than

http://pellesoft.se/communicate/forum/view.aspx?msgid=267449&forumid=63&sum=0
https://www.nordea.se/foretag/kundservice/fragor-och-svar-konton.html#faq=Kontouppgifter+432454
10
MIN_LENGTH =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Plusgiro

Returns a new instance of Plusgiro.



13
14
15
# File 'lib/banktools-se/plusgiro.rb', line 13

def initialize(number)
  @number = number
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



11
12
13
# File 'lib/banktools-se/plusgiro.rb', line 11

def number
  @number
end

Instance Method Details

#errorsObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/banktools-se/plusgiro.rb', line 21

def errors
  errors = []

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

  errors
end

#fundraising?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/banktools-se/plusgiro.rb', line 44

def fundraising?
  valid? && digits.match(/\A90\d{5}$/)
end

#normalizeObject



32
33
34
35
36
37
38
39
40
# File 'lib/banktools-se/plusgiro.rb', line 32

def normalize
  if valid?
    pre, pairs, post = digits.split(/(\d{2}*)(\d)$/)
    pairs = pairs.split(/(\d\d)/).reject(&:empty?)
    [ pre, pairs.join(" "), "-", post ].join
  else
    number
  end
end

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/banktools-se/plusgiro.rb', line 17

def valid?
  errors.empty?
end