How to send a vultr account balance report in Ruby

Vultr balance

Vultr is a great VPS provider. I have a couple of VMs hosted in Vultr. However, according to Vultr T&C: “Failure to make payment of Fees for Services when due may result in the suspension or termination of Services.” It would be nice to have an alert when the Vultr account balance reaches a threshold. Here is the implementation in Ruby.

I prefer to have a monthly report no matter if the threshold has been reached. However, if you want to have an email alert when the threshold is reached, the following code can be easily modified to do that.

Install gems

1
2
3
gem install faraday
gem install json
gem install gmail

The Ruby code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'faraday'
require 'json'
require 'gmail'
conn = Faraday.new(:url => 'https://api.vultr.com') do |c|
c.use Faraday::Request::UrlEncoded
c.use Faraday::Adapter::NetHttp
end
response = conn.get do |req|
req.url '/v1/account/info'
req.headers['API-Key'] = 'your API key'
end
responseStr = response.body
responseJson = JSON.parse(responseStr)
balance = responseJson['balance']
pending_charges = responseJson['pending_charges']
user = "your gmail"
pass = "your gmail password"
gmail = Gmail.connect(user, pass)
email = gmail.compose do
to "the email address receiving reports"
subject "Your Vultr balance"
body "Balance: " + balance + "\n" + "Pending charges: " + pending_charges
end
email.deliver!
Share