How to use tryJSON method in mountebank

Best JavaScript code snippet using mountebank

account-manager.js

Source:account-manager.js Github

copy

Full Screen

...58 if (res.ok) {59 resolve(null);60 }61 else {62 var body = tryJSON(res.text);63 if (body) {64 reject(body);65 }66 else {67 reject({ message: res.text, statusCode: res.status });68 }69 }70 });71 });72 };73 AccountManager.prototype.logout = function () {74 var _this = this;75 return Promise(function (resolve, reject, notify) {76 var req = request.post(_this.serverUrl + "/auth/logout");77 _this.attachCredentials(req, request);78 req.end(function (err, res) {79 if (err && err.status !== 401) {80 reject({ message: _this.getErrorMessage(err, res) });81 return;82 }83 _this._authedAgent = null;84 if (res.ok) {85 resolve(null);86 }87 else {88 var body = tryJSON(res.text);89 if (body) {90 reject(body);91 }92 else {93 reject({ message: res.text, statusCode: res.status });94 }95 }96 });97 });98 };99 AccountManager.prototype.isAuthenticated = function () {100 var _this = this;101 return Promise(function (resolve, reject, notify) {102 var requester = _this._authedAgent ? _this._authedAgent : request;103 var req = requester.get(_this.serverUrl + "/authenticated");104 _this.attachCredentials(req, requester);105 req.end(function (err, res) {106 if (err && err.status !== 401) {107 reject({ message: _this.getErrorMessage(err, res) });108 return;109 }110 var status = res ? res.status : err.status;111 var authenticated = status === 200;112 if (authenticated && _this._saveAuthedAgent) {113 _this._authedAgent = request.agent();114 _this._authedAgent.saveCookies(res);115 }116 resolve(authenticated);117 });118 });119 };120 AccountManager.prototype.addAccessKey = function (machine, description) {121 var _this = this;122 return Promise(function (resolve, reject, notify) {123 return _this.generateAccessKey().then(function (newAccessKey) {124 var accessKey = { id: null, name: newAccessKey, createdTime: new Date().getTime(), createdBy: machine, description: description };125 var requester = _this._authedAgent ? _this._authedAgent : request;126 var req = requester.post(_this.serverUrl + "/accessKeys/");127 _this.attachCredentials(req, requester);128 req.set("Content-Type", "application/json;charset=UTF-8")129 .send(JSON.stringify(accessKey))130 .end(function (err, res) {131 if (err) {132 reject({ message: _this.getErrorMessage(err, res) });133 return;134 }135 if (res.ok) {136 var location = res.header["location"];137 if (location && location.lastIndexOf("/") !== -1) {138 accessKey.id = location.substr(location.lastIndexOf("/") + 1);139 resolve(accessKey);140 }141 else {142 resolve(null);143 }144 }145 else {146 var body = tryJSON(res.text);147 if (body) {148 reject(body);149 }150 else {151 reject({ message: res.text, statusCode: res.status });152 }153 }154 });155 });156 });157 };158 AccountManager.prototype.getAccessKey = function (accessKeyId) {159 var _this = this;160 return Promise(function (resolve, reject, notify) {161 var requester = _this._authedAgent ? _this._authedAgent : request;162 var req = requester.get(_this.serverUrl + "/accessKeys/" + accessKeyId);163 _this.attachCredentials(req, requester);164 req.end(function (err, res) {165 if (err) {166 reject({ message: _this.getErrorMessage(err, res) });167 return;168 }169 var body = tryJSON(res.text);170 if (res.ok) {171 if (body) {172 resolve(body.accessKey);173 }174 else {175 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });176 }177 }178 else {179 if (body) {180 reject(body);181 }182 else {183 reject({ message: res.text, statusCode: res.status });184 }185 }186 });187 });188 };189 AccountManager.prototype.getAccessKeys = function () {190 var _this = this;191 return Promise(function (resolve, reject, notify) {192 var requester = _this._authedAgent ? _this._authedAgent : request;193 var req = requester.get(_this.serverUrl + "/accessKeys");194 _this.attachCredentials(req, requester);195 req.end(function (err, res) {196 if (err) {197 reject({ message: _this.getErrorMessage(err, res) });198 return;199 }200 var body = tryJSON(res.text);201 if (res.ok) {202 if (body) {203 resolve(body.accessKeys);204 }205 else {206 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });207 }208 }209 else {210 if (body) {211 reject(body);212 }213 else {214 reject({ message: res.text, statusCode: res.status });215 }216 }217 });218 });219 };220 AccountManager.prototype.removeAccessKey = function (accessKeyId) {221 var _this = this;222 return Promise(function (resolve, reject, notify) {223 var requester = _this._authedAgent ? _this._authedAgent : request;224 var req = requester.del(_this.serverUrl + "/accessKeys/" + accessKeyId);225 _this.attachCredentials(req, requester);226 req.end(function (err, res) {227 if (err) {228 reject({ message: _this.getErrorMessage(err, res) });229 return;230 }231 if (res.ok) {232 resolve(null);233 }234 else {235 var body = tryJSON(res.text);236 if (body) {237 reject(body);238 }239 else {240 reject({ message: res.text, statusCode: res.status });241 }242 }243 });244 });245 };246 // Account247 AccountManager.prototype.getAccountInfo = function () {248 var _this = this;249 return Promise(function (resolve, reject, notify) {250 var requester = (_this._authedAgent ? _this._authedAgent : request);251 var req = requester.get(_this.serverUrl + "/account");252 _this.attachCredentials(req, requester);253 req.end(function (err, res) {254 if (err) {255 reject({ message: _this.getErrorMessage(err, res) });256 return;257 }258 var body = tryJSON(res.text);259 if (res.ok) {260 if (body) {261 _this.account = body.account;262 resolve(_this.account);263 }264 else {265 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });266 }267 }268 else {269 if (body) {270 reject(body);271 }272 else {273 reject({ message: res.text, statusCode: res.status });274 }275 }276 });277 });278 };279 AccountManager.prototype.updateAccountInfo = function (accountInfoToChange) {280 var _this = this;281 return Promise(function (resolve, reject, notify) {282 var requester = (_this._authedAgent ? _this._authedAgent : request);283 var req = requester.put(_this.serverUrl + "/account");284 _this.attachCredentials(req, requester);285 req.set("Content-Type", "application/json;charset=UTF-8")286 .send(JSON.stringify(accountInfoToChange))287 .end(function (err, res) {288 if (err) {289 reject({ message: _this.getErrorMessage(err, res) });290 return;291 }292 if (res.ok) {293 resolve(null);294 }295 else {296 var body = tryJSON(res.text);297 if (body) {298 reject(body);299 }300 else {301 reject({ message: res.text, statusCode: res.status });302 }303 }304 });305 });306 };307 // Apps308 AccountManager.prototype.getApps = function () {309 var _this = this;310 return Promise(function (resolve, reject, notify) {311 var requester = (_this._authedAgent ? _this._authedAgent : request);312 var req = requester.get(_this.serverUrl + "/apps");313 _this.attachCredentials(req, requester);314 req.end(function (err, res) {315 if (err) {316 reject({ message: _this.getErrorMessage(err, res) });317 return;318 }319 var body = tryJSON(res.text);320 if (res.ok) {321 if (body) {322 resolve(body.apps);323 }324 else {325 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });326 }327 }328 else {329 if (body) {330 reject(body);331 }332 else {333 reject({ message: res.text, statusCode: res.status });334 }335 }336 });337 });338 };339 AccountManager.prototype.getApp = function (appId) {340 var _this = this;341 return Promise(function (resolve, reject, notify) {342 var requester = (_this._authedAgent ? _this._authedAgent : request);343 var req = requester.get(_this.serverUrl + "/apps/" + appId);344 _this.attachCredentials(req, requester);345 req.end(function (err, res) {346 if (err) {347 reject({ message: _this.getErrorMessage(err, res) });348 return;349 }350 var body = tryJSON(res.text);351 if (res.ok) {352 if (body) {353 resolve(body.app);354 }355 else {356 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });357 }358 }359 else {360 if (body) {361 reject(body);362 }363 else {364 reject({ message: res.text, statusCode: res.status });365 }366 }367 });368 });369 };370 AccountManager.prototype.addApp = function (appName, description) {371 var _this = this;372 return Promise(function (resolve, reject, notify) {373 var app = { name: appName, description: description };374 var requester = (_this._authedAgent ? _this._authedAgent : request);375 var req = requester.post(_this.serverUrl + "/apps/");376 _this.attachCredentials(req, requester);377 req.set("Content-Type", "application/json;charset=UTF-8")378 .send(JSON.stringify(app))379 .end(function (err, res) {380 if (err) {381 reject({ message: _this.getErrorMessage(err, res) });382 return;383 }384 if (res.ok) {385 var location = res.header["location"];386 if (location && location.lastIndexOf("/") !== -1) {387 app.id = location.substr(location.lastIndexOf("/") + 1);388 resolve(app);389 }390 else {391 resolve(null);392 }393 }394 else {395 var body = tryJSON(res.text);396 if (body) {397 reject(body);398 }399 else {400 reject({ message: res.text, statusCode: res.status });401 }402 }403 });404 });405 };406 AccountManager.prototype.removeApp = function (app) {407 var _this = this;408 return Promise(function (resolve, reject, notify) {409 var id = (typeof app === "string") ? app : app.id;410 var requester = (_this._authedAgent ? _this._authedAgent : request);411 var req = requester.del(_this.serverUrl + "/apps/" + id);412 _this.attachCredentials(req, requester);413 req.end(function (err, res) {414 if (err) {415 reject({ message: _this.getErrorMessage(err, res) });416 return;417 }418 if (res.ok) {419 resolve(null);420 }421 else {422 var body = tryJSON(res.text);423 if (body) {424 reject(body);425 }426 else {427 reject({ message: res.text, statusCode: res.status });428 }429 }430 });431 });432 };433 AccountManager.prototype.updateApp = function (infoToChange) {434 var _this = this;435 return Promise(function (resolve, reject, notify) {436 var requester = (_this._authedAgent ? _this._authedAgent : request);437 var req = requester.put(_this.serverUrl + "/apps/" + infoToChange.id);438 _this.attachCredentials(req, requester);439 req.set("Content-Type", "application/json;charset=UTF-8")440 .send(JSON.stringify(infoToChange))441 .end(function (err, res) {442 if (err) {443 reject({ message: _this.getErrorMessage(err, res) });444 return;445 }446 if (res.ok) {447 resolve(null);448 }449 else {450 var body = tryJSON(res.text);451 if (body) {452 reject(body);453 }454 else {455 reject({ message: res.text, statusCode: res.status });456 }457 }458 });459 });460 };461 // Deployments462 AccountManager.prototype.addDeployment = function (appId, name, description) {463 var _this = this;464 return Promise(function (resolve, reject, notify) {465 var deployment = { name: name, description: description };466 var requester = (_this._authedAgent ? _this._authedAgent : request);467 var req = requester.post(_this.serverUrl + "/apps/" + appId + "/deployments/");468 ;469 _this.attachCredentials(req, requester);470 req.set("Content-Type", "application/json;charset=UTF-8")471 .send(JSON.stringify(deployment))472 .end(function (err, res) {473 if (err) {474 reject({ message: _this.getErrorMessage(err, res) });475 return;476 }477 if (res.ok) {478 var location = res.header["location"];479 if (location && location.lastIndexOf("/") !== -1) {480 deployment.id = location.substr(location.lastIndexOf("/") + 1);481 resolve(deployment);482 }483 else {484 resolve(null);485 }486 }487 else {488 var body = tryJSON(res.text);489 if (body) {490 reject(body);491 }492 else {493 reject({ message: res.text, statusCode: res.status });494 }495 }496 });497 });498 };499 AccountManager.prototype.getDeployments = function (appId) {500 var _this = this;501 return Promise(function (resolve, reject, notify) {502 var requester = (_this._authedAgent ? _this._authedAgent : request);503 var req = requester.get(_this.serverUrl + "/apps/" + appId + "/deployments");504 _this.attachCredentials(req, requester);505 req.end(function (err, res) {506 if (err) {507 reject({ message: _this.getErrorMessage(err, res) });508 return;509 }510 var body = tryJSON(res.text);511 if (res.ok) {512 if (body) {513 resolve(body.deployments);514 }515 else {516 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });517 }518 }519 else {520 if (body) {521 reject(body);522 }523 else {524 reject({ message: res.text, statusCode: res.status });525 }526 }527 });528 });529 };530 AccountManager.prototype.getDeployment = function (appId, deploymentId) {531 var _this = this;532 return Promise(function (resolve, reject, notify) {533 var requester = (_this._authedAgent ? _this._authedAgent : request);534 var req = requester.get(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId);535 _this.attachCredentials(req, requester);536 req.end(function (err, res) {537 if (err) {538 reject({ message: _this.getErrorMessage(err, res) });539 return;540 }541 var body = tryJSON(res.text);542 if (res.ok) {543 if (body) {544 resolve(body.deployment);545 }546 else {547 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });548 }549 }550 else {551 if (body) {552 reject(body);553 }554 else {555 reject({ message: res.text, statusCode: res.status });556 }557 }558 });559 });560 };561 AccountManager.prototype.updateDeployment = function (appId, infoToChange) {562 var _this = this;563 return Promise(function (resolve, reject, notify) {564 var requester = (_this._authedAgent ? _this._authedAgent : request);565 var req = requester.put(_this.serverUrl + "/apps/" + appId + "/deployments/" + infoToChange.id);566 _this.attachCredentials(req, requester);567 req.set("Content-Type", "application/json;charset=UTF-8")568 .send(JSON.stringify(infoToChange))569 .end(function (err, res) {570 if (err) {571 reject({ message: _this.getErrorMessage(err, res) });572 return;573 }574 if (res.ok) {575 resolve(null);576 }577 else {578 var body = tryJSON(res.text);579 if (body) {580 reject(body);581 }582 else {583 reject({ message: res.text, statusCode: res.status });584 }585 }586 });587 });588 };589 AccountManager.prototype.removeDeployment = function (appId, deployment) {590 var _this = this;591 return Promise(function (resolve, reject, notify) {592 var id = (typeof deployment === "string") ? deployment : deployment.id;593 var requester = (_this._authedAgent ? _this._authedAgent : request);594 var req = requester.del(_this.serverUrl + "/apps/" + appId + "/deployments/" + id);595 _this.attachCredentials(req, requester);596 req.end(function (err, res) {597 if (err) {598 reject({ message: _this.getErrorMessage(err, res) });599 return;600 }601 if (res.ok) {602 resolve(null);603 }604 else {605 var body = tryJSON(res.text);606 if (body) {607 reject(body);608 }609 else {610 reject({ message: res.text, statusCode: res.status });611 }612 }613 });614 });615 };616 // Deployment key617 AccountManager.prototype.addDeploymentKey = function (appId, deploymentId, name, description) {618 var _this = this;619 return Promise(function (resolve, reject, notify) {620 var deploymentKey = _this.generateDeploymentKey(name, description, false);621 var requester = (_this._authedAgent ? _this._authedAgent : request);622 var req = requester.post(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/deploymentKeys");623 _this.attachCredentials(req, requester);624 req.set("Content-Type", "application/json;charset=UTF-8")625 .send(JSON.stringify(deploymentKey))626 .end(function (err, res) {627 if (err) {628 reject({ message: _this.getErrorMessage(err, res) });629 return;630 }631 if (res.ok) {632 var body = tryJSON(res.text);633 if (res.ok) {634 if (body) {635 resolve(body.deploymentKey);636 }637 else {638 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });639 }640 }641 else {642 if (body) {643 reject(body);644 }645 else {646 reject({ message: res.text, statusCode: res.status });647 }648 }649 }650 });651 });652 };653 AccountManager.prototype.getDeploymentKeys = function (appId, deploymentId) {654 var _this = this;655 return Promise(function (resolve, reject, notify) {656 var requester = (_this._authedAgent ? _this._authedAgent : request);657 var req = requester.get(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/deploymentKeys");658 _this.attachCredentials(req, requester);659 req.end(function (err, res) {660 if (err) {661 reject({ message: _this.getErrorMessage(err, res) });662 return;663 }664 var body = tryJSON(res.text);665 if (res.ok) {666 if (body) {667 resolve(body.deploymentKeys);668 }669 else {670 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });671 }672 }673 else {674 if (body) {675 reject(body);676 }677 else {678 reject({ message: res.text, statusCode: res.status });679 }680 }681 });682 });683 };684 AccountManager.prototype.getDeploymentKey = function (appId, deploymentId, deploymentKeyId) {685 var _this = this;686 return Promise(function (resolve, reject, notify) {687 var requester = (_this._authedAgent ? _this._authedAgent : request);688 var req = requester.get(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/deploymentKeys/" + deploymentKeyId);689 _this.attachCredentials(req, requester);690 req.end(function (err, res) {691 if (err) {692 reject({ message: _this.getErrorMessage(err, res) });693 return;694 }695 var body = tryJSON(res.text);696 if (res.ok) {697 if (body) {698 resolve(body.deploymentKey);699 }700 else {701 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });702 }703 }704 else {705 if (body) {706 reject(body);707 }708 else {709 reject({ message: res.text, statusCode: res.status });710 }711 }712 });713 });714 };715 AccountManager.prototype.updateDeploymentKey = function (appId, deploymentId, deploymentKeyId, infoToChange) {716 var _this = this;717 return Promise(function (resolve, reject, notify) {718 var requester = (_this._authedAgent ? _this._authedAgent : request);719 var req = requester.put(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/deploymentKeys/" + deploymentKeyId);720 _this.attachCredentials(req, requester);721 req.set("Content-Type", "application/json;charset=UTF-8")722 .send(JSON.stringify(infoToChange))723 .end(function (err, res) {724 if (err) {725 reject({ message: _this.getErrorMessage(err, res) });726 return;727 }728 if (res.ok) {729 resolve(null);730 }731 else {732 var body = tryJSON(res.text);733 if (body) {734 reject(body);735 }736 else {737 reject({ message: res.text, statusCode: res.status });738 }739 }740 });741 });742 };743 AccountManager.prototype.deleteDeploymentKey = function (appId, deploymentId, deploymentKey) {744 var _this = this;745 var id = (typeof deploymentKey === "string") ? deploymentKey : deploymentKey.id;746 return Promise(function (resolve, reject, notify) {747 var requester = (_this._authedAgent ? _this._authedAgent : request);748 var req = requester.del(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/deploymentKeys/" + id);749 _this.attachCredentials(req, requester);750 req.end(function (err, res) {751 if (err) {752 reject({ message: _this.getErrorMessage(err, res) });753 return;754 }755 if (res.ok) {756 resolve(null);757 }758 else {759 var body = tryJSON(res.text);760 if (body) {761 reject(body);762 }763 else {764 reject({ message: res.text, statusCode: res.status });765 }766 }767 });768 });769 };770 AccountManager.prototype.addPackage = function (appId, deploymentId, fileOrPath, description, label, appVersion, isMandatory) {771 var _this = this;772 if (isMandatory === void 0) { isMandatory = false; }773 return Promise(function (resolve, reject, notify) {774 var packageInfo = _this.generatePackageInfo(description, label, appVersion, isMandatory);775 var requester = (_this._authedAgent ? _this._authedAgent : request);776 var req = requester.put(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/package");777 _this.attachCredentials(req, requester);778 var file;779 if (typeof fileOrPath === "string") {780 file = fs.createReadStream(fileOrPath);781 }782 else {783 file = fileOrPath;784 }785 req.field("package", file)786 .field("packageInfo", JSON.stringify(packageInfo))787 .end(function (err, res) {788 if (err) {789 reject({ message: _this.getErrorMessage(err, res) });790 return;791 }792 if (res.ok) {793 resolve(null);794 }795 else {796 var body = tryJSON(res.text);797 if (body) {798 reject(body);799 }800 else {801 reject({ message: res.text, statusCode: res.status });802 }803 }804 });805 });806 };807 AccountManager.prototype.promotePackage = function (appId, sourceDeploymentId, destDeploymentId) {808 var _this = this;809 return Promise(function (resolve, reject, notify) {810 var requester = (_this._authedAgent ? _this._authedAgent : request);811 var req = requester.post(_this.serverUrl + "/apps/" + appId + "/deployments/" + sourceDeploymentId + "/promote/" + destDeploymentId);812 _this.attachCredentials(req, requester);813 req.end(function (err, res) {814 if (err) {815 reject({ message: _this.getErrorMessage(err, res) });816 return;817 }818 if (res.ok) {819 resolve(null);820 }821 else {822 var body = tryJSON(res.text);823 if (body) {824 reject(body);825 }826 else {827 reject({ message: res.text, statusCode: res.status });828 }829 }830 });831 });832 };833 AccountManager.prototype.getPackage = function (appId, deploymentId) {834 var _this = this;835 return Promise(function (resolve, reject, notify) {836 var requester = (_this._authedAgent ? _this._authedAgent : request);837 var req = requester.get(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/package");838 _this.attachCredentials(req, requester);839 req.end(function (err, res) {840 if (err) {841 reject({ message: _this.getErrorMessage(err, res) });842 return;843 }844 var body = tryJSON(res.text);845 if (res.ok) {846 if (body) {847 resolve(body.package);848 }849 else {850 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });851 }852 }853 else {854 if (body) {855 reject(body);856 }857 else {858 reject({ message: res.text, statusCode: res.status });859 }860 }861 });862 });863 };864 AccountManager.prototype.getPackageHistory = function (appId, deploymentId) {865 var _this = this;866 return Promise(function (resolve, reject, notify) {867 var requester = (_this._authedAgent ? _this._authedAgent : request);868 var req = requester.get(_this.serverUrl + "/apps/" + appId + "/deployments/" + deploymentId + "/packageHistory");869 _this.attachCredentials(req, requester);870 req.end(function (err, res) {871 if (err) {872 reject({ message: _this.getErrorMessage(err, res) });873 return;874 }875 var body = tryJSON(res.text);876 if (res.ok) {877 if (body) {878 resolve(body.packageHistory);879 }880 else {881 reject({ message: "Could not parse response: " + res.text, statusCode: res.status });882 }883 }884 else {885 if (body) {886 reject(body);887 }888 else {889 reject({ message: res.text, statusCode: res.status });890 }891 }892 });893 });894 };895 AccountManager.getLoginInfo = function (accessKey) {896 try {897 var decoded = base64.decode(accessKey);898 return tryJSON(decoded);899 }900 catch (ex) {901 return null;902 }903 };904 AccountManager.prototype.getErrorMessage = function (error, response) {905 return response && response.text ? response.text : error.message;906 };907 AccountManager.prototype.generatePackageInfo = function (description, label, appVersion, isMandatory) {908 return {909 description: description,910 label: label,911 appVersion: appVersion,912 isMandatory: isMandatory...

