Skip to content

Commit

Permalink
Add Vehicles API (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
maful authored Oct 20, 2021
1 parent e31387b commit 6ee7146
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,32 @@ client.drivers.create(driver: {name: "Yolo", dob: "1990-09-09"})
client.drivers.update(driver_id: driver_id, driver: {name: "Yolo update"})
```

### Vehicles

```ruby
# list vehicles
client.vehicles.list
# get a vehicle details
client.vehicles.retrieve(vehicle_id: vehicle_id)
# create vehicle
client.vehicles.create(vehicle: {plate_number: "SK1212", model: "Honda", speed: 50})
# update vehicle
client.vehicles.update(vehicle_id: vehicle_id, vehicle: {model: "Mercedes", speed: 70})
```

## TO DO

- [x] Add API Documentation ([#2](https://github.com/maful/versafleet-ruby/pull/2))
- [ ] Add Docker support
- [ ] Support to All VersaFleet API endpoints.
- [x] Add Jobs V2 API
- [x] Add Tasks API
- [ ] Non-Authenticated Tracking API
- [ ] Add Runsheets API
- [x] Add Drivers API ([#1](https://github.com/maful/versafleet-ruby/pull/1))
- [x] Add Vehicles API ([#4](https://github.com/maful/versafleet-ruby/pull/4))
- [ ] Add Customers API
- [ ] Add Account API

## 🙏 Contributing

Expand Down
2 changes: 2 additions & 0 deletions lib/versafleet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ module Versafleet
autoload :JobsResource, "versafleet/resources/jobs"
autoload :TasksResource, "versafleet/resources/tasks"
autoload :DriversResource, "versafleet/resources/drivers"
autoload :VehiclesResource, "versafleet/resources/vehicles"

# Classes used to return a nicer object wrapping the response data
autoload :Job, "versafleet/objects/job"
autoload :Task, "versafleet/objects/task"
autoload :Driver, "versafleet/objects/driver"
autoload :Vehicle, "versafleet/objects/vehicle"
end
7 changes: 7 additions & 0 deletions lib/versafleet/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def drivers
DriversResource.new(self)
end

# Vehicles Resource instance
#
# @return [VehiclesResource]
def vehicles
VehiclesResource.new(self)
end

# Initializes a new Faraday connection
#
# @return [Faraday::Connection]
Expand Down
4 changes: 4 additions & 0 deletions lib/versafleet/objects/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Versafleet
class Vehicle < Object
end
end
67 changes: 67 additions & 0 deletions lib/versafleet/resources/vehicles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module Versafleet
class VehiclesResource < Resource
# List All Vehicles
#
# == Examples:
#
# client.vehicles.list
# # set per page to 20
# client.vehicles.list(per_page: 20)
# # move to page 2
# client.vehicles.list(page: 2, per_page: 20)
#
# {https://versafleet.docs.apiary.io/#reference/0/vehicles-api/list-all-vehicles VersaFleet API}
#
# @param params [Hash] the filter query
# @return [Collection]
def list(**params)
response = get_request("vehicles", params: params)
Collection.from_response(response, key: "vehicles", type: Vehicle)
end

# Get Vehicle details
#
# == Examples:
#
# client.vehicles.retrieve(vehicle_id: 123)
#
# {https://versafleet.docs.apiary.io/#reference/0/vehicles-api/show-a-vehicle VersaFleet API}
#
# @param vehicle_id [Integer] Vehicle ID
# @return [Vehicle]
def retrieve(vehicle_id:)
Vehicle.new get_request("vehicles/#{vehicle_id}").body.dig("vehicle")
end

# Create a Vehicle
#
# == Examples:
#
# client.vehicles.create(vehicle: {plate_number: "SK1212", model: "Honda", speed: 50})
#
# {https://versafleet.docs.apiary.io/#reference/0/vehicles-api/create-a-vehicle VersaFleet API}
#
# @param vehicle [Hash] Vehicle request payload
# @return [Vehicle]
def create(vehicle:)
payload = {vehicle: vehicle}
Vehicle.new post_request("vehicles", body: payload).body.dig("vehicle")
end

# Update Vehicle
#
# == Examples:
#
# client.vehicles.update(vehicle_id: 123, vehicle: {model: "Mercedes", speed: 70})
#
# {https://versafleet.docs.apiary.io/#reference/0/vehicles-api/update-a-vehicle VersaFleet API}
#
# @param vehicle_id [Integer] Vehicle ID
# @param vehicle [Hash] Vehicle request payload
# @return [Vehicle]
def update(vehicle_id:, vehicle:)
payload = {vehicle: vehicle}
Vehicle.new put_request("vehicles/#{vehicle_id}", body: payload).body.dig("vehicle")
end
end
end
20 changes: 20 additions & 0 deletions test/fixtures/vehicles/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"vehicle": {
"id": 3,
"guid": "VH3360002152",
"plate_number": "KJ111",
"status": "available",
"cargo_load": null,
"model": "Honda",
"category": null,
"ownership_date": null,
"registration_date": null,
"insurance_expiry": null,
"tax_expiry": null,
"speed": 100,
"volume": null,
"custom_fields": [],
"skills": [],
"vehicle_parts": []
}
}
45 changes: 45 additions & 0 deletions test/fixtures/vehicles/list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"vehicles": [
{
"id": 1,
"guid": "VH3360002151",
"plate_number": "SK1212",
"status": "available",
"cargo_load": null,
"model": "Mercedes",
"category": null,
"ownership_date": null,
"registration_date": null,
"insurance_expiry": null,
"tax_expiry": null,
"speed": 70,
"volume": null,
"custom_fields": [],
"skills": [],
"vehicle_parts": []
},
{
"id": 2,
"guid": "VH3360002150",
"plate_number": "BA 1209 11",
"status": "available",
"cargo_load": null,
"model": null,
"category": null,
"ownership_date": null,
"registration_date": null,
"insurance_expiry": null,
"tax_expiry": null,
"speed": 100,
"volume": null,
"custom_fields": [],
"skills": [],
"vehicle_parts": []
}
],
"meta": {
"page": 1,
"per_page": 20,
"total": 2
}
}
20 changes: 20 additions & 0 deletions test/fixtures/vehicles/retrieve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"vehicle": {
"id": 1,
"guid": "VH3360002151",
"plate_number": "SK1212",
"status": "available",
"cargo_load": null,
"model": "Mercedes",
"category": null,
"ownership_date": null,
"registration_date": null,
"insurance_expiry": null,
"tax_expiry": null,
"speed": 70,
"volume": null,
"custom_fields": [],
"skills": [],
"vehicle_parts": []
}
}
20 changes: 20 additions & 0 deletions test/fixtures/vehicles/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"vehicle": {
"id": 3,
"guid": "VH3360002152",
"plate_number": "KJ111",
"status": "available",
"cargo_load": null,
"model": "Honda",
"category": null,
"ownership_date": null,
"registration_date": null,
"insurance_expiry": null,
"tax_expiry": null,
"speed": 40,
"volume": null,
"custom_fields": [],
"skills": [],
"vehicle_parts": []
}
}
46 changes: 46 additions & 0 deletions test/versafleet/resources/vehicles_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "test_helper"

class VehiclesResourceTest < Minitest::Test
def test_list
stub = stub_request("vehicles", response: stub_response(fixture: "vehicles/list"))
client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub)
vehicles = client.vehicles.list

assert_equal Versafleet::Collection, vehicles.class
assert_equal Versafleet::Vehicle, vehicles.data.first.class
assert_equal 2, vehicles.total
end

def test_retrieve
vehicle_id = 1
stub = stub_request("vehicles/#{vehicle_id}", response: stub_response(fixture: "vehicles/retrieve"))
client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub)
vehicle = client.vehicles.retrieve(vehicle_id: vehicle_id)

assert_equal Versafleet::Vehicle, vehicle.class
assert_equal vehicle_id, vehicle.id
end

def test_create
body = {plate_number: "KJ111", model: "Honda", speed: 100}
stub = stub_request("vehicles", method: :post, body: {vehicle: body}, response: stub_response(fixture: "vehicles/create"))
client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub)
vehicle = client.vehicles.create(vehicle: body)

assert_equal Versafleet::Vehicle, vehicle.class
assert_equal body[:plate_number], vehicle.plate_number
end

def test_update
vehicle_id = 3
body = {speed: 40}
stub = stub_request("vehicles/#{vehicle_id}", method: :put, body: {vehicle: body}, response: stub_response(fixture: "vehicles/update"))
client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub)
vehicle = client.vehicles.update(vehicle_id: vehicle_id, vehicle: body)

assert_equal Versafleet::Vehicle, vehicle.class
assert_equal body[:speed], vehicle.speed
end
end

0 comments on commit 6ee7146

Please sign in to comment.