Class: TrackingNumber::OnTrac

Inherits:
Base
  • Object
show all
Defined in:
lib/tracking_number/ontrac.rb

Constant Summary collapse

SEARCH_PATTERN =
/(\b(C\s*)([0-9]\s*){14,14}\b)/
VERIFY_PATTERN =
/^(C[0-9]{13,13})([0-9])$/

Instance Attribute Summary

Attributes inherited from Base

#original_number, #tracking_number

Instance Method Summary collapse

Methods inherited from Base

#decode, #initialize, #inspect, scan, search, #to_s, #valid?, #valid_format?

Constructor Details

This class inherits a constructor from TrackingNumber::Base

Instance Method Details

#carrierObject



5
6
7
# File 'lib/tracking_number/ontrac.rb', line 5

def carrier
  :ontrac
end

#matchesObject



9
10
11
# File 'lib/tracking_number/ontrac.rb', line 9

def matches
  self.tracking_number.scan(VERIFY_PATTERN).flatten
end

#valid_checksum?Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tracking_number/ontrac.rb', line 13

def valid_checksum?
  # checksum calculation is the same as UPS
  sequence, check_digit = matches

  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
    x *= 2 if i.odd?
    total += x
  end

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

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