Skip to content

Commit

Permalink
added tests. updated comments and readme. updated parameter order
Browse files Browse the repository at this point in the history
  • Loading branch information
nanek committed Dec 2, 2011
1 parent 21539cf commit 80a4546
Show file tree
Hide file tree
Showing 29 changed files with 2,945 additions and 66 deletions.
44 changes: 27 additions & 17 deletions README.md
Expand Up @@ -3,31 +3,30 @@

## Methods

Requires application client_id and client_secret
Requires your Dwolla application client_id and client_secret.

* basicAccountInfo
* nearby(lat, long)
* basicAccountInfo(client_id, client_secret, id, callback)
* nearby(client_id, client_secret, lat, lon, [, params], callback)

Requires OAuth2 Token
Requires a valid user OAuth2 token. Note token do not expire and may be
reused. See https://github.com/bnoguchi/everyauth for an example on how
to get authorize and get a Dwolla OAuth2 token.

* fullAccountInfo
* balance
* contacts
* transactionById(id)
* transactions
* transactionsStats
* fullAccountInfo(oauth_token, callback)
* balance(oauth_token, callback)
* contacts(oauth_token[, params], callback)
* transactions(oauth_token[, params], callback)
* transactionById(oauth_token, id, callback)
* transactionsStats(oauth_token[, params], callback)

All optional parameters are passed in as an optional object before the callback.

## Installation

$ npm install dwolla

You need to register your application with Dwolla to get a client_id and
client_secret to make API requests. Set these as env variables.

DWOLLA_CLIENT_ID=''
DWOLLA_CLIENT_SECRET=''

## Example Usage
See examples.js.

var dwolla = require('dwolla');