Full Screen

Full Screen

data.js

Source:data.js Github

copy

Full Screen

...48 };49 request(_.extend(defaultReq, projectReq), function(err, res, body) {50 if (err) return doneFetchProjects(err);51 var projectsIds = [];52 _.each(tryJSON(body).childrenIds, function(id){53 projectsIds.push(parseInt(id));54 });55 projectsIds.push(parseInt(projectId));56 fetchEvents(projectsIds, doneFetchProjects);57 });58 }59 /**60 * Events61 **/62 function fetchEvents(projectsIds, doneFetchEvents){63 var eventsReq = {64 url: config.mapasCulturais.endpoint + '/event/find',65 qs: {66 '@select': 'id,name,shortDescription,classificacaoEtaria,terms,traducaoLibras,descricaoSonora',67 '@files': '(avatar,header):url',68 'project': 'in(' + projectsIds.join(',') + ')'69 }70 };71 request(_.extend(defaultReq, eventsReq), function(err, res, body) {72 if (err) return done(err);73 var events = tryJSON(body);74 if(!events)75 return false;76 if(!events.length)77 throw new Error('This project has no events');78 async.parallel([function(doneParallel){79 saveEvents(events, doneParallel);80 }, function(doneParallel){81 fetchOccurrences(events, doneParallel);82 }], function(err){83 if (err) console.log(err);84 doneFetchEvents(err);85 });86 });87 }88 function saveEvents(events, done) {89 async.eachSeries(events, function(event, doneEach){90 var occurrences = event.occurrences;91 delete event.occurrences;92 Event.update({_id: event['id']}, event, {upsert:true}, doneEach);93 }, done)94 }95 /**96 * Occurrences97 **/98 function fetchOccurrences(events, doneFetchOccurrences) {99 var eventsIds = [];100 _.each(events, function(event) {101 eventsIds.push(event.id);102 });103 eventsIds = eventsIds.join(',');104 var occursReqUrl = {105 url: config.mapasCulturais.endpoint + '/eventOccurrence/find?event=in(' + eventsIds + ')',106 qs: {107 '@select': 'id,eventId,rule',108 '@order': '_startsAt'109 }110 };111 request(_.extend(defaultReq, occursReqUrl), function(err, res, body) {112 if(err) return doneFetchOccurrences(err);113 var occurrences = tryJSON(body) || [];114 var spaceIds = [];115 _.each(occurrences, function(occurrence) {116 // Store space id for following spaces request117 spaceIds.push(occurrence.rule.spaceId);118 });119 async.parallel([function(doneParallel){120 saveOccurrences(occurrences, doneParallel);121 }, function(doneParallel){122 fetchSpaces(spaceIds, doneParallel);123 }], doneFetchOccurrences);124 });125 }126 function saveOccurrences(occurrences, done) {127 async.eachSeries(occurrences, function(occurrence, doneEach){128 async.series ([ function(cb) {129 Occurrence.update({_id: occurrence.id}, occurrence.rule, {upsert: true}, cb);130 }, function(cb){131 Event.update({_id: occurrence.eventId}, {$addToSet: {'occurrences': occurrence.id }}, {upsert: true}, cb);132 }], doneEach);133 }, done);134 }135 /**136 * Spaces137 **/138 function fetchSpaces(spacesIds, doneFetchSpaces) {139 var i, j, chunk = 500, chunked = [];140 for(i = 0, j = spacesIds.length; i<j; i += chunk) {141 chunked.push(spacesIds.slice(i, i+chunk));142 }143 var spaces = [];144 async.eachSeries(chunked, function(chunk, cb) {145 var ids = chunk.join(',');146 var req = {147 url: config.mapasCulturais.endpoint + '/space/find',148 qs: {149 '@select': 'id,name,shortDescription,endereco,location,acessibilidade',150 '@files': '(avatar.viradaSmall,avatar.viradaBig):url',151 'id': 'in(' + ids + ')',152 }153 };154 request(_.extend(defaultReq, req), function(err, res, body) {155 if(err) {156 cb(err);157 } else {158 spaces = spaces.concat(tryJSON(body) || []);159 cb();160 }161 });162 }, function(err) {163 async.eachSeries(spaces, function(space, doneEach){164 // space['_id'] = space.id;165 Space.update({ _id: space.id}, space, {upsert: true}, doneEach);166 }, doneFetchSpaces);167 });168 // // create string from array169 // spacesIds = spacesIds.join(',');170 //171 // // request config172 // var spacesReqUrl =173 //174 // // fetch data175 // request(_.extend(defaultReq, spacesReqUrl), function(err, res, body) {176 // if(err) return doneFetchSpaces(err);177 //178 // // parse result179 // var spaces = tryJSON(body) || [];180 //181 // // persist spaces to db182 // });183 }184 clearDB(fetchProjects(function(err){185 if (err) {186 console.log('Data sync had errors:');187 console.log(err);188 } else console.log('Data sync completed.');189 }));190};191if(!module.parent) {192 module.exports();193}

Full Screen

Full Screen

Store.js

Source:Store.js Github

copy

Full Screen

...6 return null7 }8}9class StoreClass {10 walletUUID = tryJSON(localStorage.getItem('walletUUID'))11 account = tryJSON(localStorage.getItem('account'))12 wallets = null13 offer = tryJSON(localStorage.getItem('offer'))14 transaction_uuid = tryJSON(localStorage.getItem('transaction_uuid'))15 setWalletUUID = (walletUUID) => {16 this.walletUUID = walletUUID17 localStorage.setItem('walletUUID', JSON.stringify(walletUUID))18 }19 setCurrentOffer = (offer) => {20 this.offer = offer21 localStorage.setItem('offer', JSON.stringify(offer))22 }23 clearOffer = () => {24 this.offer = null25 }26 setTransactionUUID = (uuid) => {27 this.transaction_uuid = uuid28 localStorage.setItem('transaction_uuid', JSON.stringify(uuid))...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var request = require('request');2var options = {3 headers: {4 },5 json: {6 "stubs": [{7 "responses": [{8 "is": {9 "headers": {10 },11 "body": {12 }13 }14 }]15 }]16 }17};18request(options, function(error, response, body) {19 if (!error && response.statusCode == 200) {20 console.log(body);21 }22});

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var imposter = {3 {4 {5 is: {6 body: '{ "status": "ok" }'7 }8 }9 }10};11mb.create(imposter).then(function () {12 console.log('Imposter created');13});

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var assert = require('assert');3var imposter = {4 {5 {6 is: {7 }8 }9 }10};11mb.create(imposter, function (error, imposter) {12 assert.equal(error, null);13 mb.get('/test', function (error, response) {14 assert.equal(error, null);15 assert.equal(response.body, 'Hello, World!');16 mb.stop(imposter.port, function (error) {17 assert.equal(error, null);18 });19 });20});21var mb = require('mountebank');22var assert = require('assert');23var imposter = {24 {25 {26 is: {27 }28 }29 }30};31mb.create(imposter, function (error, imposter) {32 assert.equal(error, null);33 mb.get('/test', function (error, response) {34 assert.equal(error, null);35 assert.equal(response.body, 'Hello, World!');36 mb.stop(imposter.port, function (error) {37 assert.equal(error, null);38 });39 });40});

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2mb.start({ port: 2525, pidfile: 'mb.pid', logfile: 'mb.log', ipWhitelist: ['*'] }, function () {3 console.log('mountebank started');4});5var imposter = {6 {7 {8 is: {9 }10 }11 }12};13mb.post('/imposters', imposter, function (error, response) {14 console.log('imposter created', response.body);15});16var stub = {17 {18 is: {19 }20 }21};22mb.post('/imposters/3000/stubs', stub, function (error, response) {23 console.log('stub created', response.body);24});25var predicate = {26 equals: {27 }28};29mb.post('/imposters/3000/predicates', predicate, function (error, response) {30 console.log('predicate created', response.body);31});32var proxy = {33};34mb.post('/imposters/3000/proxies', proxy, function (error, response) {35 console.log('proxy created', response.body);36});37var behavior = {38};39mb.post('/imposters/3000/behaviors', behavior, function (error, response) {40 console.log('behavior created', response.body);41});42var sequence = {43 {44 is: {45 }46 }47};48mb.post('/imposters/3000/sequences', sequence, function (error, response) {49 console.log('sequence created', response.body);50});51var injection = {52 inject: 'var x = 1;'53};54mb.post('/imposters/3000/in

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var assert = require('assert');3var port = 2525;4var imposterPort = 3000;5var imposter = {6 {7 {8 is: {9 }10 }11 }12};13mb.create(port, imposter)14 .then(function (api) {15 return api.get('/', 3000);16 })17 .then(function (response) {18 assert.equal(response.body, 'Hello World!');19 })20 .finally(mb.stopAll)21 .done();

Full Screen

Using AI Code Generation

copy

Full Screen

1const mb = require('mountebank');2mb.create().then(imposters => {3 imposters[0].addStub({4 responses: [{5 is: { statusCode: 200, body: '{ "foo": "bar" }' }6 }]7 });8 imposters[0].addStub({9 responses: [{10 is: { statusCode: 200, body: '{ "foo": "baz" }' }11 }]12 });13});14const mb = require('mountebank');15mb.create().then(imposters => {16 imposters[0].addStub({17 responses: [{18 is: { statusCode: 200, body: '{ "foo": "bar" }' }19 }]20 });21 imposters[0].addStub({22 responses: [{23 is: { statusCode: 200, body: '{ "foo": "baz" }' }24 }]25 });26});27const mb = require('mountebank');28mb.create().then(imposters => {29 imposters[0].addStub({30 responses: [{31 is: { statusCode: 200, body: '{ "foo": "bar" }' }32 }]33 });34 imposters[0].addStub({35 responses: [{36 is: { statusCode: 200, body: '{ "foo": "baz" }' }37 }]38 });39});40const mb = require('mountebank');41mb.create().then(imposters => {42 imposters[0].addStub({43 responses: [{44 is: { statusCode: 200, body: '{ "foo": "bar" }' }45 }]46 });47 imposters[0].addStub({48 responses: [{49 is: { statusCode: 200, body: '{ "foo": "baz" }' }50 }]51 });52});53const mb = require('mountebank');54mb.create().then(imposters => {55 imposters[0].addStub({56 responses: [{57 is: { statusCode: 200, body: '{ "foo":

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var assert = require('assert');3var mbHelper = require('./mbHelper.js');4var imposterPort = 2525;5var imposterPort1 = 2526;6var imposterPort2 = 2527;7var imposterPort3 = 2528;8var imposterPort4 = 2529;9var imposterPort5 = 2530;10var imposterPort6 = 2531;11var imposterPort7 = 2532;12var imposterPort8 = 2533;13var imposterPort9 = 2534;14var imposterPort10 = 2535;15var imposterPort11 = 2536;16var imposterPort12 = 2537;17var imposterPort13 = 2538;18var imposterPort14 = 2539;19var imposterPort15 = 2540;20var imposterPort16 = 2541;21var imposterPort17 = 2542;22var imposterPort18 = 2543;23var imposterPort19 = 2544;24var imposterPort20 = 2545;25var imposterPort21 = 2546;26var imposterPort22 = 2547;27var imposterPort23 = 2548;28var imposterPort24 = 2549;29var imposterPort25 = 2550;30var imposterPort26 = 2551;31var imposterPort27 = 2552;32var imposterPort28 = 2553;33var imposterPort29 = 2554;34var imposterPort30 = 2555;35var imposterPort31 = 2556;36var imposterPort32 = 2557;37var imposterPort33 = 2558;38var imposterPort34 = 2559;39var imposterPort35 = 2560;40var imposterPort36 = 2561;41var imposterPort37 = 2562;42var imposterPort38 = 2563;43var imposterPort39 = 2564;44var imposterPort40 = 2565;45var imposterPort41 = 2566;46var imposterPort42 = 2567;47var imposterPort43 = 2568;48var imposterPort44 = 2569;49var imposterPort45 = 2570;50var imposterPort46 = 2571;

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var imposter = mb.create();3var stub = mb.createStub();4var predicate = mb.createPredicate();5var response = mb.createResponse();6imposter.port = 4545;7imposter.protocol = 'http';8predicate.matches.path = '/test';9predicate.matches.method = 'GET';10response.is.body = 'Hello World';11response.is.statusCode = 200;12stub.addPredicate(predicate);13stub.addResponse(response);14imposter.addStub(stub);15imposter.create(function (error, result) {16 if (error) {17 console.log('Error creating imposter: ' + error);18 } else {19 console.log('Imposter created successfully');20 }21});22var mb = require('mountebank');23var imposter = mb.create();24var stub = mb.createStub();25var predicate = mb.createPredicate();26var response = mb.createResponse();27imposter.port = 4545;28imposter.protocol = 'http';29predicate.matches.path = '/test2';30predicate.matches.method = 'GET';31response.is.body = 'Hello World 2';

Full Screen

Using AI Code Generation

copy

Full Screen

1var mountebank = require('mountebank');2var request = require('request');3var mb = mountebank.create('localhost', 2525);4var port = 2525;5var stub = {6 {7 is: {8 }9 }10};11mb.post('/imposters', {12}).then(function (response) {13 console.log('POST /imposters => ' + response.statusCode);14 return mb.get('/imposters');15}).then(function (response) {16 console.log('GET /imposters => ' + response.statusCode);17 console.log(JSON.stringify(response.body));18 return mb.del('/imposters');19}).then(function (response) {20 console.log('DELETE /imposters => ' + response.statusCode);21 return mb.get('/imposters');22}).then(function (response) {23 console.log('GET /imposters => ' + response.statusCode);24 console.log(JSON.stringify(response.body));25 console.log('GET / => ' + response.statusCode);26 console.log(body);27 });28});29{"imposters":[{"port":2525,"protocol":"http","stubs":[{"responses":[{"is":{"statusCode":200,"body":"Hello World!"}}]}]}]}30{"imposters":[]}31{"imposters":[{"port":2525,"protocol":"http","stubs":[{"responses":[{"is":{"statusCode":200,"body":"Hello World!"}}]}]}]}

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run mountebank automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful