Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Account API #8

Merged
merged 1 commit into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ client.customers.create(customer: {name: "John Doe", email: "[email protected]"})
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: "[email protected]"})
# 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))
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions lib/versafleet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
7 changes: 7 additions & 0 deletions lib/versafleet/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions lib/versafleet/objects/account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Versafleet
class Account < Object
end
end
4 changes: 4 additions & 0 deletions lib/versafleet/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions lib/versafleet/resources/account.rb
Original file line number Diff line number Diff line change
@@ -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: "[email protected]"})
#
# {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
11 changes: 11 additions & 0 deletions test/fixtures/account/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"billing_account": {
"id": 1,
"guid": "AC336001",
"name": "Danny",
"email": "[email protected]",
"contact_person": null,
"contact_number": null,
"addresses": []
}
}
6 changes: 6 additions & 0 deletions test/fixtures/account/delete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"message": [
"success"
],
"accessing_time": 1635582764
}
11 changes: 11 additions & 0 deletions test/fixtures/account/retrieve.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"billing_account": {
"id": 1,
"guid": "AC336001",
"name": "Danny",
"email": "[email protected]",
"contact_person": null,
"contact_number": null,
"addresses": []
}
}
11 changes: 11 additions & 0 deletions test/fixtures/account/update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"billing_account": {
"id": 1,
"guid": "AC336001",
"name": "Nany",
"email": "[email protected]",
"contact_person": null,
"contact_number": null,
"addresses": []
}
}
47 changes: 47 additions & 0 deletions test/versafleet/resources/account_test.rb
Original file line number Diff line number Diff line change
@@ -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: "[email protected]"}
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