In this article I'll show you how to do a simple task : compute your revenue by customer. It'll be easy. I promise.
Listing our invoices
Let's create a small client class. It has only an API key instance variable. The HTTParty module has been included so we have access to all its goodness.
The base_uri is set to DI's endpoint URI :
Now we will create two methods : the first to retrieve all invoices, the second to retieve all companies (customers). We need to be able to get the companies because invoices don't come with their company name (at the moment).
Note that I set the per_page to 10000 in order to get all invoices in raw. And we can now use our client to get two arrays with the invoices and the companies :
You noticed that the secret key is stored in a environment variable.
As we will also need the company names, let's create a Hash in which the key is the company number and where the value is the name :
Grouping invoices per customer
Ruby has a simple built-in method to group a collection against a block :
Preparing the data
We will need the total invoiced amount :
After that we keep only the minimum information : the company name, the total invoiced amount for this company and the percentage of the total it represents. An Array of Hashes will be perfect for that :
At the end we sort results by descending order of invoiced amount.
Building a CSV
The standard Ruby CSV library makes that an easy task too :
If you run the script, you will get a three-column CSV file. You can of course find the code on Github.
I have heard that Direct Invoice will release some tagging features soon : revenue per tag would be great metrics to implement too !