Skip to content

Commit 77c9485

Browse files
committed
moved payload checing into a utilties module
1 parent e69e5bd commit 77c9485

2 files changed

Lines changed: 56 additions & 21 deletions

File tree

services/tone_analyzer/v3.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
module.exports = function (RED) {
1818
var watson = require('watson-developer-cloud');
1919
var cfenv = require('cfenv');
20+
var toneutils = require('../../utilities/tone-utils');
2021

2122
// Require the Cloud Foundry Module to pull credentials from bound service
2223
// If they are found then they are stored in sUsername and sPassword, as the
@@ -37,31 +38,13 @@ module.exports = function (RED) {
3738
sPassword = service.password;
3839
}
3940

41+
//toneutils.ping();
42+
4043
// Node RED Admin - fetch and set vcap services
4144
RED.httpAdmin.get('/watson-tone-analyzer/vcap', function (req, res) {
4245
res.json(service ? {bound_service: true} : null);
4346
});
4447

45-
// Function that checks the payload and determines
46-
// whether it is JSON or a Buffer
47-
var checkPayload = function(payload) {
48-
var message = null;
49-
var isBuffer = false;
50-
51-
var hasJSONmethod = (typeof payload.toJSON === 'function') ;
52-
53-
if (hasJSONmethod === true) {
54-
if (payload.toJSON().type === 'Buffer') {
55-
isBuffer = true;
56-
}
57-
}
58-
// Payload (text to be analysed) must be a string (content is either raw string or Buffer)
59-
if (typeof payload !== 'string' && isBuffer !== true) {
60-
message = 'The payload must be either a string or a Buffer';
61-
}
62-
63-
return message;
64-
};
6548

6649
// Check that the credentials have been provided
6750
// Credentials are needed for each the service.
@@ -92,7 +75,7 @@ module.exports = function (RED) {
9275
if (!taSettings) {
9376
message = 'Missing Tone Analyzer service credentials';
9477
} else if (msg.payload) {
95-
message = checkPayload(msg.payload);
78+
message = toneutils.checkPayload(msg.payload);
9679
} else {
9780
message = 'Missing property: msg.payload';
9881
}

utilities/tone-utils.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright 2016 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
**/
16+
17+
function ToneUtils () {
18+
}
19+
20+
21+
ToneUtils.prototype = {
22+
check: function () {
23+
return '"IBM Watson Node-RED Utilities for Tone Analyser';
24+
},
25+
26+
// Function that checks the payload and determines
27+
// whether it is JSON or a Buffer
28+
checkPayload: function(payload) {
29+
var message = null;
30+
var isBuffer = false;
31+
32+
var hasJSONmethod = (typeof payload.toJSON === 'function') ;
33+
34+
if (hasJSONmethod === true) {
35+
if (payload.toJSON().type === 'Buffer') {
36+
isBuffer = true;
37+
}
38+
}
39+
// Payload (text to be analysed) must be a string (content is either raw string or Buffer)
40+
if (typeof payload !== 'string' && isBuffer !== true) {
41+
message = 'The payload must be either a string or a Buffer';
42+
}
43+
44+
return message;
45+
}
46+
47+
48+
};
49+
50+
var toneutils = new ToneUtils();
51+
52+
module.exports = toneutils;

0 commit comments

Comments
 (0)