mirror of
https://github.com/MailHops/mailhops-node.git
synced 2025-05-15 11:20:12 -07:00
First commit
This commit is contained in:
commit
766716766e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules
|
61
README.md
Normal file
61
README.md
Normal file
@ -0,0 +1,61 @@
|
||||
# MailHops API node
|
||||
[www.MailHops.com](http://www.mailhops.com)
|
||||
|
||||
<img src="http://www.mailhops.com/images/logos/mailhops395.png" width="200" alt="MailHops logo" title="MailHops" align="right" />
|
||||
|
||||
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/)
|
8
config.json
Normal file
8
config.json
Normal file
@ -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
|
||||
}
|
43
lib/api.js
Normal file
43
lib/api.js
Normal file
@ -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("/");
|
||||
}
|
||||
}
|
16
lib/request.js
Normal file
16
lib/request.js
Normal file
@ -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));
|
||||
});
|
||||
}
|
23
mailhops.js
Normal file
23
mailhops.js
Normal file
@ -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;
|
9
main.js
Normal file
9
main.js
Normal file
@ -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;
|
||||
}
|
48
package.json
Normal file
48
package.json
Normal file
@ -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"
|
||||
}
|
||||
]
|
||||
}
|
44
test/mailhops.js
Normal file
44
test/mailhops.js
Normal file
@ -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");
|
||||
});
|
||||
|
||||
});
|
||||
});
|
44
test/main.js
Normal file
44
test/main.js
Normal file
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user