Trackerx package tracking API.
-
Clone repository.
-
Have PostreSQL, Redis and Ruby 2.7.2 installed.
-
Run:
bundle install
-
Create a
.envfile at the root of the project with the specified values in.env.example. -
Run tests with:
rspec
-
You can start the API with 2 terminals:
# terminal 1 rails s # terminal 2 sidekiq
-
You can access the Swagger docs on /api-docs
In the event you want to add new trackers, follow these steps:
We will be using as a example UPS.
-
Add a new instance method named
ups_trackerinlib/tracking_instance/tracking_instance.rb# lib/tracking_instance/tracking_instance.rb # ... def ups_tracker(tracking_number) perform_job(tracking_number, UpsTrackerWorker) end
-
Create a new worker class
UpsTrackerWorkerinapp/workers# app/workers/ups_tracker_worker.rb # frozen_string_literal: true class UpsTrackerWorker include Sidekiq::Worker include Sidekiq::Throttled::Worker sidekiq_options queue: :normal sidekiq_throttle_as :delivery_api def perform(tracking_number) Tracker::UpsTracker.call(tracking_number: tracking_number) end end
-
Create a new tracker class
Tracker::UpsTrackerinapp/services/tracker# app/services/tracker/ups_tracker.rb # frozen_string_literal: true module Tracker class UpsTracker < ApplicationTracker include ::Constants::UpsTracking def call with_retry(number_of_retries, retries_interval, EXCEPTIONS) do Timeout.timeout(timeout) do tracking_info = track_package status = STATUS_CODES[tracking_info.status_code.to_sym].presence || 'EXCEPTION' Sidekiq.logger.info(status) unless Rails.env.test? # just for challenge visualization purpose status end end rescue => e Sidekiq.logger.error(e.message) unless Rails.env.test? false end private def track_package # this method will return the UPS tracking_info for the specified tracking_number end def ups return @ups if defined? @ups @ups = nil # UPS service initialization end end end
-
Create a file of the UPS constants needed to return the correct statuses and exceptions to raise named
Constants::UpsTrackinginlib/constants# lib/constants/ups_tracking.rb # frozen_string_literal: true module Constants module UpsTracking EXCEPTIONS = [ # list of exceptions for this tracker to be catched and raised when they happen ].freeze STATUS_CODES = { # list of status codes for this tracker and their correspondant application status # ... OC: 'CREATED', # Order Created IT: 'ON_TRANSIT', # In Transit DL: 'DELIVERED', # Delivered SE: 'EXCEPTION' # Shipment Exception # ... }.freeze end end