From aef733dcc4400d88b21bcaf2420b1891ebf94de7 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 11:35:20 -0400 Subject: [PATCH 1/8] ADD deletePlot method and example --- examples/delete-plot.js | 8 ++++++++ index.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 examples/delete-plot.js diff --git a/examples/delete-plot.js b/examples/delete-plot.js new file mode 100644 index 0000000..a26b0e3 --- /dev/null +++ b/examples/delete-plot.js @@ -0,0 +1,8 @@ +'use strict'; + +var plotly = require('../.')('username','apiKey'); + +plotly.deletePlot('888', function (err, msg) { + if (err) console.log(err); + else console.log(msg); +}); diff --git a/index.js b/index.js index 951a2f2..adc3e95 100644 --- a/index.js +++ b/index.js @@ -236,6 +236,47 @@ Plotly.prototype.getImage = function (figure, opts, callback) { req.end(); }; +Plotly.prototype.deletePlot = function (fid, callback) { + if (!callback) callback = function () {}; + + var self = this; + + // Create the base64 authstring from buffer + var encodedAPIAuth = new Buffer(this.username + ':' + this.apiKey).toString('base64'); + + var options = { + host: 'api.plot.ly', + port: this.port, + path: '/v2/files/' + this.username + ':' + fid + '/trash', + method: 'POST', + agent: false, + withCredentials: true, + headers: { + 'Plotly-Client-Platform': 'nodejs ' + this.version, + 'authorization': 'Basic ' + encodedAPIAuth + } + }; + + var req = https.request(options, function (res) { + parseRes(res, function (err, body) { + + if (res.statusCode === 200) { + var msg = 'Successfully deleted plot: ' + self.username + ':' + fid; + callback(null, msg); + } else { + callback(body); // Pass out the error message from the backend + } + + }); + }); + + req.on('error', function (err) { + callback(err); + }); + + req.end(); +}; + // response parse helper fn function parseRes (res, cb) { var body = ''; From dc1f54116281dab97bf3aabe38fd7ebd51b95648 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 11:42:41 -0400 Subject: [PATCH 2/8] if theres an error, build up an error obj from the res and body --- index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index adc3e95..772f82c 100644 --- a/index.js +++ b/index.js @@ -261,10 +261,19 @@ Plotly.prototype.deletePlot = function (fid, callback) { parseRes(res, function (err, body) { if (res.statusCode === 200) { + var msg = 'Successfully deleted plot: ' + self.username + ':' + fid; callback(null, msg); + } else { - callback(body); // Pass out the error message from the backend + + var errObj = { + statusCode: res.statusCode, + err: body, + statusMessage: res.statusMessage + }; + + callback(errObj); // Pass out the error message from the backend } }); From 659d1337a45a516134b94aef89597c5b7a6a87c0 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:15:42 -0400 Subject: [PATCH 3/8] ADD docs for deletePlot method --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 9276b01..cf0a0aa 100644 --- a/README.md +++ b/README.md @@ -233,3 +233,16 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { }); }); ``` + +##plotly.deletePlot(fid[, callback]) +`fid` is a String, the id of the plot you wish you delete +`callback` is a function with `err` and `msg` as parameters. + +```javascript +var plotly = require('../.')('username','apiKey'); + +plotly.deletePlot('88', function (err, msg) { + if (err) console.log(err) + else console.log(msg); +}); +``` From 448865842ff4e670d4cedeaf1a8ed8b7e1c46d5b Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:20:39 -0400 Subject: [PATCH 4/8] pass back the figure on successful deletePlot --- README.md | 6 +++--- examples/delete-plot.js | 6 +++--- index.js | 3 +-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cf0a0aa..52b153c 100644 --- a/README.md +++ b/README.md @@ -236,13 +236,13 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { ##plotly.deletePlot(fid[, callback]) `fid` is a String, the id of the plot you wish you delete -`callback` is a function with `err` and `msg` as parameters. +`callback` is a function with `err` and `figure` as parameters. `err`, if present, is the error message returned from the request. `figure` is the figure that was deleted. ```javascript var plotly = require('../.')('username','apiKey'); -plotly.deletePlot('88', function (err, msg) { +plotly.deletePlot('88', function (err, figure) { if (err) console.log(err) - else console.log(msg); + else console.log(figure); // msg is the figure that was deleted }); ``` diff --git a/examples/delete-plot.js b/examples/delete-plot.js index a26b0e3..56aec5c 100644 --- a/examples/delete-plot.js +++ b/examples/delete-plot.js @@ -1,8 +1,8 @@ 'use strict'; -var plotly = require('../.')('username','apiKey'); +var plotly = require('../.')('alexander.daniel','u1jactdk3m'); -plotly.deletePlot('888', function (err, msg) { +plotly.deletePlot('2718', function (err, figure) { if (err) console.log(err); - else console.log(msg); + else console.log(figure); }); diff --git a/index.js b/index.js index 772f82c..2e77bdd 100644 --- a/index.js +++ b/index.js @@ -262,8 +262,7 @@ Plotly.prototype.deletePlot = function (fid, callback) { if (res.statusCode === 200) { - var msg = 'Successfully deleted plot: ' + self.username + ':' + fid; - callback(null, msg); + callback(null, body); } else { From 4e4ad07c6456b3fa94f15ad6912b4a1f5c3485ff Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:24:15 -0400 Subject: [PATCH 5/8] figure -> plot --- README.md | 6 +++--- examples/delete-plot.js | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 52b153c..f1d6b26 100644 --- a/README.md +++ b/README.md @@ -236,13 +236,13 @@ plotly.getFigure('fileOwner', 'fileId', function (err, figure) { ##plotly.deletePlot(fid[, callback]) `fid` is a String, the id of the plot you wish you delete -`callback` is a function with `err` and `figure` as parameters. `err`, if present, is the error message returned from the request. `figure` is the figure that was deleted. +`callback` is a function with `err` and `plot` as parameters. `err`, if present, is the error message returned from the request. `plot` is the plot that was deleted. ```javascript var plotly = require('../.')('username','apiKey'); -plotly.deletePlot('88', function (err, figure) { +plotly.deletePlot('88', function (err, plot) { if (err) console.log(err) - else console.log(figure); // msg is the figure that was deleted + else console.log(plot); // msg is the figure that was deleted }); ``` diff --git a/examples/delete-plot.js b/examples/delete-plot.js index 56aec5c..a3d1afc 100644 --- a/examples/delete-plot.js +++ b/examples/delete-plot.js @@ -2,7 +2,7 @@ var plotly = require('../.')('alexander.daniel','u1jactdk3m'); -plotly.deletePlot('2718', function (err, figure) { +plotly.deletePlot('2718', function (err, plot) { if (err) console.log(err); - else console.log(figure); + else console.log(plot); }); From 92956ee6edc15b61682c6a941e9ce29a53fba309 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:25:27 -0400 Subject: [PATCH 6/8] remove unneeded comment --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f1d6b26..1c74348 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,6 @@ var plotly = require('../.')('username','apiKey'); plotly.deletePlot('88', function (err, plot) { if (err) console.log(err) - else console.log(plot); // msg is the figure that was deleted + else console.log(plot); }); ``` From 6b4bfc78eac3d9212946b2e3f5c4620f24fe4543 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:32:02 -0400 Subject: [PATCH 7/8] 1.0.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1317388..efb913f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.3", + "version": "1.0.4", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": { From d3d48d358f3cb7c027819c2ba9e2ab8f20b1c078 Mon Sep 17 00:00:00 2001 From: alexander-daniel Date: Mon, 26 Oct 2015 13:32:15 -0400 Subject: [PATCH 8/8] 1.0.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index efb913f..2bff6b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "plotly", - "version": "1.0.4", + "version": "1.0.5", "description": "Simple node.js wrapper for the plot.ly API", "main": "index.js", "devDependencies": {