Class: TrackingNumber::FedExGround96

Inherits:
FedEx
  • Object
show all
Defined in:
lib/tracking_number/fedex.rb

Constant Summary collapse

SEARCH_PATTERN =
/(\b9\s*6\s*([0-9]\s*){20,20}\b)/
VERIFY_PATTERN =
/^96[0-9]{5,5}([0-9]{14,14})([0-9])$/
LENGTH =
22

Instance Attribute Summary

Attributes inherited from Base

#original_number, #tracking_number

Instance Method Summary collapse

Methods inherited from FedEx

#carrier

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from TrackingNumber::Base

Instance Method Details

#decodeObject



64
65
66
67
68
69
70
71
72
# File 'lib/tracking_number/fedex.rb', line 64

def decode
  {:application_id => self.tracking_number.to_s.slice(0...2),
   :serial_container =>  self.tracking_number.to_s.slice(2...4),
   :service_code => self.tracking_number.to_s.slice(4...7),
   :shipper_id =>  self.tracking_number.to_s.slice(7...14),
   :package_identifier =>  self.tracking_number.to_s.slice(14...21),
   :check_digit => self.tracking_number.slice(21...22)
  }
end

#matchesObject



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

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

#valid_checksum?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tracking_number/fedex.rb', line 74

def valid_checksum?
  # 22 numbers
  # http://fedex.com/us/solutions/ppe/FedEx_Ground_Label_Layout_Specification.pdf
  # 96 - UCC/EAN Application Identifier

  # [0-9]{2,2} - SCNC
  # [0-9]{3,3} - Class Of Service
  # [0-9]{7,7} - RPS Shipper ID (used in calculation)
  # [0-9]{7,7} - Package Number (used in calculation)
  # [0-9]      - Check Digit
  sequence, check_digit = matches

  total = 0
  sequence.chars.to_a.map(&:to_i).reverse.each_with_index do |x, i|
    x *= 3 if i.even?
    total += x
  end

  check = total % 10
  check = (10 - check) unless (check.zero?)
  return true if check == check_digit.to_i
end