Advertisement
Guest User

pebble-js-app.js

a guest
Sep 17th, 2015
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Pebble.addEventListener('ready', function() {
  2.   console.log('PebbleKit JS ready!');
  3.  
  4.   // An hour ahead
  5.   var date = new Date();
  6.   date.setHours(date.getHours() + 1);
  7.  
  8.   // Create the pin
  9.   var pin = {
  10.     "id": "pin-" + Math.round((Math.random() * 100000)),
  11.     "time": date.toISOString(),
  12.     "layout": {
  13.       "type": "genericPin",
  14.       "title": "Example Pin",
  15.       "tinyIcon": "system://images/SCHEDULED_EVENT"
  16.     }
  17.   };
  18.  
  19.   console.log('Inserting pin in the future: ' + JSON.stringify(pin));
  20.  
  21.   insertUserPin(pin, function(responseText) {
  22.     console.log('Result: ' + responseText);
  23.   });
  24. });
  25.  
  26. /******************************* timeline lib *********************************/
  27.  
  28. // The timeline public URL root
  29. var API_URL_ROOT = 'https://timeline-api.getpebble.com/';
  30.  
  31. /**
  32.  * Send a request to the Pebble public web timeline API.
  33.  * @param pin The JSON pin to insert. Must contain 'id' field.
  34.  * @param type The type of request, either PUT or DELETE.
  35.  * @param callback The callback to receive the responseText after the request has completed.
  36.  */
  37. function timelineRequest(pin, type, callback) {
  38.   // User or shared?
  39.   var url = API_URL_ROOT + 'v1/user/pins/' + pin.id;
  40.  
  41.   // Create XHR
  42.   var xhr = new XMLHttpRequest();
  43.   xhr.onload = function () {
  44.     console.log('timeline: response received: ' + this.responseText);
  45.     callback(this.responseText);
  46.   };
  47.   xhr.open(type, url);
  48.  
  49.   // Get token
  50.   Pebble.getTimelineToken(function(token) {
  51.     // Add headers
  52.     xhr.setRequestHeader('Content-Type', 'application/json');
  53.     xhr.setRequestHeader('X-User-Token', '' + token);
  54.  
  55.     // Send
  56.     xhr.send(JSON.stringify(pin));
  57.     console.log('timeline: request sent.');
  58.   }, function(error) { console.log('timeline: error getting timeline token: ' + error); });
  59. }
  60.  
  61. /**
  62.  * Insert a pin into the timeline for this user.
  63.  * @param pin The JSON pin to insert.
  64.  * @param callback The callback to receive the responseText after the request has completed.
  65.  */
  66. function insertUserPin(pin, callback) {
  67.   timelineRequest(pin, 'PUT', callback);
  68. }
  69.  
  70. /**
  71.  * Delete a pin from the timeline for this user.
  72.  * @param pin The JSON pin to delete.
  73.  * @param callback The callback to receive the responseText after the request has completed.
  74.  */
  75. function deleteUserPin(pin, callback) {
  76.   timelineRequest(pin, 'DELETE', callback);
  77. }
  78.  
  79. /***************************** end timeline lib *******************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement