Module: TrackingNumber

Defined in:
lib/tracking_number.rb,
lib/tracking_number/base.rb,
lib/tracking_number/info.rb,
lib/tracking_number/loader.rb,
lib/tracking_number/unknown.rb,
lib/tracking_number/version.rb,
lib/tracking_number/partnership.rb,
lib/tracking_number/checksum_validations.rb

Defined Under Namespace

Modules: ChecksumValidations, Loader Classes: Base, Info, Partnership, Unknown

Constant Summary collapse

RENAMED_TYPES =

Renamed in tracking_number_data 2.0. The old constant still describes the same barcode.

{ USPS91: :USPSIMpbC, USPS22: :USPSIMpbN, USPS34v2: :USPSIMpbC }.freeze
WITHDRAWN_TYPES =

Removed in tracking_number_data 2.0. An alias would answer is_a? for every IMpb number, which is the claim the data stopped making, so these raise instead.

{
  FedExSmartPost: 'USPSIMpbC',
  DHLECommerce30: 'USPSIMpbC'
}.freeze
VERSION =
"3.0.0"

Class Method Summary collapse

Class Method Details

.const_missing(name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tracking_number.rb', line 74

def self.const_missing(name)
  if (renamed = RENAMED_TYPES[name])
    warn "TrackingNumber::#{name} is now TrackingNumber::#{renamed}"
    return const_get(renamed)
  end

  if (replacement = WITHDRAWN_TYPES[name])
    raise NameError, "TrackingNumber::#{name} no longer exists. These are USPS IMpb barcodes " \
                     "carrying nothing that identifies the shipper, so they now decode as " \
                     "TrackingNumber::#{replacement}."
  end

  super
end

.detect(tracking_number, match: :carrier) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/tracking_number.rb', line 40

def self.detect(tracking_number, match: :carrier)
  all_matches = search(tracking_number, match: match)

  if all_matches.empty?
    Unknown.new(tracking_number)
  else
    all_matches.max_by(&:confidence)
  end
end

.detect_all(tracking_number) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/tracking_number.rb', line 50

def self.detect_all(tracking_number)
  matches = []
  (TYPES + [Unknown]).each do |test_klass|
    tn = test_klass.new(tracking_number)
    matches << tn if tn.valid?
  end

  matches
end

.new(tracking_number) ⇒ Object



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

def self.new(tracking_number)
  detect(tracking_number)
end

.search(body, match: :carrier) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tracking_number.rb', line 19

def self.search(body, match: :carrier)
  matches = TYPES.collect { |type| type.search(body) }.flatten

  # Some tracking numbers (e.g. Fedex Smartpost) are partnerships between two parties, where one party is the shipper (e.g. Fedex)
  # and the other party is the [last mile] carrier (e.g. USPS). We're probably interested in the last mile aspect of
  # the partnership, so by default we'll show those

  # Tracking numbers without a partnership are both the shipper and carrier.

  case match
  when :carrier
    matches.filter(&:carrier?)
  when :shipper
    matches.filter(&:shipper?)
  when :all
    matches
  else
    matches
  end
end