Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brainflake committed Jan 14, 2014
0 parents commit e4a386e
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/client');
98 changes: 98 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit e4a386e

Please sign in to comment.