Expand All @@ -39,12 +38,23 @@ client_secret to make API requests. Set these as env variables.
dwolla.fullAccountInfo(token, function(err, data) {
console.log("Full Account Info: " + data);
});
dwolla.transactions(token, null, function(err, data) {

dwolla.transactions(token, function(err, data) {
console.log("Transactions: " + data);
});

var params = {};
params.search = 'Ben';
params.types = 'All';
dwolla.contacts(token, params, function(err, data) {
console.log("Contacts: " + data);
});

## Tests
Tests use mocha and should

$ npm test

or

$ mocha
14 changes: 0 additions & 14 deletions example.js

This file was deleted.

30 changes: 30 additions & 0 deletions examples.js
@@ -0,0 +1,30 @@
var dwolla = require('./lib/dwolla');

// set your dwolla application ids
var client_id = process.env.DWOLLA_CLIENT_ID;
var client_secret = process.env.DWOLLA_CLIENT_SECRET;
if (client_id === undefined) return 'Missing DWOLLA_CLIENT_ID';
if (client_secret === undefined) return 'Missing DWOLLA_CLIENT_SECRET';

var account_id = '812-728-8151'; //use valid id
dwolla.basicAccountInfo(client_id, client_secret, account_id, function(err, data) {
if (err) { console.log(err); }
console.log(data);
});

var lat = '41.58454600';
var lon = '-93.63416700';
dwolla.nearby(client_id, client_secret, lat, lon, function(err, data) {
if (err) { console.log(err); }
console.log(data.length);
});

// set a valid user oauth token here
var oauth_token = process.env.DWOLLA_OAUTH_TOKEN;
if (oauth_token === undefined) return 'Missing DWOLLA_OAUTH_TOKEN';

dwolla.transactions(oauth_token, function(err, data) {
if (err) { console.log(err); }
console.log(data);
});

120 changes: 87 additions & 33 deletions lib/dwolla.js
@@ -1,20 +1,13 @@
var https = require('https');
var qs = require('querystring');

var keys = {
client_id: process.env.DWOLLA_CLIENT_ID || null
, client_secret: process.env.DWOLLA_CLIENT_SECRET || null
};

var defaultOptions = {
host: 'www.dwolla.com',
};

var API_PATH = '/oauth/rest';

function _request(path, params, fn) {
var options = defaultOptions;
options.path = API_PATH + path;
var options = {
host: 'www.dwolla.com'
, path: API_PATH + path
};
if (params) {
options.path += '?' + qs.stringify(params);
}
Expand All @@ -32,7 +25,7 @@ function _request(path, params, fn) {
fn(data.Message, null);
}
} catch (e) {
fn('Error parsing response from dwolla api.', null);
fn('Error parsing response from dwolla api.', data);
}
});
});
Expand All @@ -42,15 +35,26 @@ function _request(path, params, fn) {
/**
* Retrieves the basic account information for the Dwolla account associated with the account identifier.
* https://www.dwolla.com/developers/endpoints/users/basicinformation
*
* @param {String} client_id
* @param {String} client_secret
* @param {String} id
* @param {Function} fn
**/
exports.basicAccountInfo = function(id, fn) {
exports.basicAccountInfo = function(client_id, client_secret, id, fn) {
var path = '/users/' + id;
_request(path, keys, fn);
var params = {};
params.client_id = client_id;
params.client_secret = client_secret;
_request(path, params, fn);
};

/**
* Retrieves the account information for the user assoicated with the authorized access token.
* https://www.dwolla.com/developers/endpoints/users/accountinformation
*
* @param {String} oauth_token
* @param {Function} fn
**/
exports.fullAccountInfo = function(oauth_token, fn) {
var params = { oauth_token: oauth_token };
Expand All @@ -60,6 +64,9 @@ exports.fullAccountInfo = function(oauth_token, fn) {
/**
* Retrieves the account balance for the user for the authorized access token.
* https://www.dwolla.com/developers/endpoints/balance/account
*
* @param {String} oauth_token
* @param {Function} fn
* */
exports.balance = function(oauth_token, fn) {
var params = { oauth_token: oauth_token };
Expand All @@ -70,11 +77,22 @@ exports.balance = function(oauth_token, fn) {
* Retrieves contacts for the user for the authorized access token.
* https://www.dwolla.com/developers/endpoints/contacts/user
*
* @param {String} search
* @param {String} types
* @param {int} limit
* Optional params:
*
* - search
* - types
* - limit
*
* @param {String} oauth_token
* @param {Object} params
* @param {Function} fn
* */
exports.contacts = function(oauth_token, params, fn) {
// params are optional
if (!fn || typeof params === 'function') {
fn = params;
params = {};
}
params = params || {};
params.oauth_token = oauth_token;
_request('/contacts/', params, fn);
Expand All @@ -86,16 +104,28 @@ exports.contacts = function(oauth_token, params, fn) {
* are returned as random spots within the range.
* https://www.dwolla.com/developers/endpoints/contacts/nearby
*
* @param {String} lat
* @param {String} long
* @param {int} range
* @param {int} limit
* Optional params:
*
* - range
* - limit
*
* @param {String} client_id
* @param {String} client_secret
* @param {String} lat
* @param {String} lon
* @param {Object} params
* @param {Function} fn
**/
exports.nearby = function(lat, lon, params, fn) {
if (keys.client_id !== null) {
exports.nearby = function(client_id, client_secret, lat, lon, params, fn) {
// params are optional
if (!fn || typeof params === 'function') {
fn = params;
params = {};
}
if (client_id !== null) {
params = params || {};
params.client_id = keys.client_id;
params.client_secret = keys.client_secret;
params.client_id = client_id;
params.client_secret = client_secret;
params.latitude = lat;
params.longitude = lon;

Expand All @@ -110,7 +140,9 @@ exports.nearby = function(lat, lon, params, fn) {
* Transactions are returned in descending order by transaction date.
* https://www.dwolla.com/developers/endpoints/transactions/details
*
* @param {int} transactionId
* @param {String} oauth_token
* @param {int} transactionId
* @param {Function} fn
**/
exports.transactionById = function(oauth_token, id, fn) {
var params = { oauth_token: oauth_token };
Expand All @@ -122,12 +154,23 @@ exports.transactionById = function(oauth_token, id, fn) {
* Transactions are returned in descending order by transaction date.
* https://www.dwolla.com/developers/endpoints/transactions/list
*
* @param {String} sinceDate
* @param {String} types
* @param {int} limit
* @param {int} skip
* Optional params:
*
* - sinceDate
* - types
* - limit
* - skip
*
* @param {String} oauth_token
* @param {Object} params
* @param {Function} fn
**/
exports.transactions = function(oauth_token, params, fn) {
// params are optional
if (!fn || typeof params === 'function') {
fn = params;
params = {};
}
params = params || {};
params.oauth_token = oauth_token;
_request('/transactions/', params, fn);
Expand All @@ -137,11 +180,22 @@ exports.transactions = function(oauth_token, params, fn) {
* Retrieves transactions stats for the user for the authorized access token.
* https://www.dwolla.com/developers/endpoints/transactions/stats
*
* @param {String} types
* @param {String} startDate
* @param {String} endDate
* Optional params:
*
* - types
* - startDate
* - endDate
*
* @param {String} oauth_token
* @param {Object} params
* @param {Function} fn
**/
exports.transactionsStats = function(oauth_token, params, fn) {
// params are optional
if (!fn || typeof params === 'function') {
fn = params;
params = {};
}
params = params || {};
params.oauth_token = oauth_token;
_request('/transactions/stats', params, fn);
Expand Down

0 comments on commit 80a4546

Please sign in to comment.