Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 335168cbb5c51caaf6b66c9d712257d4a3609000 @olalonde committed Nov 16, 2012
Showing with 104 additions and 0 deletions.
  1. +20 −0 README.md
  2. +56 −0 index.js
  3. +21 −0 package.json
  4. +1 −0 test/.gitignore
  5. +6 −0 test/index.js
20 README.md
@@ -0,0 +1,20 @@
+Wrapper for [ipinfodb.com](http://www.ipinfodb.com/ip_location_api.php)'s IP geolocation API.
+
+# Install
+
+ npm install ipinfodb
+
+# Usage
+
+```javascript
+var key = '....';
+client = require('ipinfodb')({ key: key });
+
+client.geolocate('106.186.17.133', function (err, res) {
+ console.log(res);
+});
+
+client.ip2timezoneOffset('106.186.17.133', function (err, timezoneOffset) {
+ console.log(timezoneOffset);
+});
+```
56 index.js
@@ -0,0 +1,56 @@
+/**
+ * opts:
+ * - key : API key
+ * - endpoint : (optional) API base URL
+ */
+var http = require('http'),
+ qs = require('querystring'),
+ path = require('path');
+
+function Client(opts) {
+ this.key = opts.key;
+ this.endpoint = (opts.endpoint) ? opts.endpoint : 'http://api.ipinfodb.com/v3/ip-city/';
+}
+
+Client.prototype.geolocate = function (ip, cb) {
+ var query = {
+ key: this.key,
+ ip: ip,
+ format: 'json'
+ }
+ var url = this.endpoint + '?' + qs.stringify(query);
+ http.get(url, function (res) {
+ res.on('data', function (data) {
+ try {
+ var data = JSON.parse(data);
+ cb(null, data);
+ }
+ catch (err) {
+ cb(err);
+ }
+ });
+ }).on('error', function (err) {
+ cb(err);
+ });;
+}
+function timezone2timezoneOffset(timezone) {
+ timezone = timezone.split(':');
+ var hours = parseInt(timezone[0], 10);
+ var minutes = parseInt(timezone[1], 10);
+ if (hours < 0) minutes = -minutes;
+ // convert to minutes
+ return hours * 60 + minutes;
+};
+
+Client.prototype.ip2timezoneOffset = function (ip, cb) {
+ this.geolocate(ip, function (err, res) {
+ if (err) return cb(err);
+
+ console.log(res.timeZone);
+ cb(null, timezone2timezoneOffset(res.timeZone));
+ });
+}
+
+module.exports = function (opts) {
+ return new Client(opts);
+}
21 package.json
@@ -0,0 +1,21 @@
+{
+ "name": "ipinfodb",
+ "version": "0.0.1",
+ "description": "Wrapper for ipinfodb.com API (ip to timezone)",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/olalonde/node-ipinfodb.git"
+ },
+ "keywords": [
+ "ip",
+ "timezone",
+ "geolocation",
+ "ipinfodb"
+ ],
+ "author": "Olivier Lalonde <olalonde@gmail.com>",
+ "license": "BSD"
+}
1 test/.gitignore
@@ -0,0 +1 @@
+key.js
6 test/index.js
@@ -0,0 +1,6 @@
+var key = require('./key'),
+ client = require('../')({ key: key });
+
+client.ip2timezoneOffset('106.186.17.133', function (err, timezoneOffset) {
+ console.log(timezoneOffset);
+});

0 comments on commit 335168c

Please sign in to comment.