diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..e69de29 diff --git a/examples/query.js b/examples/query.js new file mode 100644 index 0000000..73acdb7 --- /dev/null +++ b/examples/query.js @@ -0,0 +1,11 @@ +var RapleafApi = require('../index'); + + +var key = process.argv[2]; +var email = process.argv[3]; + +var rap = new RapleafApi(key); + +rap.query_by_email(email, function(data) { + console.log(data); +}); diff --git a/index.js b/index.js new file mode 100644 index 0000000..bda80c9 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/node-rapleaf'); diff --git a/lib/node-rapleaf.js b/lib/node-rapleaf.js new file mode 100644 index 0000000..0f9c14a --- /dev/null +++ b/lib/node-rapleaf.js @@ -0,0 +1,71 @@ +/* + * Author: Daniel Tralamazza + * + * RapLeaf API for node.js + * + * TODO bulk call + */ + +var https = require('https'), + qs = require('querystring'), + crypto = require('crypto'); + + +var RapleafApi = function(api_key) { + this.api_key = api_key; + this.base_path = '/v4/dr?'; + this.options = { + host: 'personalize.rapleaf.com', + headers: { + 'User-Agent': 'RapleafAPI/node/1.0' + } + }; +}; + +RapleafApi.prototype._send_query = function(params, callback) { + params.api_key = this.api_key; + this.options.path = this.base_path + qs.stringify(params); + https.get(this.options, function(res) { + res.setEncoding('utf-8'); + res.on('data', function(d) { + if (res.statusCode != 200) + throw new Error(d); + callback(d); + }); + }).on('error', function(err) { + throw new Error(err); + }); +}; + +RapleafApi.prototype.query_by_email = function(email, callback, hash_it) { + if (hash_it) { + var sha1sum = crypto.createHash('sha1'); + sha1sum.update(email); + return this.query_by_sha1(sha1sum.digest('hex'), callback); + } else + return this._send_query({ 'email': email }, callback); +}; + +RapleafApi.prototype.query_by_md5 = function(md5email, callback) { + return this._send_query({ 'md5_email': md5email }, callback); +}; + +RapleafApi.prototype.query_by_sha1 = function(sha1email, callback) { + return this._send_query({ 'sha1_email': sha1email }, callback); +}; + +RapleafApi.prototype.query_by_nap = function(first, last, street, city, state, callback, email) { + var params = { first: first, last: last, street: street, state: state }; + if (email !== undefined) + params.email = email; + return this._send_query(params, callback); +}; + +RapleafApi.prototype.query_by_nap = function(first, last, zip4, callback, email) { + var params = { first: first, last: last, zip4: zip4 }; + if (email !== undefined) + params.email = email; + return this._send_query(params, callback); +}; + +module.exports = RapleafApi; diff --git a/package.json b/package.json new file mode 100644 index 0000000..37e9b83 --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name" : "node-rapleaf", + "description" : "Rapleaf API client for node.js", + "keywords" : [ "rapleaf", "api" ], + "version" : "0.0.1", + "author" : "Daniel Tralamazza ", + "repository" : { "type" : "git", "url" : "git://github.com/tralamazza/node-rapleaf.git" }, + "main" : "index", + "engines" : { "node" : "0.4.x" } +}