Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
olalonde committed Nov 16, 2012
0 parents commit 335168c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 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 changes: 56 additions & 0 deletions 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 changes: 21 additions & 0 deletions 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 change: 1 addition & 0 deletions test/.gitignore
@@ -0,0 +1 @@
key.js
6 changes: 6 additions & 0 deletions 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.