This project is a sample RESTful API made with Ruby and Sinatra. It makes a simple order-driver assignment.
- The web service accepts lists of driver objects and order objects, in JSON.
- Drivers have a name and a current location.
- Orders have a name, an origin location, a destination location and an indication of size (large or small.)
To do the order-driver mapping the service must consider the following rules
- A driver shouldn’t be paired with any order that is to be picked up more than five miles from their current location.
- If a driver is assigned to a large order, they can carry no additional orders.
- A driver can carry no more than three orders.
Let's make the web service up and running.
- Clone this repository
git clone [email protected]:lucasdlemos/order-driver-assignment-api.git
-
Install RVM
-
Install the gem's . Open you terminal at the repository folder and run:
gem install bundler
bundle install --without production
- Get the server up. Run the command
bundle exec rackup
This will run a server in your local machine running on port 9292 . http://localhost:9292
This section explains how to use the API describing the available calls and giving some examples of how to use them. The model is composed by two entities: driver and order.
http://localhost:9292/{entity} HTTP Method: POST
The entity's JSON must be at the body.
http://localhost:9292/{entity}s HTTP Method: POST
The JSON with an Array of entities must be at the body.
http://localhost:9292/{entity}/{entityId} HTTP Method: PUT
The entire entity's JSON, including it's id, with the changed data must be at the body
http://localhost:9292/{entity}/{id} HTTP Method: DELETE
http://localhost:9292/{entity} HTTP Method: GET
http://localhost:9292/{entity}/{entityId} HTTP Method: GET
http://localhost:9292/driver/availableForOrder/{orderId} HTTP Method: GET
http://localhost:9292/driver/{driverId}/assignOrder/{orderId} HTTP Method: GET
http://localhost:9292/driver/{driverId}/removeOrder/{orderId} HTTP Method: GET
http://localhost:9292/driver/pairWithOrders HTTP Method: GET
Let's see an example of how the API works. We will create some orders, drivers, assign drivers to orders and call the automatic order-driver pairing.
Let's create three small orders and one large order:
URL: http://localhost:9292/orders HTTP Method: POST
Body:
[
{
"name": "Two Pizzas",
"size": "small",
"origin_latitude": -22.966792,
"origin_longitude": -43.182993,
"destination_latitude": -22.956242,
"destination_longitude": -43.181255
},
{
"name": "One Burguer",
"size": "small",
"origin_latitude": -22.958985,
"origin_longitude": -43.200718,
"destination_latitude": -22.956242,
"destination_longitude": -43.181255
},
{
"name": "Ten Pizzas",
"size": "large",
"origin_latitude": -22.953623,
"origin_longitude": -43.183788,
"destination_latitude": -22.956242,
"destination_longitude": -43.181255
},
{
"name": "Four Sandwiches",
"size": "small",
"origin_latitude": -22.966033,
"origin_longitude": -43.175427,
"destination_latitude": -22.956242,
"destination_longitude": -43.181255
},
{
"name": "Two Salads - Far",
"size": "small",
"origin_latitude": -22.895442,
"origin_longitude": -43.261546,
"destination_latitude": -22.956242,
"destination_longitude": -43.181255
}
]
Try to make this request using curl:
echo '[{"name":"Two Pizzas","size":"small","origin_latitude":-22.966792,"origin_longitude":-43.182993,"destination_latitude":-22.956242,"destination_longitude":-43.181255},{"name":"One Burguer","size":"small","origin_latitude":-22.958985,"origin_longitude":-43.200718,"destination_latitude":-22.956242,"destination_longitude":-43.181255},{"name":"Ten Pizzas","size":"large","origin_latitude":-22.953623,"origin_longitude":-43.183788,"destination_latitude":-22.956242,"destination_longitude":-43.181255},{"name":"Four Sandwiches","size":"small","origin_latitude":-22.966033,"origin_longitude":-43.175427,"destination_latitude":-22.956242,"destination_longitude":-43.181255},{"name":"Two Salads","size":"small","origin_latitude":-22.895442,"origin_longitude":-43.261546,"destination_latitude":-22.956242,"destination_longitude":-43.181255}]' | curl -d @- http://localhost:9292/orders --header "Content-Type:application/json"
Let's create two drivers
URL: http://localhost:9292/drivers HTTP Method: POST
Body
[
{
"name" : "Lucas",
"latitude" : -22.964245,
"longitude" : -43.177305
},
{
"name" : "John",
"latitude" : -22.965863,
"longitude" : -43.183637
}
]
Try to make this request using curl
echo '[{"name":"Lucas","latitude":-22.964245,"longitude":-43.177305},{"name":"John","latitude":-22.965863,"longitude":-43.183637}]' | curl -d @- http://localhost:9292/drivers --header "Content-Type:application/json"
According to the API rules described on the first session there are some rules to consider a Driver as available to pick an Order. Let's see which drivers are available to pick the first order:
URL: http://localhost:9292/driver/availableForOrder/1 HTTP Method: GET
Try to make this request using curl
curl http://localhost:9292/driver/availableForOrder/1
Based on the order-driver mapping algorithm this call must return both drivers Lucas and John as available do pick the order 1.
Now we know that Lucas and John are available to pick the first order, let's assign the driver 'Lucas' to it.
URL: http://localhost:9292/driver/1/assignOrder/1 HTTP Method: GET
Try to make this request using curl
curl http://localhost:9292/driver/1/assignOrder/1
Imagine the driver Lucas wants to reject the first order, so let's remove it.
URL: http://localhost:9292/driver/1/removeOrder/1 HTTP Method: GET
Try to make this request using curl
curl http://localhost:9292/driver/1/removeOrder/1
If we want the service to automatically distribute the orders to their available drivers according to it's algorithm, make this call:
URL: http://localhost:9292/drivers/pairWithOrders HTTP Method: GET
Try to make this request using curl
curl http://localhost:9292/drivers/pairWithOrders
The algorithm maps available drivers for each order, prioritizing the oldest.
Now we have asked the service to automatically map drivers and orders, let's see the result:
curl http://localhost:9292/driver
This call must return the driver Lucas paired with the large order (Ten Pizzas) and the driver John paired with the other 3 orders.