From 4bb7b762eabc397cbd933cb297b3278197a30561 Mon Sep 17 00:00:00 2001 From: Ahmader Date: Wed, 18 Oct 2017 20:26:40 +0300 Subject: [PATCH] 1.1.0 - Pass optional query parmaters to Wufoo API. - Update request npm version. - Updated README. - Adding tests for new feature. --- README.md | 132 ++++++++++++++++++++++++++++++++++++++++++++---- lib/generics.js | 31 ++++++++---- lib/wufoo.js | 94 +++++++++++++++------------------- package.json | 5 +- test/form.js | 7 +-- test/helper.js | 8 +++ test/report.js | 5 +- test/webhook.js | 2 - test/wufoo.js | 27 ++++++++-- 9 files changed, 222 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index c264650..e2232ce 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,22 @@ Each API returns it's own set of objects which is all documented on [Wufoo.com] wufoo.getFormEntries("idofForm", function(err, entries) { // do something with your entries here. }); + + // pass in optional query parameters + var optionalQuery = {pretty: true} + + wufoo.getForms(optionalQuery, function(err, forms) { + // do something with your forms here. + }); + + // get a specific form given the id and pass in optional query parameters + wufoo.getForm("idofForm", optionalQuery, function(err, forms) { + // do something with your forms here. + }); + + wufoo.getFormEntries("idofForm", optionalQuery, function(err, entries) { + // do something with your entries here. + }); ``` ### Forms @@ -37,9 +53,9 @@ Get all the forms for an account. getForms returns an array of getForms returns an array of Form: @@ -66,6 +97,22 @@ Convenience methods are provided to get entries, fields and entry count for a getFormEntries and ```javascript - wufoo.getFormEntries(formid, function(err, entries) { + wufoo.getFormEntries("formid", function(err, entries) { + // do something here. + }); + + wufoo.getReportEntries("reportid", function(err, entries) { + // do something here. + }); + + // pass in optional query parameters + var optionalQuery = {pretty: true} + + wufoo.getFormEntries("formid", optionalQuery, function(err, entries) { // do something here. }); - wufoo.getReportEntries(reportid, function(err, entries) { + wufoo.getReportEntries("reportid", optionalQuery, function(err, entries) { // do something here. }); @@ -100,6 +158,18 @@ Get all the reports for an account. getReports returns an array of // do something here. }); + // pass in optional query parameters + var optionalQuery = {pretty: true} + + wufoo.getReports(optionalQuery, function(err, reports) { + // do something here + }); + + // get a specific form given the id. + wufoo.getReport("idofReport", optionalQuery, function(err, report){ + // do something here. + }); + ``` Convenience methods are provided to get entries, fields and entry count for a Report: @@ -117,6 +187,22 @@ Convenience methods are provided to get entries, fields and entry count for a Re report.getFields(function(err, fields) { // do something here. }); + + // pass in optional query parameters + var optionalQuery = {pretty: true} + + report.getEntries(optionalQuery, function(err, entries) { + // do something here. + }); + + report.getEntriesCount(optionalQuery, function(err, count) { + // do something here. + console.log("There are " + count + " number of entries"); + }); + + report.getFields(optionalQuery, function(err, fields) { + // do something here. + }); ``` @@ -126,7 +212,14 @@ Get all the reports for a form. getFields returns an array of getWidgets returns an array of getComments returns an array of getComments returns an array of getCommentCount: ```javascript - wufoo.getCommentCount(formid, function(err, count) { + wufoo.getCommentCount("formid", function(err, count) { + // do something here. + }); + + // pass in optional query parameters + var optionalQuery = {pretty: true} + + wufoo.getCommentCount("formid", optionalQuery, function(err, count) { // do something here. }); diff --git a/lib/generics.js b/lib/generics.js index 8a0f85a..f4e3a87 100644 --- a/lib/generics.js +++ b/lib/generics.js @@ -13,20 +13,31 @@ function GenericParentEntity(wufoo) { this.wufoo = wufoo; } -GenericParentEntity.prototype.getFields = function(fn) { - this._cacheOrFetch(this.linkFields, "Fields", fn); +// build the arguments id, params, fn +GenericParentEntity.prototype.buildArgs = function() { // (arguments) + var args = Array.prototype.slice.call(arguments, 0), + params = (args.length==2 ? args.shift() : {}), + fn = args.shift(); + return {params: params, fn: fn}; } -GenericParentEntity.prototype.getEntries = function (fn) { - this._cacheOrFetch(this.linkEntries, "Entries", fn); +GenericParentEntity.prototype.getFields = function() { + var args = this.buildArgs.apply(null, arguments); + this._cacheOrFetch(this.linkFields, "Fields", args.params, args.fn); } -GenericParentEntity.prototype.getEntriesCount = function(fn) { +GenericParentEntity.prototype.getEntries = function () { + var args = this.buildArgs.apply(null, arguments); + this._cacheOrFetch(this.linkEntries, "Entries", args.params, args.fn); +} + +GenericParentEntity.prototype.getEntriesCount = function() { var self = this; - this._cacheOrAction("entriesCount", fn, function(){ - self.wufoo.get(self.linkEntriesCount, function(err, json) { + var args = this.buildArgs.apply(null, arguments); + this._cacheOrAction("entriesCount", args.fn, function(){ + self.wufoo.get(self.linkEntriesCount, args.params, function(err, json) { self.entriesCount = Number(json.EntryCount); - fn(err, self.entriesCount); + args.fn(err, self.entriesCount); }); }); } @@ -42,12 +53,12 @@ GenericParentEntity.prototype._cacheOrAction = function(propName, fn, action) { } // wrapper for fetching objects from wufoo cacheOrAction is called to check for local copy existence first. -GenericParentEntity.prototype._cacheOrFetch = function(link, propName, fn) { +GenericParentEntity.prototype._cacheOrFetch = function(link, propName, params, fn) { var self = this; var thisProp = propName.toLowerCase(); this._cacheOrAction(thisProp, fn, function() { - self.wufoo.get(link, function(err, json) { + self.wufoo.get(link, params, function(err, json) { self[thisProp] = utils.cloneObjects(json[propName], function() {return new GenericEntity(self.wufoo)}); fn(err, self[thisProp]); }); diff --git a/lib/wufoo.js b/lib/wufoo.js index 6060ae5..0d2ea9d 100644 --- a/lib/wufoo.js +++ b/lib/wufoo.js @@ -22,94 +22,82 @@ function Wufoo(account, apiKey) { this.apiKey = apiKey; } +// build the arguments params, fn +Wufoo.prototype.buildArgs = function() { // (arguments) + var args = Array.prototype.slice.call(arguments, 0), + params = (args.length==2 ? args.shift() : {}), + fn = args.shift(); + return {params: params, fn: fn}; +} + +// build the arguments id, params, fn +Wufoo.prototype.buildArgsWithId = function() { // (arguments) + var args = Array.prototype.slice.call(arguments, 0), + id = args.shift(), + params = (args.length==2 ? args.shift() : {}), + fn = args.shift(); + return {id: id, params: params, fn: fn}; +} // get all the forms for this account/subdomain. Wufoo.prototype.getForms = function() { // (params, fn) - var args = Array.prototype.slice.call(arguments, 0), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("forms", params, fn, "Forms", Form); + var args = this.buildArgs.apply(null, arguments); + this._getObjects("forms", args.params, args.fn, "Forms", Form); } // get the form given the id/hash. Wufoo.prototype.getForm = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("forms/" + id, params, fn, "Forms", Form, true); + var args = this.buildArgsWithId.apply(null, arguments); + this._getObjects("forms/" + args.id, args.params, args.fn, "Forms", Form, true); } // get all the form entries given the form id/hash. Wufoo.prototype.getFormEntries = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("forms/" + id + "/entries", params, fn, "Entries", GenericEntity); + var args = this.buildArgsWithId.apply(null, arguments); + this._getObjects("forms/" + args.id + "/entries", args.params, args.fn, "Entries", GenericEntity); } // get all the reports for this account/subdomain. Wufoo.prototype.getReports = function() { // (params, fn) - var args = Array.prototype.slice.call(arguments, 0), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("reports", params, fn, "Reports", Report); + var args = this.buildArgs.apply(null, arguments); + this._getObjects("reports", args.params, args.fn, "Reports", Report); } // get the report given the report id. Wufoo.prototype.getReport = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("reports/" + id, params, fn, "Reports", Report, true); + var args = this.buildArgsWithId.apply(null, arguments); + this._getObjects("reports/" + args.id, args.params, args.fn, "Reports", Report, true); } // get all the form entries given the report id/hash. Wufoo.prototype.getReportEntries = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("reports/" + id + "/entries", params, fn, "Entries", GenericEntity); + var args = this.buildArgsWithId.apply(null, arguments); + this._getObjects("reports/" + args.id + "/entries", args.params, args.fn, "Entries", GenericEntity); } // get the form fields given the form id/hash. Wufoo.prototype.getFields = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("forms/" + id + "/fields", params, fn, "Fields", GenericEntity); + var args = this.buildArgsWithId.apply(null, arguments); + this._getObjects("forms/" + args.id + "/fields", args.params, args.fn, "Fields", GenericEntity); } // get the widgets from wufoo given the reportid/hash. Wufoo.prototype.getWidgets = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("reports/" + id + "/widgets", params, fn, "Widgets", GenericEntity); + var args = this.buildArgsWithId.apply(null, arguments); + this._getObjects("reports/" + args.id + "/widgets", args.params, args.fn, "Widgets", GenericEntity); } // get form comments given form id/hash Wufoo.prototype.getComments = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getObjects("forms/" + id + "/comments", params, fn, "Comments", GenericEntity); + var args = this.buildArgsWithId.apply(null, arguments); + this._getObjects("forms/" + args.id + "/comments", args.params, args.fn, "Comments", GenericEntity); } // get the count for the comments of a form given the id/hash. Wufoo.prototype.getCommentCount = function() { // (id, params, fn) - var args = Array.prototype.slice.call(arguments, 0), - id = args.shift(), - params = (args.length==2 ? args.shift() : {}), - fn = args.shift(); - this._getMethod("forms/" + id + "/comments/count", params, function(err, json) { - fn(err, Number(json.Count)); + var args = this.buildArgsWithId.apply(null, arguments); + this._getMethod("forms/" + args.id + "/comments/count", args.params, function(err, json) { + args.fn(err, Number(json.Count)); }); } @@ -152,9 +140,8 @@ Wufoo.prototype.request = function(method, uri, params, fn) { if (fn!=undefined && params !=undefined) options["form"] = params; // params for form post was passed in. if (fn == undefined && params != undefined) fn = params; // no params was passed in. In which case it is fn callback. - util.debug(method.toUpperCase() + " " + uri); request(options, function(err, res, body) { - if (err){util.debug(err);} + if (err){console.error(err);} fn(err, JSON.parse(body)); }); } @@ -200,8 +187,9 @@ function Report(wufoo){ this.wufoo = wufoo; } -Report.prototype.getWidgets = function(fn) { - this._cacheOrFetch(this.linkWidgets, "Widgets", fn); +Report.prototype.getWidgets = function() { + var args = this.buildArgs.apply(null, arguments); + this._cacheOrFetch(this.linkWidgets, "Widgets", args.params, args.fn); } diff --git a/package.json b/package.json index ce2ae40..b46ce1e 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "wufoo", "description": "Wrapper for the Wufoo API (http://www.wufoo.com/docs/api/v3/)", - "version": "1.0.0", + "version": "1.1.0", "author": {"name": "Justin Law", "email": ""}, + "contributors": ["Ahmader "], "dependencies": { - "request": "~2.0.0" + "request": "^2.83.0" }, "main": "index", "repository": { diff --git a/test/form.js b/test/form.js index 7e6ad21..28db74e 100644 --- a/test/form.js +++ b/test/form.js @@ -1,6 +1,5 @@ var should = require("should"); var helper = require("./helper.js"); -var util = require("util"); describe("Form", function() { var $form; @@ -14,7 +13,7 @@ describe("Form", function() { describe("#getEntries", function() { it("Should return array of entries", function(done){ - $form.getEntries(function(err, entries){ + $form.getEntries({system:'true'}, function(err, entries){ (entries.constructor == Array).should.be.true; helper.isEntry(entries[0]); done(err); @@ -25,7 +24,7 @@ describe("Form", function() { describe("#getFields", function() { it("Should return array of fields", function(done){ - $form.getFields(function(err, fields){ + $form.getFields({system:'true'}, function(err, fields){ (fields.constructor == Array).should.be.true; helper.isField(fields[0]); done(err); @@ -49,10 +48,8 @@ describe("Form", function() { $form.addWebhook("http://localhost:3000/bin", function(err, hashid) { should.exist(hashid); (typeof(hashid)).should.equal("string"); - util.debug("Added Webhook: " + hashid); $form.deleteWebhook(hashid, function(err, success) { success.should.be.true; - util.debug("Removed Webhook: " + hashid); done(err); }); }); diff --git a/test/helper.js b/test/helper.js index 7160063..88a422d 100644 --- a/test/helper.js +++ b/test/helper.js @@ -9,6 +9,7 @@ function helper() { } helper.prototype.isEntry = isEntry; helper.prototype.isField = isField; +helper.prototype.isSystemFields = isSystemFields; helper.prototype.isWidget = function(widget) { for (prop in ["names", "size", "hash", "type", "typeDesc"]) { should.exist(widget[prop]); @@ -35,6 +36,13 @@ function isField(field) { should.not.exist(field.endDate); } +function isSystemFields(fields) { + fields.should.containDeep([{isSystem: true}]); + var found = fields.filter(function(field) { + return (field.isSystem) + }); + (found.length > 0).should.be.true; +} diff --git a/test/report.js b/test/report.js index 8a6268f..fb76e76 100644 --- a/test/report.js +++ b/test/report.js @@ -1,6 +1,5 @@ var should = require("should"); var helper = require("./helper.js"); -var util = require("util"); describe("Report", function() { @@ -15,12 +14,12 @@ describe("Report", function() { describe("#getWidgets", function() { it("Should return array of widgets", function(done){ - $report.getWidgets(function(err, widgets){ + $report.getWidgets({system:'true'}, function(err, widgets){ (widgets.constructor == Array).should.be.true; if (widgets.length>0) { // TODO -- the fishbowl account we are using for testing do not have any widgets. // Must find a way for better test coverage. - util.debug("Testing Widget Entity"); + console.error("Testing Widget Entity"); helper.isWidget(widgets[0]); } done(err); diff --git a/test/webhook.js b/test/webhook.js index 30e7aa0..f72b39f 100644 --- a/test/webhook.js +++ b/test/webhook.js @@ -1,5 +1,4 @@ var should = require("should"); -var util = require("util"); var helper = require("./helper.js"); var WebHook = require("../lib/webhook.js"); @@ -30,7 +29,6 @@ describe("WebHook", function() { id = ids[i] helper.wufoo.webhook().delete($form.hash, id, function(err, success) { success.should.be.true; - util.debug("Removed Webhook: " + id); }); } done(); diff --git a/test/wufoo.js b/test/wufoo.js index 5718139..7add1bd 100644 --- a/test/wufoo.js +++ b/test/wufoo.js @@ -1,5 +1,4 @@ var should = require("should"); -var util = require("util"); var helper = require("./helper.js"); describe("Wufoo", function() { @@ -39,6 +38,12 @@ describe("Wufoo", function() { }); }); }) + it("Should return array of forms containing EntryCountToday property", function(done){ + $wufoo.getForms({includeTodayCount:'true'}, function(err, forms){ + should.exist(forms[0].entryCountToday); + done(err); + }); + }) }); describe("#getFields", function() { @@ -62,11 +67,17 @@ describe("Wufoo", function() { }); it("Should return an array Objects that are Fields.", function(done){ - $wufoo.getFields(formId, function(err, fields) { + $wufoo.getFields(formId, {system:'true'}, function(err, fields) { helper.isField(fields[0]); done(err); }); }); + it("Should return array of Fields containing System Fields", function(done){ + $wufoo.getFields(formId, {system:'true'}, function(err, fields){ + helper.isSystemFields(fields); + done(err); + }); + }) }); describe("#getFormEntries", function() { @@ -89,11 +100,17 @@ describe("Wufoo", function() { }); it("Should return array of objects containing typical wufoo entry attributes", function(done) { - $wufoo.getFormEntries(formId, function(err, entries){ + $wufoo.getFormEntries(formId, {system:'true'}, function(err, entries){ helper.isEntry(entries[0]); done(err); }); }); + it("Should return entries containing System Fields", function(done){ + $wufoo.getFormEntries(formId, {system:'true'}, function(err, entries){ + should.exist(entries[0].iP); + done(err); + }); + }) }); describe("#getReportEntries", function() { @@ -175,7 +192,7 @@ describe("Wufoo", function() { if (widgets.length>0) { // TODO -- the fishbowl account we are using for testing do not have any widgets. // Must find a way for better test coverage. - util.debug("Testing Widget Entity"); + console.error("Testing Widget Entity"); helper.isWidget(widgets[0]); } done(err); @@ -208,7 +225,7 @@ describe("Wufoo", function() { if (comments.length>0) { // TODO -- the fishbowl account we are using for testing do not have any widgets. // Must find a way for better test coverage. - util.debug("Testing Comment Entity"); + console.error("Testing Comment Entity"); helper.isComment(comment[0]); } done(err);