Skip to content

Instantly share code, notes, and snippets.

@thunsaker
Last active February 17, 2016 16:20
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 thunsaker/01136112c5bd03538a89 to your computer and use it in GitHub Desktop.
Save thunsaker/01136112c5bd03538a89 to your computer and use it in GitHub Desktop.
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