-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ dist.zip | |
|
||
# Ignore installed node modules | ||
node_modules/ | ||
|
||
testpin.json | ||
# ignore pngs i create | ||
*.png | ||
|
||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// 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); | ||
} | ||
|
||
// Export | ||
module.exports.insertUserPin = insertUserPin; | ||
module.exports.deleteUserPin = deleteUserPin; | ||
module.exports.insertSharedPin = insertSharedPin; | ||
module.exports.deleteSharedPin = deleteSharedPin; |