commit 766716766ef9b7211dc6b0648f650a30458a0be2 Author: Andrew Van Tassel Date: Sun Jun 14 12:04:38 2015 -0600 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..2d901c9 --- /dev/null +++ b/README.md @@ -0,0 +1,61 @@ +# MailHops API node +[www.MailHops.com](http://www.mailhops.com) + +MailHops logo + +A nodejs module for interacting with the MailHops API. + +##Getting Started + +###Installation + +``` +$ npm install mailhops +``` + +###Configuration +Simply require the mailhops module, instantiate a new MailHops object, configure it if necessary, and start making calls. + +New MailHops objects can be instantiated with configuration parameters. Here is an example: + +```javascript +var MailHops = require("mailhops"); +var mailhops = new MailHops({ + api_key: "aWN8Pb27Xj6GfV8D6ARsjokonYwbWUNbz9rM", + api_version: 1, + proxy: "http://myproxy:3128", + app_name: "Node App v1.0.0", + forecastio_api_key: "", + show_client: 1 +}); +``` + +MailHops objects can also be configured via the ```.configure(options)``` method. Here is an exmaple: + +```javascript +var MailHops = require("mailhops"); +var mailhops = new MailHops(); + +var options = { + api_key: "aWN8Pb27Xj6GfV8D6ARsjokonYwbWUNbz9rM" +} + +mailhops.configure(options); + +mailhops.lookup('216.58.217.46,98.138.253.109',function(err,response){ + console.log(response); +}); + +var mapUrl = mailhops.mapUrl('216.58.217.46,98.138.253.109'); + +``` + +###Running Tests +``` +$ npm test +``` + +## Other MailHops projects +- [API](https://github.com/avantassel/mailhops-api) +- [Postbox & Thunderbird plugin](https://github.com/avantassel/mailhops-plugin) +- [Download](https://addons.mozilla.org/en-US/thunderbird/addon/mailhops/) diff --git a/config.json b/config.json new file mode 100644 index 0000000..c43ea13 --- /dev/null +++ b/config.json @@ -0,0 +1,8 @@ +{ + "base_uri": "http://api.mailhops.com" + ,"app_name": "Node App" + ,"api_version": 1 + ,"api_key": "" + ,"forecastio_api_key": "" + ,"show_client": 1 +} \ No newline at end of file diff --git a/lib/api.js b/lib/api.js new file mode 100644 index 0000000..5cf363a --- /dev/null +++ b/lib/api.js @@ -0,0 +1,43 @@ +var _ = require("lodash"); +var async = require("async"); +var querystring = require("qs"); +var request = require([__dirname, "request"].join("/")); + +module.exports = { + + lookup: function(route, options, fn){ + if(_.isFunction(options) && _.isUndefined(fn)){ + fn = options; + options = {}; + } + + var qs = options; + qs.api_key = this.api_key || ''; + qs.c = this.show_client; + qs.r = route.replace(" ", "+"); + + if(this.forecastio_api_key) + qs.fkey = this.forecastio_api_key; + + var config = { + uri: [this.api_version, "lookup"].join("/"), + qs: qs, + proxy: this.proxy + } + + request.create(config, fn); + }, + + //just returns a map url that can be used as an iframe src + mapUrl: function(route, options){ + var qs = options || {}; + qs.api_key = this.api_key || ''; + qs.c = this.show_client; + qs.r = route.replace(" ", "+"); + + if(this.forecastio_api_key) + qs.fkey = this.forecastio_api_key; + + return [this.base_uri, this.api_version, "map", '?'+querystring.stringify(qs)].join("/"); + } +} \ No newline at end of file diff --git a/lib/request.js b/lib/request.js new file mode 100644 index 0000000..132a11a --- /dev/null +++ b/lib/request.js @@ -0,0 +1,16 @@ +var _ = require("lodash"); +var request = require("request"); +var configuration = require([__dirname, "..", "config"].join("/")); + +exports.create = function(config, fn){ + var options = { + uri: [configuration.base_uri, config.uri].join("/"), + method: "GET", + qs: config.qs || {}, + proxy: config.proxy + } + + request(options, function(err, response, body){ + fn(err, JSON.parse(body)); + }); +} diff --git a/mailhops.js b/mailhops.js new file mode 100644 index 0000000..f4925b8 --- /dev/null +++ b/mailhops.js @@ -0,0 +1,23 @@ +var _ = require("lodash"); +var api = require([__dirname, "lib", "api"].join("/")); + +function MailHops(config){ + this.configure(config || {}); +} + +MailHops.prototype.configure = function(config){ + this.base_uri = config.base_uri; + this.api_key = config.api_key || undefined; + this.proxy = config.proxy || undefined; + this.api_version = config.api_version || 1; + this.api_version = ["v", this.api_version].join(""); + this.app_name = config.app_name; + this.forecastio_api_key = config.forecastio_api_key || undefined; + this.show_client = config.show_client || 1; +} + +_.each(api, function(method, name){ + MailHops.prototype[name] = method; +}); + +module.exports = MailHops; diff --git a/main.js b/main.js new file mode 100644 index 0000000..32a6bd2 --- /dev/null +++ b/main.js @@ -0,0 +1,9 @@ +var _ = require("lodash"); +var MailHops = require([__dirname, "mailhops"].join("/")); +var pkg = require([__dirname, "package"].join("/")); + +exports = module.exports = function(config){ + var mailhops = new MailHops(config); + mailhops.version = pkg.version; + return mailhops; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c3676e1 --- /dev/null +++ b/package.json @@ -0,0 +1,48 @@ +{ + "name": "mailhops", + "version": "0.0.1", + "description": "A nodejs module for interacting with the MailHops API.", + "main": "main.js", + "dependencies": { + "assert": "^1.1.1", + "async": "^0.6.2", + "lodash": "^2.4.1", + "mocha": "^2.2.4", + "qs": "^3.1.0", + "request": "https://registry.npmjs.org/request/-/request-2.34.0.tgz" + }, + "scripts": { + "test": "mocha" + }, + "repository": { + "type": "git", + "url": "git://github.com/avantassel/mailhops-node.git" + }, + "keywords": [ + "mailhops", + "api", + "email", + "route", + "geoip", + "weather" + ], + "author": { + "name": "Andrew Van Tassel", + "email": "andrew@mailhops.com" + }, + "license": "GPLv2", + "bugs": { + "url": "https://github.com/avantassel/mailhops-node/issues" + }, + "homepage": "https://github.com/avantassel/mailhops-node", + "_npmUser": { + "name": "avantassel", + "email": "andrew@andrewvantassel.com" + }, + "maintainers": [ + { + "name": "avantassel", + "email": "andrew@mailhops.com" + } + ] +} diff --git a/test/mailhops.js b/test/mailhops.js new file mode 100644 index 0000000..b8e2672 --- /dev/null +++ b/test/mailhops.js @@ -0,0 +1,44 @@ +var _ = require("lodash"); +var assert = require("assert"); +var MailHops = require([__dirname, "..", "main"].join("/")); +var configuration = require([__dirname, "..", "config"].join("/")); +var mailhops; + +describe("mailhops", function(){ + before(function(fn){ + mailhops = new MailHops(configuration); + fn(); + }); + + describe("MailHops()", function(){ + it("required api methods exist", function(){ + var required_keys = [ + "configure", + "lookup", + "map", + ] + + assert.deepEqual(_.keys(mailhops.__proto__), required_keys); + }); + + it("default config parameters are set correctly", function(){ + assert.equal(mailhops.api_version, "v1"); + assert.equal(mailhops.api_key, undefined); + }); + + }); + + describe("configure()", function(){ + it("sets config parameters correctly", function(){ + mailhops.configure({ + api_version: 1, + api_key: "aWN8Pb27Xj6GfV8D6ARsjokonYwbWUNbz9rM", + app_name: "Node App" + }); + assert.equal(mailhops.api_version, "v1"); + assert.equal(mailhops.api_key, "aWN8Pb27Xj6GfV8D6ARsjokonYwbWUNbz9rM"); + assert.equal(mailhops.app_name, "Node App"); + }); + + }); +}); diff --git a/test/main.js b/test/main.js new file mode 100644 index 0000000..bbb8f72 --- /dev/null +++ b/test/main.js @@ -0,0 +1,44 @@ +var _ = require("lodash"); +var assert = require("assert"); +var configuration = require([__dirname, "..", "config"].join("/")); +var pkg = require([__dirname, "..", "package"].join("/")); +var MailHops = require([__dirname, "..", "main"].join("/")); +var mailhops = new MailHops(configuration); + +describe("main", function(){ + + describe("new MailHops()", function(){ + it("api_version parameter exists", function(){ + assert.ok(_.has(mailhops, "api_version")); + }); + + it("api_key parameter exists", function(){ + assert.ok(_.has(mailhops, "api_key")); + }); + + it("app_name parameter exists", function(){ + assert.ok(_.has(mailhops, "app_name")); + }); + + it("version parameter exists", function(){ + assert.ok(_.has(mailhops, "version")); + }); + + it("version parameter equals that defined in package.json", function(){ + assert.equal(mailhops.version, pkg.version); + }); + + }); + + describe("GET lookup", function(){ + + it('should return a 200 response with private ip', function(done){ + mailhops.lookup('127.0.0.1', function(err, response){ + assert.equal(response.meta['code'],200); + assert.equal(response.response.route[0]['private'],true); + done(); + }); + }); + }); + +});