From b2c77f1cf6821d84490fca0564787ee47c2b4d92 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 9 Mar 2016 16:13:49 -0500 Subject: [PATCH] handle optional parameters and URI encoding values --- lib/api/graph.js | 51 ++++++++++++++++++++++++++++------------------- test/api/graph.js | 27 +++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/lib/api/graph.js b/lib/api/graph.js index 680951f..94ce703 100644 --- a/lib/api/graph.js +++ b/lib/api/graph.js @@ -62,37 +62,48 @@ function snapshot(query, from, to, eventQuery, callback){ * }; * dogapi.initialize(options); * var query = "system.cpu.idle{*}"; - * var graph_json = { - * "viz": "timeseries", - * "requests": [ + * var graphJSON = { + * viz: "timeseries", + * requests: [ * { - * "q": query, - * "aggregator": "avg", - * "conditional_formats": [], - * "type": "area" + * q: query, + * aggregator: "avg", + * conditional_formats: [], + * type: "area" * } * ] * } - * var timeframe = '1_hour'; - * var size = 'xlarge'; - * var legend = 'yes'; - * var title = 'my awesome embed'; - * dogapi.graph.createEmbed(graph_json, timeframe, size, legend, title, function(err, res){ + * var timeframe = "1_hour"; + * var size = "xlarge"; + * var legend = "yes"; + * var title = "my awesome embed"; + * dogapi.graph.createEmbed(graphJSON, timeframe, size, legend, title, function(err, res){ * console.dir(res); * }); * ``` */ -function createEmbed(graph_json, timeframe, size, legend, title, callback){ +function createEmbed(graphJSON, timeframe, size, legend, title, callback){ var lastArgument = arguments.length -1; + // Build the form body + var body = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON)); + if (timeframe !== undefined) { + body += "&timeframe=" + encodeURIComponent(timeframe); + } + if (size !== undefined) { + body += "&size=" + encodeURIComponent(size); + } + if (legend !== undefined) { + body += "&legend=" + encodeURIComponent(legend); + } + if (title !== undefined) { + body += "&title=" + encodeURIComponent(title); + } + + // Create the request var params = { - body: 'graph_json=' + JSON.stringify(graph_json) + - '&timeframe=' + timeframe + - '&size=' + size + - '&legend=' + legend + - '&title=' + title - , - contentType: 'application/x-www-form-urlencoded' + body: body, + contentType: "application/x-www-form-urlencoded" }; client.request("POST", "/graph/embed", params, callback); diff --git a/test/api/graph.js b/test/api/graph.js index f3ea9cf..007efbc 100644 --- a/test/api/graph.js +++ b/test/api/graph.js @@ -38,10 +38,33 @@ describe("api/graph", function(){ // Properly formatted body and content-type var params = call_args[2]; - var expectedBody = "graph_json=" + JSON.stringify(graphJSON) + - "&timeframe=1_hour&size=large&legend=yes&title=test graph embed"; + var expectedBody = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON)) + + "&timeframe=1_hour&size=large&legend=yes&title=test%20graph%20embed"; assert.equal(params.body, expectedBody); assert(params.contentType, "application/x-form-urlencoded"); }); + + it("should only require graph_json", function(){ + var graphJSON = { + viz: "timeseries", + requests: [ + { + q: "system.cpu.idle{*}" + } + ] + }; + + // Make our api call + graph.createEmbed(graphJSON); + + // Assert we properly called `client.request` + assert(stub_request.calledOnce); + var call_args = stub_request.getCall(0).args; + + // Properly formatted body + var params = call_args[2]; + var expectedBody = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON)); + assert.equal(params.body, expectedBody); + }); }); });