Class: TrackingNumber::USPS20

Inherits:
USPS
  • Object
show all
Defined in:
lib/tracking_number/usps.rb

Constant Summary collapse

SEARCH_PATTERN =

www.usps.com/cpim/ftp/pubs/pub109.pdf (Publication 109. Extra Services Technical Guide, pg. 19) www.usps.com/cpim/ftp/pubs/pub91.pdf (Publication 91. Confirmation Services Technical Guide pg. 38)

/(\b([0-9]\s*){20,20}\b)/
VERIFY_PATTERN =
/^([0-9]{2,2})([0-9]{9,9})([0-9]{8,8})([0-9])$/

Instance Attribute Summary

Attributes inherited from Base

#original_number, #tracking_number

Instance Method Summary collapse

Methods inherited from USPS

#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



80
81
82
83
84
85
86
# File 'lib/tracking_number/usps.rb', line 80

def decode
  {:service_code =>  self.tracking_number.to_s.slice(0...2),
   :mailer_id => self.tracking_number.to_s.slice(2...11),
   :package_identifier =>  self.tracking_number.to_s.slice(11...19),
   :check_digit => self.tracking_number.slice(19...20)
  }
end

#matchesObject



76
77
78
# File 'lib/tracking_number/usps.rb', line 76

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

#service_typeObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tracking_number/usps.rb', line 88

def service_type
  case decode[:service_code]
  when "71"
    "Certified Mail"
  when "73"
    "Insured Mail"
  when "77"
    "Registered Mail"
  when "81"
    "Return Receipt for Merchandise"
  end
end

#valid_checksum?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tracking_number/usps.rb', line 101

def valid_checksum?
  chars = tracking_number.chars.to_a
  check_digit = chars.pop

  total = 0
  chars.reverse.each_with_index do |c, i|
    x = c.to_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