Class: Rfc::VerificationDigitCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/rfc/verification_digit_calculator.rb

Constant Summary collapse

MAPPING =
{
  "0" => 0, "1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6,
  "7" => 7, "8" => 8, "9" => 9, "A" => 10, "B" => 11, "C" => 12, "D" => 13,
  "E" => 14, "F" => 15, "G" => 16, "H" => 17, "I" => 18, "J" => 19, "K" => 20,
  "L" => 21, "M" => 22, "N" => 23, "&" => 24, "O" => 25, "P" => 26, "Q" => 27,
  "R" => 28, "S" => 29, "T" => 30, "U" => 31, "V" => 32, "W" => 33, "X" => 34,
  "Y" => 35, "Z" => 36, " " => 37, "Ñ" => 38
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(rfc12_digits) ⇒ VerificationDigitCalculator

Returns a new instance of VerificationDigitCalculator.



14
15
16
# File 'lib/rfc/verification_digit_calculator.rb', line 14

def initialize(rfc12_digits)
  @rfc12_digits = rfc12_digits
end

Instance Method Details

#calculateObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rfc/verification_digit_calculator.rb', line 18

def calculate
  rfc12_padded = @rfc12_digits.ljust(12, " ")
  sum = 0
  (0..11).each do |i|
    sum += map_digit(rfc12_padded[i]) * (13 - i)
  end
  reminder = sum % 11

  return "0" if reminder.zero?

  (11 - reminder).to_s(16).upcase
end