# All files / lib/geocoder freegeoipgeocoder.js

94.44% Statements 17/18
50% Branches 1/2
100% Functions 3/3
94.44% Lines 17/18

    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
    562x
    2x

    2x
    6x
    6x
    6x
    6x

    2x

    2x

    2x
    2x
    1x

    1x

    1x

    1x
    1x

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

    /**
     * Constructor
     */
    var FreegeoipGeocoder = function FreegeoipGeocoder(httpAdapter) {
        this.supportIPv4   = true;
        this.supportIPv6   = true;
        this.supportAddress = false;
        FreegeoipGeocoder.super_.call(this, httpAdapter);
    };

    util.inherits(FreegeoipGeocoder, AbstractGeocoder);

    // WS endpoint
    FreegeoipGeocoder.prototype._endpoint = 'http://freegeoip.net/json/';

    /**
    * Geocode
    * @param <string>   value    Value to geocode (IP only)
    * @param <function> callback Callback method
    */
    FreegeoipGeocoder.prototype._geocode = function(value, callback) {
        this.httpAdapter.get(this._endpoint + value , { }, function(err, result) {
            Iif (err) {
                return callback(err);
            } else {

                var results = [];

                results.push({
                    'ip' : result.ip,
                    'countryCode' : result.country_code,
                    'country' : result.country_name,
                    'regionCode' : result.region_code,
                    'regionName' : result.region_name,
                    'city' : result.city,
                    'zipcode' : result.zip_code,
                    'timeZone' : result.time_zone,
                    'latitude' : result.latitude,
                    'longitude' : result.longitude,
                    'metroCode' : result.metro_code

                });

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

        });

    };

    module.exports = FreegeoipGeocoder;