Module: TrackingNumber::ChecksumValidations

Defined in:
lib/tracking_number/checksum_validations.rb

Class Method Summary collapse

Class Method Details

.validates_mod10?(sequence, check_digit, extras = {}) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tracking_number/checksum_validations.rb', line 35

def validates_mod10?(sequence, check_digit, extras = {})
  total = 0
  sequence.chars.each_with_index do |c, i|
    x = if c[/[0-9]/] # numeric
      c.to_i
    else
      (c[0].ord - 3) % 10
    end

    if extras[:odds_multiplier] && i.odd?
      x *= extras[:odds_multiplier].to_i
    elsif extras[:evens_multiplier] && i.even?
      x *= extras[:evens_multiplier].to_i
    end

    total += x
  end

  check = (total % 10)
  check = (10 - check) unless (check.zero?)

  return (check.to_i == check_digit.to_i)
end

.validates_mod7?(sequence, check_digit, extras = {}) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/tracking_number/checksum_validations.rb', line 59

def validates_mod7?(sequence, check_digit, extras = {})
  # standard mod 7 check
  return true if sequence.to_i % 7 == check_digit.to_i
end

.validates_s10?(sequence, check_digit, extras = {}) ⇒ Boolean

Returns:

  • (Boolean)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tracking_number/checksum_validations.rb', line 4

def validates_s10?(sequence, check_digit, extras = {})
  weighting = [8,6,4,2,3,5,9,7]

  total = 0
  sequence.chars.to_a.zip(weighting).each do |(a,b)|
    total += a.to_i * b.to_i
  end

  remainder = total % 11
  check = case remainder
  when 1
    0
  when 0
    5
  else
    11 - remainder
  end

  return check.to_i == check_digit.to_i
end

.validates_sum_product_with_weightings_and_modulo?(sequence, check_digit, extras = {}) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/tracking_number/checksum_validations.rb', line 25

def validates_sum_product_with_weightings_and_modulo?(sequence, check_digit, extras = {})
  weighting = extras[:weightings] || []

  total = 0
  sequence.chars.to_a.zip(weighting).each do |(a,b)|
    total += a.to_i * b
  end
  return (total % extras[:modulo1] % extras[:modulo2]) == check_digit.to_i
end