Module: Trackerific

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

Overview

Trackerific provides package tracking to Rails apps.

Defined Under Namespace

Classes: Configuration, Details, Error, Event, FedEx, Service, UPS, USPS

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Trackerific::Configuration) configuration

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Stores the configuration options for Trackerific



34
35
36
# File 'lib/trackerific/configuration.rb', line 34

def configuration
  @configuration ||= Trackerific::Configuration.new
end

+ (Trackerific::Configuration) configure {|configuration| ... }

Configures Trackerific

Examples:

Defining credentials

Trackerific.configure do |config|
  config.fedex :meter => '123456789', :account => '123456789'
end

Yields:

Returns:



44
45
46
47
# File 'lib/trackerific/configuration.rb', line 44

def configure
  yield configuration
  configuration
end

+ (Trackerific::Service) service_get(name)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets a Trackerific::Service class

Parameters:

  • name (Symbol)

    the name of the service

Returns:



31
32
33
34
35
36
# File 'lib/trackerific.rb', line 31

def service_get(name)
  services.each do |service|
    return Trackerific.const_get(service) if name == service.to_s.downcase.to_sym
  end
  return nil
end

+ (Array, Symbol) services

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets a list of all Trackerific services

Returns:

  • (Array, Symbol)

    the services



20
21
22
23
24
25
# File 'lib/trackerific.rb', line 20

def services
  # a service is any Trackerific class that descends from Trackerific::Service
  Trackerific.constants.reject { |const|
    const unless Trackerific.const_get(const).superclass == Trackerific::Service
  }
end

Instance Method Details

- (Trackerific::Details) track_package(package_id)

Tracks a package by determining its service from the package id

Examples:

Track a package

include Trackerific
# make sure to configure Trackerific before hand with the different services credentials
Trackerific.config do |config|
  config.fedex :meter => '123456789', :account => '123456789'
end
details = track_package "183689015000001"

Parameters:

  • package_id (String)

    the package identifier

Returns:

Raises:

  • (Trackerific::Error)

    raised when the server returns an error (invalid credentials, tracking package, etc.)



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

def track_package(package_id)
  # find the service that will be able to track this package
  service = tracking_service package_id
  raise Trackerific::Error "Cannot find a service to track package id #{package_id}" if service.nil?
  # get the name of the service
  service_name = service.to_s.split('::')[1].downcase
  # get the default configuration for the service
  options = Trackerific.configuration.send service_name
  # track the package
  service.new(options).track_package package_id
end

- (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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/trackerific.rb', line 47

def tracking_service(package_id)
  # loop through all the services
  Trackerific.services.each do |service|
    # get the class associated with this service
    cls = Trackerific.const_get(service)
    # loop through all the packge id regular expressions
    cls.package_id_matchers.each do |matcher|
      # return this class if the regular expression matches
      return cls if package_id =~ matcher
    end
  end
  # if we've made it this far, nothing matched
  nil
end