Created
June 8, 2015 20:00
-
-
Save C-D-Lewis/81254414c15e6561ac33 to your computer and use it in GitHub Desktop.
Timeline library for PebbleKit JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************* 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); | |
} | |
/** | |
* 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); | |
} | |
/** | |
* 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); | |
} | |
/** | |
* 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); | |
} | |
/************************************* App ************************************/ | |
Pebble.addEventListener('ready', function() { | |
console.log('PebbleKit JS ready!'); | |
var pinWithAction = { | |
"id": "example-pin-generic-d2sd3d5bdsd", | |
"time": "2015-05-08T01:30:00Z", | |
"layout": { | |
"type": "genericPin", | |
"title": "Args Pin", | |
"tinyIcon": "system://images/TIMELINE_FLAG" | |
}, | |
"actions": [ | |
{ | |
"title": "Test Args", | |
"type": "openWatchApp", | |
"launchCode": 42 | |
} | |
] | |
}; | |
insertUserPin(pinWithAction, function() { }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment