Class: TrackingNumber::UPS

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

Constant Summary collapse

SEARCH_PATTERN =
/(\b1\s*Z\s*(\w\s*){16,16}\b)/
VERIFY_PATTERN =
/^1Z(\w{15,15})(\w)$/

Instance Attribute Summary

Attributes inherited from Base

#original_number, #tracking_number

Instance Method Summary collapse

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

#carrierObject



6
7
8
# File 'lib/tracking_number/ups.rb', line 6

def carrier
  :ups
end

#decodeObject



35
36
37
38
39
40
41
# File 'lib/tracking_number/ups.rb', line 35

def decode
  {:shipper_account =>  self.tracking_number.to_s.slice(2...8),
   :service_type => self.tracking_number.to_s.slice(8...10),
   :package_identifier =>  self.tracking_number.to_s.slice(10...17),
   :check_digit => self.tracking_number.to_s.slice(17, 18)
  }
end

#matchesObject



10
11
12
# File 'lib/tracking_number/ups.rb', line 10

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

#service_typeObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tracking_number/ups.rb', line 43

def service_type
  case decode[:service_type]
  when "01"
   "UPS United States Next Day Air (Red)"
  when "02"
   "UPS United States Second Day Air (Blue)"
  when "03"
   "UPS United States Ground"
  when "12"
   "UPS United States Third Day Select"
  when "13"
   "UPS United States Next Day Air Saver (Red Saver)"
  when "15"
   "UPS United States Next Day Air Early A.M."
  when "22"
   "UPS United States Ground - Returns Plus - Three Pickup Attempts"
  when "32"
   "UPS United States Next Day Air Early A.M. - COD"
  when "33"
   "UPS United States Next Day Air Early A.M. - Saturday Delivery, COD"
  when "41"
   "UPS United States Next Day Air Early A.M. - Saturday Delivery"
  when "42"
   "UPS United States Ground - Signature Required"
  when "44"
   "UPS United States Next Day Air - Saturday Delivery"
  when "66"
   "UPS United States Worldwide Express"
  when "72"
   "UPS United States Ground - Collect on Delivery"
  when "78"
   "UPS United States Ground - Returns Plus - One Pickup Attempt"
  when "90"
   "UPS United States Ground - Returns - UPS Prints and Mails Label"
  when "A0"
   "UPS United States Next Day Air Early A.M. - Adult Signature Required"
  when "A1"
   "UPS United States Next Day Air Early A.M. - Saturday Delivery, Adult Signature Required"
  when "A2"
   "UPS United States Next Day Air - Adult Signature Required"
  when "A8"
   "UPS United States Ground - Adult Signature Required"
  when "A9"
   "UPS United States Next Day Air Early A.M. - Adult Signature Required, COD"
  when "AA"
   "UPS United States Next Day Air Early A.M. - Saturday Delivery, Adult Signature Required, COD"
  end
end

#valid_checksum?Boolean

Returns:

  • (Boolean)


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

def valid_checksum?
  sequence = tracking_number.slice(2...17)
  check_digit = tracking_number.slice(17, 18)

  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