diff --git a/README.md b/README.md index 4f5ab98..b059bba 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,19 @@ client.customers.create(customer: {name: "John Doe", email: "well@example.com"}) client.customers.update(customer_id: customer_id, customer: {name: "Bourne"}) ``` +### Account + +```ruby +# get account details +client.account.retrieve(account_id: account_id) +# create an account +client.account.create(customer_id: customer_id, account: {name: "Johhny", email: "john@example.com"}) +# update account +client.account.update(account_id: account_id, account: {name: "Bobby"}) +# delete an account +client.account.delete(account_id: account_id) +``` + ## TO DO - [x] Add API Documentation ([#2](https://github.com/maful/versafleet-ruby/pull/2)) @@ -151,7 +164,7 @@ client.customers.update(customer_id: customer_id, customer: {name: "Bourne"}) - [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)) - [x] Add Customers API ([#6](https://github.com/maful/versafleet-ruby/pull/6)) - - [ ] Add Account API + - [x] Add Account API ([#8](https://github.com/maful/versafleet-ruby/pull/8)) ## 🙏 Contributing diff --git a/lib/versafleet.rb b/lib/versafleet.rb index 2fb3a6e..bf63978 100644 --- a/lib/versafleet.rb +++ b/lib/versafleet.rb @@ -16,6 +16,7 @@ module Versafleet autoload :VehiclesResource, "versafleet/resources/vehicles" autoload :RunsheetsResource, "versafleet/resources/runsheets" autoload :CustomersResource, "versafleet/resources/customers" + autoload :AccountResource, "versafleet/resources/account" # Classes used to return a nicer object wrapping the response data autoload :Job, "versafleet/objects/job" @@ -24,4 +25,5 @@ module Versafleet autoload :Vehicle, "versafleet/objects/vehicle" autoload :Runsheet, "versafleet/objects/runsheet" autoload :Customer, "versafleet/objects/customer" + autoload :Account, "versafleet/objects/account" end diff --git a/lib/versafleet/client.rb b/lib/versafleet/client.rb index 545fb1c..234838a 100644 --- a/lib/versafleet/client.rb +++ b/lib/versafleet/client.rb @@ -73,6 +73,13 @@ def customers CustomersResource.new(self) end + # Account Resource instance + # + # @return [AccountResource] + def account + AccountResource.new(self) + end + # Initializes a new Faraday connection # # @return [Faraday::Connection] diff --git a/lib/versafleet/objects/account.rb b/lib/versafleet/objects/account.rb new file mode 100644 index 0000000..83262e8 --- /dev/null +++ b/lib/versafleet/objects/account.rb @@ -0,0 +1,4 @@ +module Versafleet + class Account < Object + end +end diff --git a/lib/versafleet/resource.rb b/lib/versafleet/resource.rb index 0d0b9d4..eec1d45 100644 --- a/lib/versafleet/resource.rb +++ b/lib/versafleet/resource.rb @@ -20,6 +20,10 @@ def put_request(url, body:, headers: {}) handle_response client.connection.put(url, body, headers) end + def delete_request(url, params: {}, headers: {}) + handle_response client.connection.delete(url, params, headers) + end + def handle_response(response) case response.status when 404 diff --git a/lib/versafleet/resources/account.rb b/lib/versafleet/resources/account.rb new file mode 100644 index 0000000..d8041b2 --- /dev/null +++ b/lib/versafleet/resources/account.rb @@ -0,0 +1,62 @@ +module Versafleet + class AccountResource < Resource + # Get Account details + # + # == Example: + # + # client.account.retrieve(account_id: 123) + # + # {https://versafleet.docs.apiary.io/#reference/0/account-api/view-a-account VersaFleet API} + # + # @param account_id [Integer|String] Account ID or Account GUID + # @return [Account] + def retrieve(account_id:) + Account.new get_request("billing_accounts/#{account_id}").body.dig("billing_account") + end + + # Create an Account + # + # == Example: + # + # client.account.create(customer_id: 123, account: {name: "Johhny", email: "john@example.com"}) + # + # {https://versafleet.docs.apiary.io/#reference/0/account-api/create-account VersaFleet API} + # + # @param customer_id [Integer] Customer ID + # @param account [Hash] Account request payload + # @return [Account] + def create(customer_id:, account:) + payload = {billing_account: account} + Account.new post_request("billing_accounts/#{customer_id}", body: payload).body.dig("billing_account") + end + + # Update an Account + # + # == Example: + # + # client.account.update(account_id: 123, account: {name: "Johhny"}) + # + # {https://versafleet.docs.apiary.io/#reference/0/account-api VersaFleet API} + # + # @param account_id [Integer] Account ID + # @param account [Hash] Account request payload + # @return [Account] + def update(account_id:, account:) + payload = {billing_account: account} + Account.new put_request("billing_accounts/#{account_id}", body: payload).body.dig("billing_account") + end + + # Delete an Account + # + # == Example: + # + # client.account.delete(account_id: 123) + # + # {https://versafleet.docs.apiary.io/#reference/0/account-api/delete-an-account VersaFleet API} + # + # @param account_id [Integer] Account ID + def delete(account_id:) + delete_request("billing_accounts/#{account_id}").body + end + end +end diff --git a/test/fixtures/account/create.json b/test/fixtures/account/create.json new file mode 100644 index 0000000..0205b79 --- /dev/null +++ b/test/fixtures/account/create.json @@ -0,0 +1,11 @@ +{ + "billing_account": { + "id": 1, + "guid": "AC336001", + "name": "Danny", + "email": "danny@example.com", + "contact_person": null, + "contact_number": null, + "addresses": [] + } +} \ No newline at end of file diff --git a/test/fixtures/account/delete.json b/test/fixtures/account/delete.json new file mode 100644 index 0000000..2f8ea45 --- /dev/null +++ b/test/fixtures/account/delete.json @@ -0,0 +1,6 @@ +{ + "message": [ + "success" + ], + "accessing_time": 1635582764 +} \ No newline at end of file diff --git a/test/fixtures/account/retrieve.json b/test/fixtures/account/retrieve.json new file mode 100644 index 0000000..0205b79 --- /dev/null +++ b/test/fixtures/account/retrieve.json @@ -0,0 +1,11 @@ +{ + "billing_account": { + "id": 1, + "guid": "AC336001", + "name": "Danny", + "email": "danny@example.com", + "contact_person": null, + "contact_number": null, + "addresses": [] + } +} \ No newline at end of file diff --git a/test/fixtures/account/update.json b/test/fixtures/account/update.json new file mode 100644 index 0000000..60272ba --- /dev/null +++ b/test/fixtures/account/update.json @@ -0,0 +1,11 @@ +{ + "billing_account": { + "id": 1, + "guid": "AC336001", + "name": "Nany", + "email": "danny@example.com", + "contact_person": null, + "contact_number": null, + "addresses": [] + } +} \ No newline at end of file diff --git a/test/versafleet/resources/account_test.rb b/test/versafleet/resources/account_test.rb new file mode 100644 index 0000000..1ebb61b --- /dev/null +++ b/test/versafleet/resources/account_test.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require "test_helper" + +class AccountResourceTest < Minitest::Test + def test_retrieve + account_id = 1 + stub = stub_request("billing_accounts/#{account_id}", response: stub_response(fixture: "account/retrieve")) + client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub) + account = client.account.retrieve(account_id: account_id) + + assert_equal Versafleet::Account, account.class + assert_equal account_id, account.id + end + + def test_create + customer_id = 1 + body = {name: "Danny", email: "danny@example.com"} + stub = stub_request("billing_accounts/#{customer_id}", method: :post, body: {billing_account: body}, response: stub_response(fixture: "account/create")) + client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub) + account = client.account.create(customer_id: customer_id, account: body) + + assert_equal Versafleet::Account, account.class + assert_equal body[:name], account.name + assert_equal body[:email], account.email + end + + def test_update + account_id = 1 + body = {name: "Nany"} + stub = stub_request("billing_accounts/#{account_id}", method: :put, body: {billing_account: body}, response: stub_response(fixture: "account/update")) + client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub) + account = client.account.update(account_id: account_id, account: body) + + assert_equal Versafleet::Account, account.class + assert_equal body[:name], account.name + end + + def test_delete + account_id = 1 + stub = stub_request("billing_accounts/#{account_id}", method: :delete, response: stub_response(fixture: "account/delete")) + client = Versafleet::Client.new(client_id: "fake", client_secret: "fake", adapter: :test, stubs: stub) + response = client.account.delete(account_id: account_id) + + assert_equal response["message"].first, "success" + end +end