<commit_before>/**
 * @module telize
 * @description Client for Telize API.
 * @version 1.0.4
 * @author <NAME> [<EMAIL>]
 * @copyright Copyright (c) 2015 <NAME>
 * @license MIT
 */

"use strict";

/**
 * @private
 * @description Module dependencies.
 */

var http = require("http");
var freeze = require("deep-freeze-node");

/**
 * @private
 * @constant {String}
 * @description Module version.
 */

var VERSION = require('../package.json').version;

/**
 * @public
 * @constructor
 * @description Initialize instance of Telize with default request options.
 * @property {Object} defaultRequestOptions - Default request options.
 * @property {String} defaultRequestOptions.hostname - Default request hostname.
 * @property {Number} defaultRequestOptions.port - Default request port.
 * @property {String} defaultRequestOptions.basePath - Default request base path.
 */

function Telize() {
    this.defaultRequestOptions = {
        hostname: "www.telize.com",
        port: 80,
        basePath: ""
    };
}

/**
 * @public
 * @function getIP
 * @description Request current IP address.
 * @param {getIP~callback} callback - Callback when response comes in.
 */

Telize.prototype.getIP = function(callback) {
    if((typeof callback !== "function") || !(callback instanceof Function)) {
        throw new Error("getIP(): callback is undefined or contains non-function value");
    }

    request(this.defaultRequestOptions, "/jsonip", function(error, rawResponse) {
        var parsedResponse;
        if(!error) {
            try {
                parsedResponse = JSON.parse(rawResponse);
            } catch(error) {
                callback(error);
            }
            callback(null, parsedResponse.ip);
        } else {
            callback(error);
        }
    });
};

/**
 * @callback getIP~callback
 * @description Use as callback in getIP function.
 * @param {Object} error - Generic error object.
 * @param {String} ip - IP address.
 */

/**
 * @public
 * @function getGeoIP
 * @description Request Geo IP data.
 * @param {String} [ip] - IP address.
 * @param {getGeoIP~callback} callback - Callback when response comes in.
 */

Telize.prototype.getGeoIP = function(ip, callback) {
    if((typeof ip === "function") || (ip instanceof Function)) {
        callback = ip;
        ip = undefined;
    } else if((typeof callback !== "function") || !(callback instanceof Function)) {
        throw new Error("getGeoIP(): callback is undefined or contains non-function value");
    }

    request(this.defaultRequestOptions, "/geoip" + (ip ? "/" + ip : ""), function(error, rawResponse) {
        if(!error) {
            try {
                var parsedResponse = JSON.parse(rawResponse);
            } catch(error) {
                callback(error);
            }
            if(parsedResponse.code && parsedResponse.message) {
                callback(new Error(parsedResponse.message, parsedResponse.code));
            } else {
                callback(null, parsedResponse);
            }
        } else {
            callback(error);
        }
    });
};

/**
 * @callback getGeoIP~callback
 * @description Use as callback in getGeoIP function.
 * @param {Object} error - Generic error object.
 * @param {Object} data - Geo IP data object.
 */

/**
 * @private
 * @function request
 * @description Perform HTTP GET request.
 * @param options - Request options.
 * @param path - Request path.<commit_msg>[CodeClimate] Fix 'parsedResponse is used out of scope' and 'parsedResponse is defined but never used' in telize.js<commit_after>/**
 * @module telize
 * @description Client for Telize API.
 * @version 1.0.4
 * @author <NAME> [<EMAIL>]
 * @copyright Copyright (c) 2015 <NAME>
 * @license MIT
 */

"use strict";

/**
 * @private
 * @description Module dependencies.
 */

var http = require("http");
var freeze = require("deep-freeze-node");

/**
 * @private
 * @constant {String}
 * @description Module version.
 */

var VERSION = require('../package.json').version;

/**
 * @public
 * @constructor
 * @description Initialize instance of Telize with default request options.
 * @property {Object} defaultRequestOptions - Default request options.
 * @property {String} defaultRequestOptions.hostname - Default request hostname.
 * @property {Number} defaultRequestOptions.port - Default request port.
 * @property {String} defaultRequestOptions.basePath - Default request base path.
 */

function Telize() {
    this.defaultRequestOptions = {
        hostname: "www.telize.com",
        port: 80,
        basePath: ""
    };
}

/**
 * @public
 * @function getIP
 * @description Request current IP address.
 * @param {getIP~callback} callback - Callback when response comes in.
 */

Telize.prototype.getIP = function(callback) {
    if((typeof callback !== "function") || !(callback instanceof Function)) {
        throw new Error("getIP(): callback is undefined or contains non-function value");
    }

    request(this.defaultRequestOptions, "/jsonip", function(error, rawResponse) {
        var parsedResponse;
        if(!error) {
            try {
                parsedResponse = JSON.parse(rawResponse);
            } catch(error) {
                callback(error);
            }
            callback(null, parsedResponse.ip);
        } else {
            callback(error);
        }
    });
};

/**
 * @callback getIP~callback
 * @description Use as callback in getIP function.
 * @param {Object} error - Generic error object.
 * @param {String} ip - IP address.
 */

/**
 * @public
 * @function getGeoIP
 * @description Request Geo IP data.
 * @param {String} [ip] - IP address.
 * @param {getGeoIP~callback} callback - Callback when response comes in.
 */

Telize.prototype.getGeoIP = function(ip, callback) {
    if((typeof ip === "function") || (ip instanceof Function)) {
        callback = ip;
        ip = undefined;
    } else if((typeof callback !== "function") || !(callback instanceof Function)) {
        throw new Error("getGeoIP(): callback is undefined or contains non-function value");
    }

    request(this.defaultRequestOptions, "/geoip" + (ip ? "/" + ip : ""), function(error, rawResponse) {
        if(!error) {
            var parsedResponse;
            try {
                parsedResponse = JSON.parse(rawResponse);
            } catch(error) {
                callback(error);
            }
            if(parsedResponse.code && parsedResponse.message) {
                callback(new Error(parsedResponse.message, parsedResponse.code));
            } else {
                callback(null, parsedResponse);
            }
        } else {
            callback(error);
        }
    });
};

/**
 * @callback getGeoIP~callback
 * @description Use as callback in getGeoIP function.
 * @param {Object} error - Generic error object.
 * @param {Object} data - Geo IP data object.
 */

/**
 * @private
 * @function request
 * @description Perform HTTP GET request.
 * @param options - Request options.
 * @param path - Request path.
{'fraction_non_alphanumeric': 0.13155631986242478, 'fraction_numerical': 0.0025795356835769563, 'mean_word_length': 2.6443864229765013, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6148200', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1895, 'n_words': 550}