The objective of this project is the implementation of a microservice that allows tracking shipments made by logistics companies such as FedEx.
For the implementation I used the ruby on rails framework configured only as an API.
The API consists of an endpoint for creating shipments and a task for synchronization.
The idea is that microservice clients who want to get the status of a shipment make a POST request to the shipments creation endpoint. Then, every certain period of time, the microservice must execute the synchronization task which is responsible for:
- Search shipments with pending notifications.
- Obtain the status of shipments by consulting an external service.
- Update the status of shipments in the microservice database.
- Notify the status of shipments to a notification topic.
The communication of notifications about the status of shipments is done in an asynchronous manner. For this I used a pub/sub pattern supported in the cloud by AWS.
I used SNS, SQS and Cognito.
- Ruby 2.7.0
- Rails 6.0.3
- PostgreSQL
$ git clone https://github.com/anmacagno/shipments-tracking.git
$ bundle install
Important: postgres will use the default role. This is the same name as the operating system user that initialized the database.
$ rails db:create
$ rails db:setup
$ bundle exec rspec
The project includes seed data, but if you want you can start the server and make a request to the shipments creation endpoint.
$ rails server
POST http://localhost:3000/shipments
{
"shipment": {
"carrier": "sandbox",
"tracking_reference": "001"
}
}
The script syncs shipments for a given carrier (sandbox or fedex).
$ rake shipments:synchronize[sandbox]
$ rake shipments:synchronize[fedex]
These are the gems that I added to the project:
- interactor-rails
- fedex
- aws-sdk-sns
- rspec-rails
- shoulda-matchers
These are the folders and files created/modified. Where to start? Look at the file lib/tasks/shipments.rake.
├── app
│ ├── controllers
│ │ └── shipments_controller.rb
│ ├── factories
│ │ ├── trackers
│ │ │ ├── base_tracker.rb
│ │ │ ├── fedex_tracker.rb
│ │ │ └── sandbox_tracker.rb
│ │ └── tracker_factory.rb
│ ├── interactors
│ │ ├── steps
│ │ │ ├── find_shipments.rb
│ │ │ ├── publish_notifications.rb
│ │ │ ├── track_shipments.rb
│ │ │ └── update_shipments.rb
│ │ └── synchronize_shipments.rb
│ ├── models
│ │ └── shipment.rb
│ ├── services
│ │ ├── shipments
│ │ │ └── create_service.rb
│ │ └── application_service.rb
│ └── singletons
│ ├── fedex_service_error.rb
│ ├── fedex_service.rb
│ ├── sns_service_error.rb
│ └── sns_service.rb
├── config
│ ├── database.yml
│ └── routes.rb
├── db
│ ├── migrate
│ │ └── 20201118205942_create_shipments.rb
│ ├── schema.rb
│ └── seeds.rb
├── lib
│ └── tasks
│ └── shipments.rake
├── spec
│ └── *
└── Gemfile