//This content is released under the MIT License, http://opensource.org/licenses/MIT. See licence.txt for more details.
var Utils = require("./Utils");
var Constants = require("./Constants");
var nets = require("nets");
/**
* @constructor
* @param {string} baseURL - URL for the Open PHACTS API
* @param {string} appID - Application ID for the application being used. Created by {@link https://dev.openphacts.org}
* @param {string} appKey - Application Key for the application ID.
* @license [MIT]{@link http://opensource.org/licenses/MIT}
* @author [Ian Dunlop]{@link https://github.com/ianwdunlop}
*/
MapSearch = function MapSearch(baseURL, appID, appKey) {
this.baseURL = baseURL;
this.appID = appID;
this.appKey = appKey;
}
MapSearch.prototype.mapURL = function(URI, targetUriPattern, graph, lens, callback) {
params={};
params['_format'] = "json";
params['app_key'] = this.appKey;
params['app_id'] = this.appID;
params['Uri'] = URI;
targetUriPattern ? params['targetUriPattern'] = targetUriPattern : '';
graph ? params['graph'] = graph : '';
lens ? params['lensUri'] = lens : '';
Utils.nets({
url: this.baseURL + '/mapUri?' + Utils.encodeParams(params),
method: "GET",
// 30 second timeout just in case
timeout: 30000,
headers: {
"Accept": "application/json"
}
}, function(err, resp, body) {
if (resp.statusCode === 200) {
callback.call(this, true, resp.statusCode, JSON.parse(body.toString()).result);
} else {
callback.call(this, false, resp.statusCode);
}
});
}
MapSearch.prototype.parseMapURLResponse = function(response) {
var constants = new Constants();
var items = response.primaryTopic[constants.EXACT_MATCH];
var urls = [];
Utils.arrayify(items).forEach(function(item, i) {
urls.push(item);
});
return urls;
}
exports.MapSearch = MapSearch;