Skip to content

Instantly share code, notes, and snippets.

@tbeseda
Last active December 19, 2015 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbeseda/5879344 to your computer and use it in GitHub Desktop.
Save tbeseda/5879344 to your computer and use it in GitHub Desktop.
Update Ducksboard slots from a Google Apps Script.

Ducksboard for Google Apps Scripts

Usage

Add Ducksboard.gs (using .js here to enable syntax hilighting) to your Apps Script project, and instantiate a new instance in your main function:

var ducks = new Ducksboard('YOUR_API_KEY');

Send the number 42 to the slot at 181242 (works for boxes, pins, guages, bars, etc.):

ducks.send_number(181242, 42);

Send an item to a timeline at 181243 with a title of "Test", content "foo bar", and my github avatar omitting the optional link parameter:

ducks.send_timeline_update(181243, 'Test', 'foo bar', 'https://secure.gravatar.com/avatar/6e585eb21989377a2669d19cca43f40d');

Send another type of update to other types of slots as documented by the Ducksboard API:

var funnel_update = {
  "value": {
    "funnel": [
      {"name": "STEP 1", "value": 1600},
      {"name": "STEP 2", "value": 1400},
      {"name": "STEP 3", "value": 1200},
      {"name": "STEP 4", "value": 900},
      {"name": "STEP 5", "value": 600},
      {"name": "STEP 6", "value": 330}
    ]
  }
}
ducks.send(181244, funnel_update);
Ducksboard = function(token) {
this.token = token;
this.api_url = 'https://push.ducksboard.com/v/'
}
Ducksboard.prototype = {
send: function(endpoint, payload) {
var options = {
method: 'post',
headers: {
'Authorization': 'Basic ' + Utilities.base64Encode(this.token + ':x', Utilities.Charset.UTF_8)
},
payload: JSON.stringify(payload)
};
return UrlFetchApp.fetch(this.api_url + endpoint, options);
},
send_timeline_update: function(slug, title, content, image, link) {
// convenience method to post a timeline update
var payload = {
value: {
title: title,
content: content,
image: image
}
};
if (typeof link != 'undefined') { payload.value.link = link }
return this.send(slug, payload);
},
send_number: function(slug, number) {
// convenience method to send just a single value
return this.send(slug, { value: number });
},
send_status_board_update: function(slug, board_data) {
// accepts data as array of objects in this format:
// {name: 'foobar', values: [1, 'baz, 42], status: 'green'}
var payload = {value: {board: board_data}};
return this.send(slug, payload);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment