|
| 1 | +/** |
| 2 | + * Copyright 2016 IBM Corp. All Rights Reserved. |
| 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 | +'use strict'; |
| 18 | + |
| 19 | +var requestFactory = require('../lib/requestwrapper'); |
| 20 | +var pick = require('object.pick'); |
| 21 | +var extend = require('extend'); |
| 22 | +var helper = require('../lib/helper'); |
| 23 | +var util = require('util'); |
| 24 | +var BaseService = require('../lib/base_service'); |
| 25 | + |
| 26 | +/** |
| 27 | + * |
| 28 | + * @param options |
| 29 | + * @constructor |
| 30 | + */ |
| 31 | +function PersonalityInsightsV3(options) { |
| 32 | + BaseService.call(this, options); |
| 33 | + |
| 34 | + // Check if 'version_date' was provided |
| 35 | + if (typeof this._options.version_date === 'undefined') { |
| 36 | + throw new Error('Argument error: version_date was not specified, use 2016-10-19'); |
| 37 | + } |
| 38 | + this._options.qs.version = options.version_date; |
| 39 | + |
| 40 | +} |
| 41 | +util.inherits(PersonalityInsightsV3, BaseService); |
| 42 | +PersonalityInsightsV3.prototype.name = 'personality_insights'; |
| 43 | +PersonalityInsightsV3.prototype.version = 'v3'; |
| 44 | +PersonalityInsightsV3.URL = 'https://gateway.watsonplatform.net/personality-insights/api'; |
| 45 | + |
| 46 | + |
| 47 | +/** |
| 48 | + * @param {Object} params The parameters to call the service |
| 49 | + * @param {Object} [params.headers] - The header parameters. |
| 50 | + * @param {string} [params.headers.accept-language=en] - The desired language of the response: ar (Arabic), en (English), es (Spanish), or ja (Japanese). |
| 51 | + * @param {string} [params.headers.content-type=text/plain] - The content type of the request: text/plain (the default), text/html, or application/json. |
| 52 | + * @param {string} [params.headers.content-language=en] - The language of the input text for the request: ar (Arabic), en (English), es (Spanish), or ja (Japanese) |
| 53 | + * @param {string} [params.headers.accept=application/json] - The desired content type of the response: application/json (the default) or text/csv |
| 54 | + * @param {string} [params.text] - The text to analyze. |
| 55 | + * @param {Object} [params.content_items] - A JSON input (if 'text' not provided). |
| 56 | + * @param {boolean} [params.raw_scores=false] - include raw results. |
| 57 | + * @param {boolean} [params.consumption_preferences=true] - If true, information about consumption preferences is returned with the results. |
| 58 | + * |
| 59 | + * @param callback The callback. |
| 60 | + */ |
| 61 | +PersonalityInsightsV3.prototype.profile = function(_params, callback) { // eslint-disable-line complexity |
| 62 | + var params = extend ({}, _params); |
| 63 | + |
| 64 | + if (params.content_items) { |
| 65 | + params.contentItems = params.content_items; |
| 66 | + } |
| 67 | + |
| 68 | + if ((!params.text && !params.contentItems)) { |
| 69 | + callback(new Error('Missing required parameters: text or content_items')); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Content-Type |
| 74 | + var content_type = null; |
| 75 | + if (params.text) |
| 76 | + content_type = helper.isHTML(params.text) ? 'text/html' : 'text/plain'; |
| 77 | + else |
| 78 | + content_type = 'application/json'; |
| 79 | + |
| 80 | + var parameters = { |
| 81 | + options: { |
| 82 | + method: 'POST', |
| 83 | + url: '/v3/profile', |
| 84 | + body: params.text || pick(params, ['contentItems']), |
| 85 | + json: true, |
| 86 | + qs: pick(params, ['csv_headers', 'raw_scores', 'consumption_preferences']), |
| 87 | + headers: extend({ 'content-type': content_type, 'accept-language': 'en' }, params.headers) |
| 88 | + }, |
| 89 | + defaultOptions: this._options |
| 90 | + }; |
| 91 | + |
| 92 | + return requestFactory(parameters, callback); |
| 93 | +}; |
| 94 | +module.exports = PersonalityInsightsV3; |
0 commit comments