Module: Trackerific

Defined in:
lib/trackerific.rb,
lib/trackerific/base.rb,
lib/trackerific/event.rb,
lib/trackerific/error.rb,
lib/trackerific/details.rb,
lib/trackerific/services/ups.rb,
lib/trackerific/services/usps.rb,
lib/trackerific/services/fedex.rb

Overview

Trackerific provides package tracking to Rails apps.

Defined Under Namespace

Classes: Base, Details, Error, Event, FedEx, UPS, USPS

Instance Method Summary (collapse)

Instance Method Details

- (Trackerific::Base) tracking_service(package_id)

Checks a string for a valid package tracking service

Examples:

Find out which service provider will track a valid FedEx number

include Trackerific
tracking_service "183689015000001" # => Trackerific::FedEx

Parameters:

  • package_id (String)

    the package identifier

Returns:

  • (Trackerific::Base)

    the Trackerific class that can track the given package id, or nil if none found.



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

def tracking_service(package_id)
  # loop through each constant in Trackerific
  Trackerific.constants.each do |const|
    # get the constant's class
    cls = Trackerific.const_get(const)
    # check if it descends from Trackerific::Base
    if cls.superclass == Trackerific::Base
      # loop through each package id matcher
      cls.package_id_matchers.each do |matcher|
        # return the class if it matches
        return cls if package_id =~ matcher
      end
    end
  end
  # if we've made it this far, nothing matched
  nil
end