# All files / lib/geocoder datasciencetoolkitgeocoder.js

96.55% Statements 28/29
91.67% Branches 11/12
100% Functions 4/4
96.55% Lines 28/29

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    752x
    2x
    2x

    2x
    10x
    10x

    10x

    2x

    2x
    5x
    5x

    5x
    1x

    5x
    5x

    5x

    2x

    5x
    5x
    3x

    3x
    3x
    1x

    2x

    2x

    2x
    2x

    2x
     var util             = require('util'),
        net              = require('net'),
        AbstractGeocoder = require('./abstractgeocoder');

    /**
     * Constructor
     */
    var DataScienceToolkitGeocoder = function DataScienceToolkitGeocoder(httpAdapter,options) {
        this.options     = ['host'];
        this.supportIPv4 = true;

        DataScienceToolkitGeocoder.super_.call(this, httpAdapter, options);
    };

    util.inherits(DataScienceToolkitGeocoder, AbstractGeocoder);

    /**
    * Build DSTK endpoint, allows for local DSTK installs
    * @param <string>   value    Value to geocode (Address or IPv4)
    */
    DataScienceToolkitGeocoder.prototype._endpoint = function(value) {
       var ep = { };
       var host = 'www.datasciencetoolkit.org';

       if(this.options.host) {
            host =  this.options.host;
        }

        ep.ipv4Endpoint = 'http://' + host + '/ip2coordinates/';
        ep.street2coordinatesEndpoint = 'http://' + host + '/street2coordinates/';

        return net.isIPv4(value) ? ep.ipv4Endpoint : ep.street2coordinatesEndpoint;
    };

    /**
    * Geocode
    * @param <string>   value    Value to geocode (Address or IPv4)
    * @param <function> callback Callback method
    */
    DataScienceToolkitGeocoder.prototype._geocode = function(value, callback) {

        var ep = this._endpoint(value);
        this.httpAdapter.get(ep + value , { }, function(err, result) {
            Iif (err) {
                return callback(err);
            } else {
                result = result[value];
                if (!result) {
                    return callback(new Error('Could not geocode "' + value + '".'));
                }

                var results = [];

                results.push({
                    'latitude' : result.latitude,
                    'longitude' : result.longitude,
                    'country' : result.country_name,
                    'city' : result.city || result.locality,
                    'state' : result.state || result.region,
                    'zipcode' : result.postal_code,
                    'streetName': result.street_name,
                    'streetNumber' : result.street_number,
                    'countryCode' : result.country_code
                });

                results.raw = result;
                callback(false, results);
            }

        });

    };

    module.exports = DataScienceToolkitGeocoder;