diff --git a/README.md b/README.md index 92ccffc..f0164a4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/versafleet.rb b/lib/versafleet.rb index d0f65d5..35fe2a8 100644 --- a/lib/versafleet.rb +++ b/lib/versafleet.rb @@ -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 diff --git a/lib/versafleet/client.rb b/lib/versafleet/client.rb index fc9afb8..c58818c 100644 --- a/lib/versafleet/client.rb +++ b/lib/versafleet/client.rb @@ -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] diff --git a/lib/versafleet/objects/vehicle.rb b/lib/versafleet/objects/vehicle.rb new file mode 100644 index 0000000..a3c442a --- /dev/null +++ b/lib/versafleet/objects/vehicle.rb @@ -0,0 +1,4 @@ +module Versafleet + class Vehicle < Object + end +end diff --git a/lib/versafleet/resources/vehicles.rb b/lib/versafleet/resources/vehicles.rb new file mode 100644 index 0000000..4d54d14 --- /dev/null +++ b/lib/versafleet/resources/vehicles.rb @@ -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 diff --git a/test/fixtures/vehicles/create.json b/test/fixtures/vehicles/create.json new file mode 100644 index 0000000..6583bb6 --- /dev/null +++ b/test/fixtures/vehicles/create.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/vehicles/list.json b/test/fixtures/vehicles/list.json new file mode 100644 index 0000000..e8f9623 --- /dev/null +++ b/test/fixtures/vehicles/list.json @@ -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 + } +} \ No newline at end of file diff --git a/test/fixtures/vehicles/retrieve.json b/test/fixtures/vehicles/retrieve.json new file mode 100644 index 0000000..fc4165b --- /dev/null +++ b/test/fixtures/vehicles/retrieve.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/fixtures/vehicles/update.json b/test/fixtures/vehicles/update.json new file mode 100644 index 0000000..57bfc5e --- /dev/null +++ b/test/fixtures/vehicles/update.json @@ -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": [] + } +} \ No newline at end of file diff --git a/test/versafleet/resources/vehicles_test.rb b/test/versafleet/resources/vehicles_test.rb new file mode 100644 index 0000000..f9d2088 --- /dev/null +++ b/test/versafleet/resources/vehicles_test.rb @@ -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