Skip to content

Commit

Permalink
[TASK] change accountRequest method signature
Browse files Browse the repository at this point in the history
`(type, options, callback)` where the last argument will be considered for callback if it's a function
  • Loading branch information
geertweening committed Nov 8, 2014
1 parent 3c96602 commit 6f5d110
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/js/ripple/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Account.prototype.lines = function(callback) {
}
}

this._remote.requestAccountLines(this._account_id, accountLines);
this._remote.requestAccountLines({account: this._account_id}, accountLines);

return this;
};
Expand Down
6 changes: 3 additions & 3 deletions src/js/ripple/orderbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ OrderBook.prototype.requestTransferRate = function(callback) {
return this.emit('transfer_rate', this._issuerTransferRate);
}

this._remote.requestAccountInfo(issuer, function(err, info) {
this._remote.requestAccountInfo({account: issuer}, function(err, info) {
if (err) {
// XXX What now?
return callback(err);
Expand Down Expand Up @@ -447,7 +447,7 @@ OrderBook.prototype.requestFundedAmount = function(account, callback) {
}

function requestNativeBalance(callback) {
self._remote.requestAccountInfo(account, function(err, info) {
self._remote.requestAccountInfo({account: account}, function(err, info) {
if (err) {
callback(err);
} else {
Expand All @@ -458,8 +458,8 @@ OrderBook.prototype.requestFundedAmount = function(account, callback) {

function requestLineBalance(callback) {
var request = self._remote.requestAccountLines(
account,
{
account: account,
ledger: 'validated',
peer: self._issuerGets
}
Expand Down
30 changes: 17 additions & 13 deletions src/js/ripple/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -1218,10 +1218,11 @@ Remote.prototype.requestTx = function(hash, callback) {
* @param [Function] callback
* @return {Request}
*/
Remote.accountRequest = function(type, account, options, callback) {
var ledger, peer, limit, marker;
Remote.accountRequest = function(type, options, callback) {
var account, ledger, peer, limit, marker;

if (typeof options === 'object') {
account = options.account;
ledger = options.ledger;
peer = options.peer;
limit = options.limit;
Expand All @@ -1231,7 +1232,7 @@ Remote.accountRequest = function(type, account, options, callback) {
// if a marker is given, we need a ledger
// check if a valid ledger_index or ledger_hash is provided
if (marker) {
if(!(Number(ledger) > 0) && !UInt256.is_valid(ledger)) {
if (!(Number(ledger) > 0) && !UInt256.is_valid(ledger)) {
throw new Error('A ledger_index or ledger_hash must be provided when using a marker');
}
}
Expand All @@ -1243,9 +1244,12 @@ Remote.accountRequest = function(type, account, options, callback) {
}

var request = new Request(this, type);
var account = UInt160.json_rewrite(account);

request.message.account = account;
if (account) {
account = UInt160.json_rewrite(account);
request.message.account = account;
}

request.ledgerSelect(ledger);

if (UInt160.is_valid(peer)) {
Expand Down Expand Up @@ -1280,31 +1284,31 @@ Remote.accountRequest = function(type, account, options, callback) {
/**
* Request account_info
*
* @param {String} account - ripple address
* @param {Object} options
* @param {String} account - ripple address
* @param {String} peer - ripple address
* @param [String|Number] ledger identifier
* @param [Function] callback
* @return {Request}
*/

Remote.prototype.requestAccountInfo = function(account, options, callback) {
Remote.prototype.requestAccountInfo = function(options, callback) {
var args = Array.prototype.concat.apply(['account_info'], arguments);
return Remote.accountRequest.apply(this, args);
};

/**
* Request account_currencies
*
* @param {String} account - ripple address
* @param {Object} options
* @param {String} account - ripple address
* @param {String} peer - ripple address
* @param [String|Number] ledger identifier
* @param [Function] callback
* @return {Request}
*/

Remote.prototype.requestAccountCurrencies = function(account, options, callback) {
Remote.prototype.requestAccountCurrencies = function(options, callback) {
var args = Array.prototype.concat.apply(['account_currencies'], arguments);
return Remote.accountRequest.apply(this, args);
};
Expand All @@ -1319,8 +1323,8 @@ Remote.prototype.requestAccountCurrencies = function(account, options, callback)
* ledger closes. You have to supply a ledger_index or ledger_hash
* when paging to ensure a complete response
*
* @param {String} account - ripple address
* @param {Object} options
* @param {String} account - ripple address
* @param {String} peer - ripple address
* @param [String|Number] ledger identifier
* @param [Number] limit - max results per response
Expand All @@ -1329,7 +1333,7 @@ Remote.prototype.requestAccountCurrencies = function(account, options, callback)
* @return {Request}
*/

Remote.prototype.requestAccountLines = function(account, options, callback) {
Remote.prototype.requestAccountLines = function(options, callback) {
// XXX Does this require the server to be trusted?
//utils.assert(this.trusted);
var args = Array.prototype.concat.apply(['account_lines'], arguments);
Expand All @@ -1346,16 +1350,16 @@ Remote.prototype.requestAccountLines = function(account, options, callback) {
* ledger closes. You have to supply a ledger_index or ledger_hash
* when paging to ensure a complete response
*
* @param {String} account - ripple address
* @param {Object} options
* @param {String} account - ripple address
* @param [String|Number] ledger identifier
* @param [Number] limit - max results per response
* @param {String} marker - start position in response paging
* @param [Function] callback
* @return {Request}
*/

Remote.prototype.requestAccountOffers = function(account, options, callback) {
Remote.prototype.requestAccountOffers = function(options, callback) {
var args = Array.prototype.concat.apply(['account_offers'], arguments);
return Remote.accountRequest.apply(this, args);
};
Expand Down
48 changes: 24 additions & 24 deletions test/remote-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,25 @@ describe('Remote', function () {
});

it('request account currencies with ledger index', function() {
var request = remote.requestAccountCurrencies(ADDRESS);
var request = remote.requestAccountCurrencies({account: ADDRESS});
assert.strictEqual(request.message.command, 'account_currencies');
assert.strictEqual(request.message.account, ADDRESS);
});

it('request account info with ledger index', function() {
var request = remote.requestAccountInfo(ADDRESS, {ledger: 9592219});
var request = remote.requestAccountInfo({account: ADDRESS, ledger: 9592219});
assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, ADDRESS);
assert.strictEqual(request.message.ledger_index, 9592219);
});
it('request account info with ledger hash', function() {
var request = remote.requestAccountInfo(ADDRESS, {ledger: LEDGER_HASH});
var request = remote.requestAccountInfo({account: ADDRESS, ledger: LEDGER_HASH});
assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, ADDRESS);
assert.strictEqual(request.message.ledger_hash, LEDGER_HASH);
});
it('request account info with ledger identifier', function() {
var request = remote.requestAccountInfo(ADDRESS, {ledger: 'validated'});
var request = remote.requestAccountInfo({account: ADDRESS, ledger: 'validated'});
assert.strictEqual(request.message.command, 'account_info');
assert.strictEqual(request.message.account, ADDRESS);
assert.strictEqual(request.message.ledger_index, 'validated');
Expand All @@ -238,7 +238,7 @@ describe('Remote', function () {
});

it('pagingAccountRequest', function() {
var request = Remote.accountRequest('account_lines', ADDRESS);
var request = Remote.accountRequest('account_lines', {account: ADDRESS});
assert.deepEqual(request.message, {
command: 'account_lines',
id: undefined,
Expand All @@ -247,7 +247,7 @@ describe('Remote', function () {
});

it('pagingAccountRequest - limit', function() {
var request = Remote.accountRequest('account_lines', ADDRESS, {limit: 100});
var request = Remote.accountRequest('account_lines', {account: ADDRESS, limit: 100});
assert.deepEqual(request.message, {
command: 'account_lines',
id: undefined,
Expand All @@ -257,7 +257,7 @@ describe('Remote', function () {
});

it('pagingAccountRequest - limit, marker', function() {
var request = Remote.accountRequest('account_lines', ADDRESS, {limit: 100, marker: PAGING_MARKER, ledger: 9592219});
var request = Remote.accountRequest('account_lines', {account: ADDRESS, limit: 100, marker: PAGING_MARKER, ledger: 9592219});
assert.deepEqual(request.message, {
command: 'account_lines',
id: undefined,
Expand All @@ -271,38 +271,38 @@ describe('Remote', function () {
});

it('accountRequest - limit min', function() {
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 0}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: -1}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: -1e9}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: -1e24}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 0}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: -1}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: -1e9}).message.limit, 0);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: -1e24}).message.limit, 0);
});

it('accountRequest - limit max', function() {
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e9}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e9+1}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e10}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', ADDRESS, {limit: 1e24}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e9}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e9+1}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e10}).message.limit, 1e9);
assert.strictEqual(Remote.accountRequest('account_lines', {account: ADDRESS, limit: 1e24}).message.limit, 1e9);
});

it('accountRequest - a valid ledger is required when using a marker', function() {
assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER})
Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER})
},'A ledger_index or ledger_hash must be provided when using a marker');

assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:'validated'})
Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:'validated'})
},'A ledger_index or ledger_hash must be provided when using a marker');

assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:NaN})
Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:NaN})
},'A ledger_index or ledger_hash must be provided when using a marker');

assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:LEDGER_HASH.substr(0,63)})
Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:LEDGER_HASH.substr(0,63)})
},'A ledger_index or ledger_hash must be provided when using a marker');

assert.throws(function() {
Remote.accountRequest('account_lines', ADDRESS, {marker: PAGING_MARKER, ledger:LEDGER_HASH+'F'})
Remote.accountRequest('account_lines', {account: ADDRESS, marker: PAGING_MARKER, ledger:LEDGER_HASH+'F'})
},'A ledger_index or ledger_hash must be provided when using a marker');
});

Expand All @@ -312,7 +312,7 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
});
var request = remote.requestAccountLines(
ADDRESS,
{account: ADDRESS},
callback
);

Expand All @@ -331,8 +331,8 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
});
var request = remote.requestAccountLines(
ADDRESS,
{
account: ADDRESS,
ledger: LEDGER_HASH,
peer: PEER_ADDRESS
},
Expand All @@ -356,8 +356,8 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
});
var request = remote.requestAccountLines(
ADDRESS,
{
account: ADDRESS,
ledger: LEDGER_INDEX,
peer: PEER_ADDRESS,
limit: 200,
Expand Down Expand Up @@ -385,8 +385,8 @@ describe('Remote', function () {
servers: [ { host: 's-west.ripple.com', port: 443, secure: true } ]
});
var request = remote.requestAccountOffers(
ADDRESS,
{
account: ADDRESS,
ledger: LEDGER_HASH,
peer: PEER_ADDRESS,
limit: 32,
Expand Down

0 comments on commit 6f5d110

Please sign in to comment.