From e4a386e7c3579a568f481f885efaa5e7e459f3ba Mon Sep 17 00:00:00 2001 From: Brian Falk Date: Tue, 14 Jan 2014 13:13:36 -0500 Subject: [PATCH] initial commit --- .editorconfig | 11 ++++++ README.md | 20 +++++++++++ index.js | 1 + lib/client.js | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 14 ++++++++ 5 files changed, 144 insertions(+) create mode 100644 .editorconfig create mode 100644 README.md create mode 100644 index.js create mode 100644 lib/client.js create mode 100644 package.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3b281f2 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,11 @@ +# EditorConfig is awesome: http://EditorConfig.org + +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..92ec89d --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# node-hubspot + +Node.js wrapper for the HubSpot API + +## install + +npm install node-hubspot + +## use + + var Client = require('hubspot'); + + var client = new Client(); + + client.useKey('API_KEY'); + client.useToken('MY_ACCESS_TOKEN'); + client.campaigns.get(function(err, res) { + if (err) { throw err; } + console.log(res); + }); \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..19d5246 --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/client'); \ No newline at end of file diff --git a/lib/client.js b/lib/client.js new file mode 100644 index 0000000..76043b2 --- /dev/null +++ b/lib/client.js @@ -0,0 +1,98 @@ +var request = require('request'); + +var baseUrl = 'http://api.hubapi.com/'; + +function Client() { + var self = this; + + self.token; + self.key; + + function useToken (token) { + if (!token || typeof token !== 'string') { return cb(new Error("You must provide a token.")); } + self.token = token; + } + + function useKey (key) { + if (!key || typeof key !== 'string') { return cb(new Error("You must provide a key.")); } + self.key = key; + } + + var contacts = { + get: function(options, cb) { + if (typeof options === 'function') { + cb = options; + options = {}; + } + var call = { + method: 'GET', + url: baseUrl + 'contacts/v1/lists/all/contacts/all', + qs: options + }; + sendRequest(call, cb); + } + }; + + var lists = { + get: function(options, cb) { + if (typeof options === 'function') { + cb = options; + options = {}; + } + var call = { + method: 'GET', + url: baseUrl + 'contacts/v1/lists', + qs: options + }; + sendRequest(call, cb); + } + }; + + function sendRequest (call, cb) { + if (!self.key && !self.token) { return cb(new Error("You need to provide either a token or a key.")); } + + call.qs = call.qs || {}; + if (self.key) { + call.qs.hapikey = self.key; + } else if (self.token) { + call.qs.access_token = self.token; + } + + console.log(call); + request(call, handleResponse(cb)); + } + + function handleResponse (cb) { + return function (err, res, data) { + if (err) { + console.log('error'); + console.log(err); + return cb(err); + } + console.log('res'); + console.log(require('util').inspect(res)); + console.log('data'); + console.log(require('util').inspect(data)); + if (typeof data === 'string') { + try { + var parsed = JSON.parse(data); + data = parsed; + } catch (e) { + console.log('error parsing response'); + } + } + if (data.error) { return cb(new Error(data.error)); } + if (data && data.length && data.length > 0 && data[0].error_message) { return cb(new Error(data[0].error_message)); } + return cb(null, data); + } + } + + return { + contacts: contacts, + lists: lists, + useToken: useToken, + useKey: useKey + } +} + +module.exports = Client; diff --git a/package.json b/package.json new file mode 100644 index 0000000..3a8ac21 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "hubspot", + "version": "0.0.1", + "description": "A node wrapper for the HubSpot API", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "hubspot" + ], + "author": "Brian Falk", + "license": "BSD" +}