buycraft
v1.0.4
Published
Simple node.js wrapper for the Buycraft API
Downloads
17
Maintainers
Readme
Buycraft for Node.js
Simple node.js wrapper for Buycraft's HTTP API.
Table of Contents
Installation
To use buycraft
in your projects execute the following command from the command line:
$ npm install --save buycraft
Usage
To use the module simply require
it at the top of any script you need it in.
var buycraft = require('buycraft');
You must provide a "secret key" in order to use the API, you can find out how to get one here. You don't require a server to do this, however using this would be pretty pointless if you don't have one. :wink:
You can set the secret key simply by calling the setKey
method as follows:
buycraft.setKey('foo_bar');
Please bear in mind there's minimal verification for keys. If you're experiencing issues then consider double checking everything works outside of your application.
Configuring HTTPS
I'm personally fond of HTTPS, though if you want to switch to plain ol' HTTP then it's as simple as this:
buycraft.setSecure(false);
Fetching Categories
If you fancy printing a list of every category to the console then you could use the following snippet to achieve this.
buycraft.getCategories(function(categories) {
categories.forEach(function(category) {
console.log('Category: ' + category.name);
});
});
Fetching Packages
Fetching packages is no problem either, you can do this using code that's mostly the same as fetching categories.
buycraft.getPackages(function(packages) {
packages.forEach(function(package) {
console.log('Package: ');
console.log('\t' + package.name);
console.log('\t' + package.category);
console.log('\t' + package.description);
}
});
Fetching Payments
Need to know how much money Notch has spent on your server? Easy.
var config = {
ign: 'Notch'
};
var process = function(payments) {
var spent = 0.00;
payments.forEach(function(payment) {
spent += parseFloat(payment.price).toFixed(2);
});
return spent;
};
buycraft.getPayments(config, function(payments) {
console.log('Notch has spent $' + process(payments) + ' on your server!');
});
/*
* "What if I want to know much everyone has spent?"
* Got you covered, though I'm unsure how far this snippet goes back as my buycraft account is less than 24 hours old.
*/
buycraft.getPayments(null, function(payments) {
console.log('People have spent $' + process(payments) + ' on your server, wow!');
});
Documentation
"Proper" documentation (generated by JSDoc) is available here, it should be noted that this is currently a work in progress though.
If you wish to generate the documentation files locally then you may execute the following commands:
$ npm install -g jsdoc
$ jsdoc ./lib/
If you happen to use IntelliJ (or another IDE/Editor with JSDoc support) then you will find that
the code completion tool will include each property an object has, eg: the name
property of a package.
Todo
- Figure out if it's possible to capture a PHP session ID and CSRF token then re-use, would allow editing of packages from third party websites but requires a user to login first.