-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
64 lines (55 loc) · 1.64 KB
/
index.js
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
'use strict';
var url = require('url')
, request = require('request')
, debug = require('diagnostics')('geoip2-client');
/**
* GeoIp2APIClient constructor.
*
* @param {Object} options Configuration.
* @Constructor
* @api public
*/
var GeoIp2Client = module.exports = function GeoIp2Client(options) {
if (!(this instanceof GeoIp2Client)) return new GeoIp2Client(options);
this.options = options || {};
this.options.port = this.options.port || 8082;
this.options.timeout = this.options.timeout || 5E3;
this.options.hostname = this.options.hostname || 'localhost';
this.options.protocol = this.options.protocol || 'http';
this.options.host = this.options.host || (
this.options.hostname + ':' + this.options.port
);
debug('created new GeoIP2 client');
};
/**
* Query the API by IP.
*
* @param {String} ip IP to query for.
* @param {Function} done Completion callback.
* @api public
*/
GeoIp2Client.prototype.get = function get(ip, done) {
var uri = url.format({
protocol: this.options.protocol,
hostname: this.options.hostname,
host: this.options.host,
port: this.options.port,
pathname: ip
});
debug('querying data from GeoIP2 via %s', uri);
//
// Do a GET request to the generated url.
//
request.get({
timeout: this.options.timeout,
json: true,
uri: uri
}, function resolved(error, response, result) {
if (error || response.statusCode !== 200 || result.code === 'InternalError') {
error = error || new Error(result.message || 'Failed to query IP');
debug('failed to GET data for IP: %s', error.message);
return done(error);
}
done(null, result);
});
};