Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and improvements #61

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/new-interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var Dogapi = require("../lib");

var options = {
api_key: "YOUR_KEY_HERE",
app_key: "YOUR_KEY_HERE",
};

const dogapi = new Dogapi(options);

dogapi.metric.send('test', 1)
10 changes: 10 additions & 0 deletions examples/original-interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var dogapi = require("../lib");

var options = {
api_key: "YOUR_KEY_HERE",
app_key: "YOUR_KEY_HERE",
};

dogapi.initialize(options);

dogapi.metric.send('test', 1)
283 changes: 142 additions & 141 deletions lib/api/comment.js
Original file line number Diff line number Diff line change
@@ -1,157 +1,158 @@
var client = require("../client");
var util = require("util");
const util = require("util");

/*section: comment
*comment: create a new comment
*params:
* message: the message of the comment
* properties: |
* optional, an object containing any of the following
* * handle: the handle to associate the comment with (e.g. "[email protected]")
* * related_event_id: the event to associate the comment with
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* dogapi.comment.create("a comment message", function(err, res){
* console.dir(res);
* });
* ```
*/
function create(message, properties, callback){
if(arguments.length < 3 && typeof arguments[1] === "function"){
callback = properties;
properties = {};
module.exports = function (client) {
/*section: comment
*comment: create a new comment
*params:
* message: the message of the comment
* properties: |
* optional, an object containing any of the following
* * handle: the handle to associate the comment with (e.g. "[email protected]")
* * related_event_id: the event to associate the comment with
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* dogapi.comment.create("a comment message", function(err, res){
* console.dir(res);
* });
* ```
*/
function create(message, properties, callback) {
if (arguments.length < 3 && typeof arguments[1] === "function") {
callback = properties;
properties = {};
}

const params = {
body: {
message: message
}
};

if (typeof properties === "object") {
if (properties.handle) {
params.body.handle = properties.handle;
}
if (properties.related_event_id) {
params.body.related_event_id = properties.related_event_id;
}
}

client.request("POST", "/comments", params, callback);
}

var params = {
body: {
message: message
/*section: comment
*comment: update an existing comment
*params:
* commentId: the id of the comment to update
* message: the message of the comment
* handle: optional, the handle to associate the comment with (e.g. "[email protected]")
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* dogapi.comment.update(1234, "new message", function(err, res){
* console.dir(res);
* });
* ```
*/
function update(commentId, message, handle, callback) {
if (arguments.length < 4 && typeof arguments[2] === "function") {
callback = handle;
handle = undefined;
}
};

if(typeof properties === "object"){
if(properties.handle){
const params = {
body: {
message: message
}
};
if (handle) {
params.body.handle = properties.handle;
}
if(properties.related_event_id){
params.body.related_event_id = properties.related_event_id;
}
}

client.request("POST", "/comments", params, callback);
}

/*section: comment
*comment: update an existing comment
*params:
* commentId: the id of the comment to update
* message: the message of the comment
* handle: optional, the handle to associate the comment with (e.g. "[email protected]")
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* dogapi.comment.update(1234, "new message", function(err, res){
* console.dir(res);
* });
* ```
*/
function update(commentId, message, handle, callback){
if(arguments.length < 4 && typeof arguments[2] === "function"){
callback = handle;
handle = undefined;
client.request("PUT", util.format("/comments/%s", commentId), params, callback);
}

var params = {
body: {
message: message
}
};
if(handle){
params.body.handle = properties.handle;
/*section: comment
*comment: remove a comment
*params:
* commentId: the id of the comment to remove
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* dogapi.comment.remove(1234, function(err, res){
* console.dir(res);
* });
* ```
*/
function remove(commentId, callback) {
client.request("DELETE", util.format("/comments/%s", commentId), callback);
}

client.request("PUT", util.format("/comments/%s", commentId), params, callback);
}

/*section: comment
*comment: remove a comment
*params:
* commentId: the id of the comment to remove
* callback: function(err, res)
*example: |
* ```javascript
* var dogapi = require("dogapi");
* var options = {
* api_key: "api_key",
* app_key: "app_key"
* };
* dogapi.initialize(options);
* dogapi.comment.remove(1234, function(err, res){
* console.dir(res);
* });
* ```
*/
function remove(commentId, callback){
client.request("DELETE" ,util.format("/comments/%s", commentId), callback);
}


module.exports = {
create: create,
update: update,
remove: remove,
getUsage: function(){
return [
" dogapi comment create <message> [--handle <handle>] [--event <event-id>]",
" dogapi comment update <comment-id> <message> [--handle <handle>]",
" dogapi comment remove <comment-id>"
];
},
getHelp: function(){
return [
"Comment:",
" Subcommands:",
" create <message> add a new comment",
" update <comment-id> <message> update an existing comment",
" remove <comment-id> delete a comment",
"",
" Options:",
" --handle <handle> the handle to associate with the comment (e.g. \"[email protected]\")",
" --event <event-id> related event id to associate the comment with"
];
},
handleCli: function(subcommand, args, callback){
if(subcommand === "create"){
var message = args._[4];
var properties = {};
if(args["handle"]){
properties.handle = args["handle"];
}
if(args["event"]){
properties.related_event_id = parseInt(args["event"]);
return {
create: create,
update: update,
remove: remove,
getUsage: function () {
return [
" dogapi comment create <message> [--handle <handle>] [--event <event-id>]",
" dogapi comment update <comment-id> <message> [--handle <handle>]",
" dogapi comment remove <comment-id>"
];
},
getHelp: function () {
return [
"Comment:",
" Subcommands:",
" create <message> add a new comment",
" update <comment-id> <message> update an existing comment",
" remove <comment-id> delete a comment",
"",
" Options:",
" --handle <handle> the handle to associate with the comment (e.g. \"[email protected]\")",
" --event <event-id> related event id to associate the comment with"
];
},
handleCli: function (subcommand, args, callback) {
if (subcommand === "create") {
const message = args._[4];
const properties = {};
if (args["handle"]) {
properties.handle = args["handle"];
}
if (args["event"]) {
properties.related_event_id = parseInt(args["event"]);
}
create(message, properties, callback);
} else if (subcommand === "update") {
const commentId = args._[4];
const message = args._[5];
update(commentId, message, args["handle"], callback);
} else if (subcommand === "remove") {
const commentId = args._[4];
remove(commentId, callback);
} else {
callback("unknown subcommand or arguments try `dogapi comment --help` for help", false);
}
create(message, properties, callback);
} else if(subcommand === "update"){
var commentId = args._[4];
var message = args._[5];
update(commentId, message, args["handle"], callback);
} else if(subcommand === "remove"){
var commentId = args._[4];
remove(commentId, callback);
} else {
callback("unknown subcommand or arguments try `dogapi comment --help` for help", false);
}
}
};
};
Loading