{"action":"created","comment":{"url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/comments/65183824","id":65183824,"diff_hunk":"@@ -0,0 +1,183 @@\n+/**\n+ * Copyright 2013,2016 IBM Corp.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ **/\n+\n+module.exports = function (RED) {\n+  var watson = require('watson-developer-cloud');  \n+  var cfenv = require('cfenv');\n+\n+  // Require the Cloud Foundry Module to pull credentials from bound service \n+  // If they are found then they are stored in sUsername and sPassword, as the \n+  // service credentials. This separation from sUsername and username to allow \n+  // the end user to modify the node credentials when the service is not bound.\n+  // Otherwise, once set username would never get reset, resulting in a frustrated\n+  // user who, when he errenously enters bad credentials, can't figure out why\n+  // the edited ones are not being taken.\n+\n+  // Not ever used, and codeacy complains about it.\n+\n+  var username, password, sUsername, sPassword;\n+\n+  var service = cfenv.getAppEnv().getServiceCreds(/tone analyzer/i)\n+\n+  if (service) {\n+    sUsername = service.username;\n+    sPassword = service.password;\n+  }\n+\n+  // Node RED Admin - fetch and set vcap services\n+  RED.httpAdmin.get('/watson-tone-analyzer/vcap', function (req, res) {\n+    res.json(service ? {bound_service: true} : null);\n+  });\n+\n+  // Function that checks the payload and determines\n+  // whether it is JSON or a Buffer\n+  var checkPayload = function(payload) {\n+    var message = null;\n+    var isBuffer = false;\n+\n+    var hasJSONmethod = (typeof payload.toJSON === 'function') ;\n+\n+    if (hasJSONmethod === true) {\n+      if (payload.toJSON().type === 'Buffer') {\n+        isBuffer = true;\n+      }      \n+    }      \n+    // Payload (text to be analysed) must be a string (content is either raw string or Buffer)\n+    if (typeof payload !== 'string' &&  isBuffer !== true) {\n+      message = 'The payload must be either a string or a Buffer';\n+    }\n+\n+    return message;\n+  };\n+\n+  // Check that the credentials have been provided\n+  // Credentials are needed for each the service.\n+  var checkCreds = function(credentials) {\n+    var taSettings = null;\n+\n+    username = sUsername || credentials.username;\n+    password = sPassword || credentials.password;      \n+\n+    if (username && password) {\n+      taSettings = {};\n+      taSettings.username = username;\n+      taSettings.password = password;\n+    }\n+\n+    return taSettings;\n+  }\n+\n+\n+  // Function that checks the configuration to make sure that credentials,\n+  // payload and options have been proviced in the correct format.\n+  var checkConfiguration = function(msg, node, cb) {\n+    var message = null;      \n+    var taSettings = null;\n+\n+    taSettings = checkCreds(node.credentials);\n+\n+    if (!taSettings) {\n+      message = 'Missing Tone Analyzer service credentials';\n+    }\n+\n+    if (!message && !msg.payload) {\n+      message = 'Missing property: msg.payload';\n+    } \n+\n+    if (!message) {\n+      message = checkPayload(msg.payload);\n+    }\n+\n+    if (cb) {\n+      cb(message, taSettings);\n+    }\n+  };\n+\n+  // function to parse through the options in preparation\n+  // for the sevice call.\n+  var parseOptions = function (msg, config) {","path":"services/tone_analyzer/v3.js","position":111,"original_position":111,"commit_id":"2abc512e465061adf4abbf5b76385f3ae77b847f","original_commit_id":"2abc512e465061adf4abbf5b76385f3ae77b847f","user":{"login":"yrezgui","id":1113976,"avatar_url":"https://avatars.githubusercontent.com/u/1113976?v=3","gravatar_id":"","url":"https://api.github.com/users/yrezgui","html_url":"https://github.com/yrezgui","followers_url":"https://api.github.com/users/yrezgui/followers","following_url":"https://api.github.com/users/yrezgui/following{/other_user}","gists_url":"https://api.github.com/users/yrezgui/gists{/gist_id}","starred_url":"https://api.github.com/users/yrezgui/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yrezgui/subscriptions","organizations_url":"https://api.github.com/users/yrezgui/orgs","repos_url":"https://api.github.com/users/yrezgui/repos","events_url":"https://api.github.com/users/yrezgui/events{/privacy}","received_events_url":"https://api.github.com/users/yrezgui/received_events","type":"User","site_admin":false},"body":"![Codacy](https://www.codacy.com/assets/images/favicon.png) Issue found: [Function 'anonymous' has a complexity of 5.](https://www.codacy.com/app/rezgui-y/node-red-node-watson/file/2654803696/issues/source?bid=3225115&fileBranchId=3326618#l111)","created_at":"2016-05-31T13:47:20Z","updated_at":"2016-05-31T13:47:20Z","html_url":"https://github.com/watson-developer-cloud/node-red-node-watson/pull/99#discussion_r65183824","pull_request_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99","_links":{"self":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/comments/65183824"},"html":{"href":"https://github.com/watson-developer-cloud/node-red-node-watson/pull/99#discussion_r65183824"},"pull_request":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99"}}},"pull_request":{"url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99","id":71900820,"html_url":"https://github.com/watson-developer-cloud/node-red-node-watson/pull/99","diff_url":"https://github.com/watson-developer-cloud/node-red-node-watson/pull/99.diff","patch_url":"https://github.com/watson-developer-cloud/node-red-node-watson/pull/99.patch","issue_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/issues/99","number":99,"state":"closed","locked":false,"title":"New Tone Analysis node for V3 GA API","user":{"login":"chughts","id":10371360,"avatar_url":"https://avatars.githubusercontent.com/u/10371360?v=3","gravatar_id":"","url":"https://api.github.com/users/chughts","html_url":"https://github.com/chughts","followers_url":"https://api.github.com/users/chughts/followers","following_url":"https://api.github.com/users/chughts/following{/other_user}","gists_url":"https://api.github.com/users/chughts/gists{/gist_id}","starred_url":"https://api.github.com/users/chughts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chughts/subscriptions","organizations_url":"https://api.github.com/users/chughts/orgs","repos_url":"https://api.github.com/users/chughts/repos","events_url":"https://api.github.com/users/chughts/events{/privacy}","received_events_url":"https://api.github.com/users/chughts/received_events","type":"User","site_admin":false},"body":"A new Tone Analyzer node developed to support the newly GA version of the API. The old V3 beta node has been retained for backward compatibility so that any existing flows with valid (old credentials) are not broken.\r\nThe package pre-reqs have been updated to the the level of the watson-developer-cloud SDK that provides support for the GA Tone Analyser API (1.9.2)\r\nThe functions have been simplified to pass through codacy checks.","created_at":"2016-05-30T13:20:27Z","updated_at":"2016-05-31T13:47:20Z","closed_at":"2016-05-30T13:23:08Z","merged_at":null,"merge_commit_sha":"6052e8e0d6a97d6f318ec63a11590c389cbc6073","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99/commits","review_comments_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99/comments","review_comment_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/comments{/number}","comments_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/issues/99/comments","statuses_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/statuses/2abc512e465061adf4abbf5b76385f3ae77b847f","head":{"label":"chughts:master","ref":"master","sha":"2abc512e465061adf4abbf5b76385f3ae77b847f","user":{"login":"chughts","id":10371360,"avatar_url":"https://avatars.githubusercontent.com/u/10371360?v=3","gravatar_id":"","url":"https://api.github.com/users/chughts","html_url":"https://github.com/chughts","followers_url":"https://api.github.com/users/chughts/followers","following_url":"https://api.github.com/users/chughts/following{/other_user}","gists_url":"https://api.github.com/users/chughts/gists{/gist_id}","starred_url":"https://api.github.com/users/chughts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chughts/subscriptions","organizations_url":"https://api.github.com/users/chughts/orgs","repos_url":"https://api.github.com/users/chughts/repos","events_url":"https://api.github.com/users/chughts/events{/privacy}","received_events_url":"https://api.github.com/users/chughts/received_events","type":"User","site_admin":false},"repo":{"id":58115630,"name":"node-red-node-watson","full_name":"chughts/node-red-node-watson","owner":{"login":"chughts","id":10371360,"avatar_url":"https://avatars.githubusercontent.com/u/10371360?v=3","gravatar_id":"","url":"https://api.github.com/users/chughts","html_url":"https://github.com/chughts","followers_url":"https://api.github.com/users/chughts/followers","following_url":"https://api.github.com/users/chughts/following{/other_user}","gists_url":"https://api.github.com/users/chughts/gists{/gist_id}","starred_url":"https://api.github.com/users/chughts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chughts/subscriptions","organizations_url":"https://api.github.com/users/chughts/orgs","repos_url":"https://api.github.com/users/chughts/repos","events_url":"https://api.github.com/users/chughts/events{/privacy}","received_events_url":"https://api.github.com/users/chughts/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/chughts/node-red-node-watson","description":"A collection of nodes for the IBM Watson services","fork":true,"url":"https://api.github.com/repos/chughts/node-red-node-watson","forks_url":"https://api.github.com/repos/chughts/node-red-node-watson/forks","keys_url":"https://api.github.com/repos/chughts/node-red-node-watson/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chughts/node-red-node-watson/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chughts/node-red-node-watson/teams","hooks_url":"https://api.github.com/repos/chughts/node-red-node-watson/hooks","issue_events_url":"https://api.github.com/repos/chughts/node-red-node-watson/issues/events{/number}","events_url":"https://api.github.com/repos/chughts/node-red-node-watson/events","assignees_url":"https://api.github.com/repos/chughts/node-red-node-watson/assignees{/user}","branches_url":"https://api.github.com/repos/chughts/node-red-node-watson/branches{/branch}","tags_url":"https://api.github.com/repos/chughts/node-red-node-watson/tags","blobs_url":"https://api.github.com/repos/chughts/node-red-node-watson/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chughts/node-red-node-watson/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chughts/node-red-node-watson/git/refs{/sha}","trees_url":"https://api.github.com/repos/chughts/node-red-node-watson/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chughts/node-red-node-watson/statuses/{sha}","languages_url":"https://api.github.com/repos/chughts/node-red-node-watson/languages","stargazers_url":"https://api.github.com/repos/chughts/node-red-node-watson/stargazers","contributors_url":"https://api.github.com/repos/chughts/node-red-node-watson/contributors","subscribers_url":"https://api.github.com/repos/chughts/node-red-node-watson/subscribers","subscription_url":"https://api.github.com/repos/chughts/node-red-node-watson/subscription","commits_url":"https://api.github.com/repos/chughts/node-red-node-watson/commits{/sha}","git_commits_url":"https://api.github.com/repos/chughts/node-red-node-watson/git/commits{/sha}","comments_url":"https://api.github.com/repos/chughts/node-red-node-watson/comments{/number}","issue_comment_url":"https://api.github.com/repos/chughts/node-red-node-watson/issues/comments{/number}","contents_url":"https://api.github.com/repos/chughts/node-red-node-watson/contents/{+path}","compare_url":"https://api.github.com/repos/chughts/node-red-node-watson/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chughts/node-red-node-watson/merges","archive_url":"https://api.github.com/repos/chughts/node-red-node-watson/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chughts/node-red-node-watson/downloads","issues_url":"https://api.github.com/repos/chughts/node-red-node-watson/issues{/number}","pulls_url":"https://api.github.com/repos/chughts/node-red-node-watson/pulls{/number}","milestones_url":"https://api.github.com/repos/chughts/node-red-node-watson/milestones{/number}","notifications_url":"https://api.github.com/repos/chughts/node-red-node-watson/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chughts/node-red-node-watson/labels{/name}","releases_url":"https://api.github.com/repos/chughts/node-red-node-watson/releases{/id}","deployments_url":"https://api.github.com/repos/chughts/node-red-node-watson/deployments","created_at":"2016-05-05T08:19:28Z","updated_at":"2016-05-14T07:16:53Z","pushed_at":"2016-05-31T12:55:15Z","git_url":"git://github.com/chughts/node-red-node-watson.git","ssh_url":"git@github.com:chughts/node-red-node-watson.git","clone_url":"https://github.com/chughts/node-red-node-watson.git","svn_url":"https://github.com/chughts/node-red-node-watson","homepage":null,"size":7759,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"watson-developer-cloud:master","ref":"master","sha":"001821ba9f1b43061389d162bd3c67577fd2715b","user":{"login":"watson-developer-cloud","id":10017763,"avatar_url":"https://avatars.githubusercontent.com/u/10017763?v=3","gravatar_id":"","url":"https://api.github.com/users/watson-developer-cloud","html_url":"https://github.com/watson-developer-cloud","followers_url":"https://api.github.com/users/watson-developer-cloud/followers","following_url":"https://api.github.com/users/watson-developer-cloud/following{/other_user}","gists_url":"https://api.github.com/users/watson-developer-cloud/gists{/gist_id}","starred_url":"https://api.github.com/users/watson-developer-cloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/watson-developer-cloud/subscriptions","organizations_url":"https://api.github.com/users/watson-developer-cloud/orgs","repos_url":"https://api.github.com/users/watson-developer-cloud/repos","events_url":"https://api.github.com/users/watson-developer-cloud/events{/privacy}","received_events_url":"https://api.github.com/users/watson-developer-cloud/received_events","type":"Organization","site_admin":false},"repo":{"id":55415107,"name":"node-red-node-watson","full_name":"watson-developer-cloud/node-red-node-watson","owner":{"login":"watson-developer-cloud","id":10017763,"avatar_url":"https://avatars.githubusercontent.com/u/10017763?v=3","gravatar_id":"","url":"https://api.github.com/users/watson-developer-cloud","html_url":"https://github.com/watson-developer-cloud","followers_url":"https://api.github.com/users/watson-developer-cloud/followers","following_url":"https://api.github.com/users/watson-developer-cloud/following{/other_user}","gists_url":"https://api.github.com/users/watson-developer-cloud/gists{/gist_id}","starred_url":"https://api.github.com/users/watson-developer-cloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/watson-developer-cloud/subscriptions","organizations_url":"https://api.github.com/users/watson-developer-cloud/orgs","repos_url":"https://api.github.com/users/watson-developer-cloud/repos","events_url":"https://api.github.com/users/watson-developer-cloud/events{/privacy}","received_events_url":"https://api.github.com/users/watson-developer-cloud/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/watson-developer-cloud/node-red-node-watson","description":"A collection of nodes for the IBM Watson services","fork":true,"url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson","forks_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/forks","keys_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/keys{/key_id}","collaborators_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/teams","hooks_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/hooks","issue_events_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/issues/events{/number}","events_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/events","assignees_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/assignees{/user}","branches_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/branches{/branch}","tags_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/tags","blobs_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/git/refs{/sha}","trees_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/git/trees{/sha}","statuses_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/statuses/{sha}","languages_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/languages","stargazers_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/stargazers","contributors_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/contributors","subscribers_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/subscribers","subscription_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/subscription","commits_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/commits{/sha}","git_commits_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/git/commits{/sha}","comments_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/comments{/number}","issue_comment_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/issues/comments{/number}","contents_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/contents/{+path}","compare_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/compare/{base}...{head}","merges_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/merges","archive_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/downloads","issues_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/issues{/number}","pulls_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls{/number}","milestones_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/milestones{/number}","notifications_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/labels{/name}","releases_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/releases{/id}","deployments_url":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/deployments","created_at":"2016-04-04T13:51:28Z","updated_at":"2016-05-30T13:10:27Z","pushed_at":"2016-05-31T12:55:15Z","git_url":"git://github.com/watson-developer-cloud/node-red-node-watson.git","ssh_url":"git@github.com:watson-developer-cloud/node-red-node-watson.git","clone_url":"https://github.com/watson-developer-cloud/node-red-node-watson.git","svn_url":"https://github.com/watson-developer-cloud/node-red-node-watson","homepage":null,"size":7813,"stargazers_count":8,"watchers_count":8,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":13,"mirror_url":null,"open_issues_count":7,"forks":13,"open_issues":7,"watchers":8,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99"},"html":{"href":"https://github.com/watson-developer-cloud/node-red-node-watson/pull/99"},"issue":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/issues/99"},"comments":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/issues/99/comments"},"review_comments":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99/comments"},"review_comment":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/pulls/99/commits"},"statuses":{"href":"https://api.github.com/repos/watson-developer-cloud/node-red-node-watson/statuses/2abc512e465061adf4abbf5b76385f3ae77b847f"}}}}
{"actor":{"display_login":"yrezgui"}}