Sunny thanks for the reply
I did it :)
But to bypass the API transaction limits I had to put in a delay, which turns the routine a little slow ( I'm refreshing 1000 records )
Also, don't quite know how to auto delete all records prior to refresh the information, since this code will add another set of records to the existing ones, and what I need is to replace them all.
Another step would be to run this every hour, don't think Knack can handle this level of customization yet
//CODE#1 Button to get coinmarketcap values via Json
$(document).on('knack-scene-render.scene_1', function(event, scene) {
$('#view_1 .view-header').after('<div style="padding:5px"><a href="#" id="refreshcmc" class="kn-button">Update From Coinmarketcap</a></div>');
$('#refreshcmc').click(function(event) {
event.preventDefault();
// get data from coinmarketcap (Json)
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'https://api.coinmarketcap.com/v1/ticker/?limit=1000', true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
// .open will NOT return a value but simply returns undefined in async mode so use a callback
callback(xobj.responseText);
}
}
xobj.send(null);
}
loadJSON(function(response) {
// Do Something with the response e.g.
jsonresponse = JSON.parse(response);
var data = {};
// set the delay to prevent hitting Knack API rate limit (milliseconds)
var myDelay = 100;
//Knack.showSpinner();
for (var i = 0, len = jsonresponse.length; i < len; i++) {
data.field_6 = jsonresponse[i].id;
data.field_7 = jsonresponse[i].name;
data.field_8 = jsonresponse[i].symbol;
data.field_9 = jsonresponse[i].rank;
data.field_10 = jsonresponse[i].price_usd;
data.field_11 = jsonresponse[i].price_btc;
//data.field_12 = jsonresponse[0].24h_volume_usd;
data.field_13 = jsonresponse[i].market_cap_usd;
data.field_14 = jsonresponse[i].available_supply;
data.field_18 = jsonresponse[i].total_supply;
data.field_19 = jsonresponse[i].max_supply;
data.field_15 = jsonresponse[i].percent_change_1h;
data.field_16 = jsonresponse[i].percent_change_24h;
data.field_17 = jsonresponse[i].percent_change_7d;
data.field_20 = jsonresponse[i].last_updated;
setTimeout(runapi(data), myDelay);
}; // for
// Knack.hideSpinner();
alert('All information refreshed');
function runapi(data) {
$.ajax({
url: "https://us-api.knack.com/v1/objects/object_2/records/",
type: "POST",
headers: {
"X-Knack-Application-Id": "***********",
"X-Knack-REST-API-Key": "***********"
},
data: data,
success: function(response) {
//console.log('Checks updated!!!');
}
});
}
});
});
});