Browse Source

take extra options as an object

dev/test.graph.create
Brett Langdon 10 years ago
parent
commit
931de20942
2 changed files with 46 additions and 36 deletions
  1. +26
    -30
      lib/api/graph.js
  2. +20
    -6
      test/api/graph.js

+ 26
- 30
lib/api/graph.js View File

@ -1,5 +1,6 @@
var client = require("../client"); var client = require("../client");
var json = require("../json"); var json = require("../json");
var querystring = require("querystring");
/*section: graph /*section: graph
*comment: take a snapshot of a metric query *comment: take a snapshot of a metric query
@ -48,10 +49,11 @@ function snapshot(query, from, to, eventQuery, callback){
*comment: create an embed graph of a metric query *comment: create an embed graph of a metric query
*params: *params:
* graph_json: The request array to pass create in the embed * graph_json: The request array to pass create in the embed
* timeframe: optional, one of ("1_hour", "4_hours", "1_day", "2_days", and "1_week")
* size: optional, one of ("small", "medium", "large", "xlarge")
* legend: optional, "yes" or "no"
* title: optional, the title of the embed
* options: optional, object of extra parameters to pass to the embed create (see options[*] params)
* options["timeframe"]: optional, one of ("1_hour", "4_hours", "1_day", "2_days", and "1_week")
* options["size"]: optional, one of ("small", "medium", "large", "xlarge")
* options["legend"]: optional, "yes" or "no"
* options["title"]: optional, the title of the embed
* callback: function(err, res) * callback: function(err, res)
*example: | *example: |
* ```javascript * ```javascript
@ -73,36 +75,28 @@ function snapshot(query, from, to, eventQuery, callback){
* } * }
* ] * ]
* } * }
* 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){
* var options = {
* timeframe: "1_hour",
* size: "xlarge",
* legend: "yes",
* title: "my awesome embed"
* };
* dogapi.graph.createEmbed(graphJSON, options, function(err, res){
* console.dir(res); * console.dir(res);
* }); * });
* ``` * ```
*/ */
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);
function createEmbed(graphJSON, options, callback){
if(callback === undefined && typeof options === 'function'){
callback = options;
options = {};
} }
var body = options || {};
body.graph_json = JSON.stringify(graphJSON);
// Create the request // Create the request
var params = { var params = {
body: body,
body: querystring.stringify(body),
contentType: "application/x-www-form-urlencoded" contentType: "application/x-www-form-urlencoded"
}; };
@ -142,10 +136,12 @@ module.exports = {
snapshot(query, from, to, eventQuery, callback); snapshot(query, from, to, eventQuery, callback);
} else if (args._.length > 4 && subcommand === "create_embed") { } else if (args._.length > 4 && subcommand === "create_embed") {
var graph_json = json.parse(args._[4]); var graph_json = json.parse(args._[4]);
var timeframe = args["timeframe"];
var size = args["size"];
var legend = args["legend"];
var title = args["title"];
var options = {
timeframe: args["timeframe"],
size: args["size"],
legend: args["legend"],
title: args["title"]
};
createEmbed(graph_json, timeframe, size, legend, title, callback); createEmbed(graph_json, timeframe, size, legend, title, callback);
} else { } else {
callback("unknown subcommand or arguments try `dogapi graph --help` for help", false); callback("unknown subcommand or arguments try `dogapi graph --help` for help", false);


+ 20
- 6
test/api/graph.js View File

@ -3,6 +3,7 @@ var client = require("../../lib/client");
var extend = require("extend"); var extend = require("extend");
var graph = require("../../lib/api/graph"); var graph = require("../../lib/api/graph");
var sinon = require("sinon"); var sinon = require("sinon");
var querystring = require("querystring");
describe("api/graph", function(){ describe("api/graph", function(){
var stub_request; var stub_request;
@ -25,9 +26,15 @@ describe("api/graph", function(){
} }
] ]
}; };
var options = {
timeframe: "1_hour",
size: "large",
legend: "yes",
title: "test graph embed"
};
// Make our api call // Make our api call
graph.createEmbed(graphJSON, "1_hour", "large", "yes", "test graph embed");
graph.createEmbed(graphJSON, options);
// Assert we properly called `client.request` // Assert we properly called `client.request`
assert(stub_request.calledOnce); assert(stub_request.calledOnce);
@ -38,9 +45,14 @@ describe("api/graph", function(){
// Properly formatted body and content-type // Properly formatted body and content-type
var params = call_args[2]; var params = call_args[2];
var expectedBody = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON)) +
"&timeframe=1_hour&size=large&legend=yes&title=test%20graph%20embed";
assert.equal(params.body, expectedBody);
var expectedBody = {
graph_json: JSON.stringify(graphJSON),
timeframe: "1_hour",
size: "large",
legend: "yes",
title: "test graph embed"
};
assert.deepEqual(querystring.parse(params.body), expectedBody);
assert(params.contentType, "application/x-form-urlencoded"); assert(params.contentType, "application/x-form-urlencoded");
}); });
@ -63,8 +75,10 @@ describe("api/graph", function(){
// Properly formatted body // Properly formatted body
var params = call_args[2]; var params = call_args[2];
var expectedBody = "graph_json=" + encodeURIComponent(JSON.stringify(graphJSON));
assert.equal(params.body, expectedBody);
var expectedBody = {
graph_json: JSON.stringify(graphJSON)
};
assert.deepEqual(querystring.parse(params.body), expectedBody);
}); });
}); });
}); });

Loading…
Cancel
Save