Module: QRCreditorReference

Defined in:
lib/qr-bills/qr-creditor-reference.rb

Overview

implement Creditor Reference ISO 11649 generator

Constant Summary collapse

PREFIX =
"RF"

Class Method Summary collapse

Class Method Details

.create(reference) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/qr-bills/qr-creditor-reference.rb', line 37

def self.create(reference)
  reference = reference.delete(' ')
  chars = reference.split('')

  if chars.size == 0 
    raise QRExceptions::INVALID_PARAMETERS + ": provided reference too short: must be at least one char"
  end

  # max 25 chars: 2 chars (RF) + 2 chars (check code) + 21 chars (reference)
  if chars.size > 21 
    raise QRExceptions::INVALID_PARAMETERS + ": provided reference too long: must be less than 21 chars"
  end

  reference_val = ""

  chars.each do |c|
    reference_val += get_char_value(c).to_s
  end

  # put RF+00 at the end to resolve check code
  reference_val += @char_values["R".to_sym].to_s + @char_values["F".to_sym].to_s + "00"

  # get check code
  # when performing the validation of the check code (n % 97) 
  # the remainder must be equal to 1, thus the 98
  check_code = 98 - (reference_val.to_i % 97)

  if check_code < 10
    check_code = "0" + check_code.to_s
  end

  return PREFIX + check_code.to_s + reference
end

.get_char_value(char) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/qr-bills/qr-creditor-reference.rb', line 71

def self.get_char_value(char)
  if char =~ /[0-9]/
    return char.to_i
  end

  return @char_values[char.upcase.to_sym]
end