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

VERSION =
"1.6.0"

Class Method Summary collapse

Class Method Details

.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