Class: TrackingNumber::USPS91
- Defined in:
- lib/tracking_number/usps.rb
Constant Summary collapse
- SEARCH_PATTERN =
[/(\b(?:420\s*\d{5})?9\s*[1-5]\s*(?:(?:(?:[0-9]\s*){20}\b)|(?:(?:[0-9]\s*){24}\b)))/, /(\b([0-9]\s*){20}\b)/]
- VERIFY_PATTERN =
/^(?:420\d{5})?(9[1-5](?:[0-9]{19}|[0-9]{23}))([0-9])$/
Instance Attribute Summary
Attributes inherited from Base
#original_number, #tracking_number
Instance Method Summary collapse
-
#decode ⇒ Object
Sometimes these numbers will appear without the leading 91, 93, or 94, though, so we need to account for that case.
- #matches ⇒ Object
- #valid_checksum? ⇒ Boolean
Methods inherited from USPS
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
#decode ⇒ Object
Sometimes these numbers will appear without the leading 91, 93, or 94, though, so we need to account for that case
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/tracking_number/usps.rb', line 14 def decode # Application ID: 91, 93, 94 or 95 # Service Code: 2 Digits # Mailer Id: 8 Digits # Package Id: 9 Digits # Checksum: 1 Digit base_tracking_number = self.tracking_number.to_s.gsub(/^420\d{5}/, '') {:application_id => base_tracking_number.to_s.slice(0...2), :service_code => base_tracking_number.to_s.slice(2...4), :mailer_id => base_tracking_number.to_s.slice(4...12), :package_identifier => base_tracking_number.to_s.slice(12...21), :check_digit => base_tracking_number.slice(21...22) } end |
#matches ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/tracking_number/usps.rb', line 31 def matches if self.tracking_number =~ /^(420\d{5})?9[1-5]/ self.tracking_number.scan(VERIFY_PATTERN).flatten else "91#{self.tracking_number}".scan(VERIFY_PATTERN).flatten end end |
#valid_checksum? ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tracking_number/usps.rb', line 39 def valid_checksum? if self.tracking_number =~ /^(420\d{5})?9[1-5]/ return true if weighted_usps_checksum_valid?(tracking_number) else if weighted_usps_checksum_valid?("91#{self.tracking_number}") # set the tracking number to the 91 format if it passes this test self.tracking_number = "91#{self.tracking_number}" return true end end end |