class AfterShip

Quick rundown, check individual methods for more info:

client = AfterShip.new(api_key: 'your-aftership-api-key')
client.trackings
client.tracking('tracking-number', 'ups')
client.create_tracking('tracking-number', 'ups', order_id: 'external-id')
client.update_tracking('tracking-number', 'ups', order_id: 'external-id')
client.couriers

To debug:

AfterShip.debug = true

Some test trackings:

client.tracking('1ZA2207X0444990982', 'ups')

Constants

COURIERS_ENDPOINT

The activated couriers endpoint URL.

DEFAULT_API_ADDRESS

The API root URL.

JSON_OPTIONS

Common JSON loading/dumping options.

TAG_STATUS

Tag to human-friendly status conversion

TRACKINGS_ENDPOINT

The trackings endpoint URL.

VERSION

Version number, happy now?

Attributes

debug[RW]

If debugging is turned on, it is passed to Typhoeus as “verbose” options, which is passed down to Ethon and displays request/response in STDERR.

@return [Bool]

api_key[R]

The API key required for the AfterShip service.

@return [String]

Public Class Methods

new(options) click to toggle source

Init the client:

client = AfterShip.new(api_key: 'your-aftership-api-key')

@param options [Hash] @option options :api_key [String]

# File lib/after_ship.rb, line 80
def initialize(options)
  @api_key = options.fetch(:api_key)
end

Public Instance Methods

couriers() click to toggle source

Get a list of activated couriers. www.aftership.com/docs/api/4/couriers/get-couriers

client.couriers

# Will return list of Courier objects:

[
  #<AfterShip::Courier:0x007fa2771d4bf8
    @name="USPS",
    @other_name="United States Postal Service",
    @phone="+1 800-275-8777",
    @required_fields=[],
    @slug="usps",
    @web_url="https://www.usps.com">,
  #<AfterShip::Courier ...>
  ...
]

@return [Hash]

# File lib/after_ship.rb, line 262
def couriers
  data = Request.get(url: COURIERS_ENDPOINT, api_key: api_key) do |response|
    response.fetch(:data).fetch(:couriers)
  end

  data.map { |datum| Courier.new(datum) }
end
create_tracking(tracking_number, courier, options = {}) click to toggle source

Create a new tracking. www.aftership.com/docs/api/4/trackings/post-trackings

client.create_tracking('tracking-number', 'ups', order_id: 'external-id')

# Will return Tracking object or raise
# AfterShip::Error::TrackingAlreadyExists:

#<AfterShip::Tracking ...>

@param tracking_number [String] @param courier [String] @param options [Hash]

@return [Hash]

rubocop:disable Metrics/MethodLength

# File lib/after_ship.rb, line 198
def create_tracking(tracking_number, courier, options = {})
  body = {
    tracking: {
      tracking_number: tracking_number,
      slug:            courier
    }.merge(options)
  }

  data = Request.post(url: TRACKINGS_ENDPOINT, api_key: api_key,
                      body: body) do |response|
    response.fetch(:data).fetch(:tracking)
  end

  Tracking.new(data)
end
tracking(tracking_number, courier) click to toggle source

Get a single tracking. Raises an error if not found. www.aftership.com/docs/api/4/trackings/get-trackings-slug-tracking_number

client.tracking('tracking-number', 'ups')

# Will return Tracking object or raise AfterShip::Error::NotFound:

#<AfterShip::Tracking:0x007f838ef44e58
  @active=false,
  @android=[],
  @courier="UPS",
  @created_at=#<DateTime: 2014-11-19T15:16:17+00:00 ...>,
  @custom_fields={},
  @customer_name=nil,
  @delivery_time=8,
  @destination_country_iso3="USA",
  @emails=[],
  @expected_delivery=nil,
  @id="546cb4414a1a2097122ae7b1",
  @ios=[],
  @order_id="PL-66448782",
  @order_id_path=nil,
  @origin_country_iso3="IND",
  @shipment_package_count=1,
  @shipment_type="UPS SAVER",
  @shipment_weight=0.5,
  @shipment_weight_unit="kg",
  @signed_by="MET CUSTOM",
  @slug="ups",
  @smses=[],
  @source="api",
  @status="Delivered",
  @tag="Delivered",
  @title="1ZA2207X0490715335",
  @tracked_count=6,
  @tracking_account_number=nil,
  @tracking_number="1ZA2207X0490715335",
  @tracking_postal_code=nil,
  @tracking_ship_date=nil,
  @unique_token="-y6ziF438",
  @updated_at=#<DateTime: 2014-11-19T22:12:32+00:00 ...>,
  @checkpoints=[
    #<AfterShip::Checkpoint:0x007f838ef57d50
      @checkpoint_time=
      #<DateTime: 2014-11-11T19:12:00+00:00 ...>,
      @city="MUMBAI",
      @country_iso3=nil,
      @country_name="IN",
      @courier="UPS",
      @created_at=
      #<DateTime: 2014-11-19T15:16:17+00:00 ...>,
      @message="PICKUP SCAN",
      @slug="ups",
      @state=nil,
      @status="In Transit",
      @tag="InTransit",
      @zip=nil>,
    #<AfterShip::Checkpoint ...>,
    ...
  ]
>

@param tracking_number [String] @param courier [String]

@return [Hash]

# File lib/after_ship.rb, line 172
def tracking(tracking_number, courier)
  url  = "#{TRACKINGS_ENDPOINT}/#{courier}/#{tracking_number}"
  data = Request.get(url: url, api_key: api_key) do |response|
    response.fetch(:data).fetch(:tracking)
  end

  Tracking.new(data)
end
trackings() click to toggle source

Get a list of trackings. www.aftership.com/docs/api/4/trackings/get-trackings

client.trackings

# Will return list of Tracking objects:

[
  #<AfterShip::Tracking ...>,
  #<AfterShip::Tracking ...>,
  ...
]

@return [Hash]

# File lib/after_ship.rb, line 98
def trackings
  data = Request.get(url: TRACKINGS_ENDPOINT, api_key: api_key) do |response|
    response.fetch(:data).fetch(:trackings)
  end

  data.map { |datum| Tracking.new(datum) }
end
update_tracking(tracking_number, courier, options = {}) click to toggle source

Update a tracking. www.aftership.com/docs/api/4/trackings/put-trackings-slug-tracking_number

client.update_tracking('tracking-number', 'ups', order_id: 'external-id')

# Will return Tracking object or raise
# AfterShip::Error::TrackingAlreadyExists:

#<AfterShip::Tracking ...>

@param tracking_number [String] @param courier [String] @param options [Hash]

@return [Hash]

# File lib/after_ship.rb, line 229
def update_tracking(tracking_number, courier, options = {})
  url  = "#{TRACKINGS_ENDPOINT}/#{courier}/#{tracking_number}"
  body = {
    tracking: options
  }

  data = Request.put(url: url, api_key: api_key, body: body) do |response|
    response.fetch(:data).fetch(:tracking)
  end

  Tracking.new(data)
end