tbeseda
6/27/2013 - 7:00 PM

Update Ducksboard slots from a Google Apps Script.

Update Ducksboard slots from a Google Apps Script.

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);
  }
}

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);