Sha256: da93eff90c519b21c8bc9f1bcb5e39c746239f5b4a719ef8e878293fd153ba11

Contents?: true

Size: 972 Bytes

Versions: 5

Compression:

Stored size: 972 Bytes

Contents

module ParcelApi

  # This module provides API requests to track the parcels and
  # return tracking information for a specific tracking reference.

  class Track
    PARCELTRACK_URL = '/ParcelTrack/3.0/parcels'

    # Creates a new ParcelApi::Track instance.

    def initialize(connection=nil)
      @connection ||= connection || ParcelApi::Client.connection
    end

    # Return details for a specific tracking reference.
    # @param tracking_reference [String]
    # @return Object of tracking details

    def details(tracking_reference)
      details_url = File.join(PARCELTRACK_URL, tracking_reference.to_s)
      response = @connection.get details_url
      events = response.parsed['results'].tap do |d|
        d['tracking_events'].map {|e| e['event_datetime'] = Time.parse(e['event_datetime'])}
        d['tracking_events'].sort_by! {|k| k['event_datetime'].to_i}
      end
      RecursiveOpenStruct.new(events, recurse_over_arrays: true)
    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
parcel_api-1.2.1 lib/parcel_api/track.rb
parcel_api-1.2.0 lib/parcel_api/track.rb
parcel_api-1.1.1 lib/parcel_api/track.rb
parcel_api-1.1.0 lib/parcel_api/track.rb
parcel_api-1.0.0 lib/parcel_api/track.rb