Skip to content

Instantly share code, notes, and snippets.

@thunsaker
Last active Feb 17, 2016
Embed
What would you like to do?
Small JS sample to push pins from inside a PebbleKit JS app
/******************************* timeline lib *********************************/
// The timeline public URL root
var API_URL_ROOT = 'https://timeline-api.getpebble.com/';
/**
* Send a request to the Pebble public web timeline API.
* @param pin The JSON pin to insert. Must contain 'id' field.
* @param type The type of request, either PUT or DELETE.
* @param topics Array of topics if a shared pin, 'null' otherwise.
* @param apiKey Timeline API key for this app, available from dev-portal.getpebble.com
* @param callback The callback to receive the responseText after the request has completed.
*/
function timelineRequest(pin, type, topics, apiKey, callback) {
// User or shared?
var url = API_URL_ROOT + 'v1/' + ((topics != null) ? 'shared/' : 'user/') + 'pins/' + pin.id;
// Create XHR
var xhr = new XMLHttpRequest();
xhr.onload = function () {
console.log('timeline: response received: ' + this.responseText);
callback(this.responseText);
};
xhr.open(type, url);
// Set headers
xhr.setRequestHeader('Content-Type', 'application/json');
if(topics != null) {
xhr.setRequestHeader('X-Pin-Topics', '' + topics.join(','));
xhr.setRequestHeader('X-API-Key', '' + apiKey);
}
// Get token
Pebble.getTimelineToken(function(token) {
// Add headers
xhr.setRequestHeader('X-User-Token', '' + token);
// Send
xhr.send(JSON.stringify(pin));
console.log('timeline: request sent.');
}, function(error) { console.log('timeline: error getting timeline token: ' + error); });
}
/**
* Insert a pin into the timeline for this user.
* @param pin The JSON pin to insert.
* @param callback The callback to receive the responseText after the request has completed.
*/
function insertUserPin(pin, callback) {
timelineRequest(pin, 'PUT', null, null, callback);
}
module.exports.insertUserPin = insertUserPin;
/**
* Delete a pin from the timeline for this user.
* @param pin The JSON pin to delete.
* @param callback The callback to receive the responseText after the request has completed.
*/
function deleteUserPin(pin, callback) {
timelineRequest(pin, 'DELETE', null, null, callback);
}
module.exports.deleteUserPin = deleteUserPin;
/**
* Insert a pin into the timeline for these topics.
* @param pin The JSON pin to insert.
* @param topics Array of topics to insert pin to.
* @param apiKey Timeline API key for this app, available from dev-portal.getpebble.com
* @param callback The callback to receive the responseText after the request has completed.
*/
function insertSharedPin(pin, topics, apiKey, callback) {
timelineRequest(pin, 'PUT', topics, apiKey, callback);
}
module.exports.insertSharedPin = insertSharedPin;
/**
* Delete a pin from the timeline for these topics.
* @param pin The JSON pin to delete.
* @param topics Array of topics to delete pin from.
* @param apiKey Timeline API key for this app, available from dev-portal.getpebble.com
* @param callback The callback to receive the responseText after the request has completed.
*/
function deleteSharedPin(pin, topics, apiKey, callback) {
timelineRequest(pin, 'DELETE', topics, apiKey, callback);
}
module.exports.deleteSharedPin = deleteSharedPin;
/****************************** end timeline lib ******************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment