-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module Versafleet | ||
class Vehicle < Object | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |