move Copay.js -> API.js, and handle case of no command better

* API is a more appropriate name for what this is. It is intended to be the
 interface used by external apps.
* The case where you run "copay" with no command is now handled better.
This commit is contained in:
Ryan X. Charles 2014-04-15 10:50:54 -03:00
parent 4773b6577a
commit dd2e8c585e
2 changed files with 32 additions and 32 deletions

View File

@ -1,12 +1,12 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var PublicKeyRing = imports.PublicKeyRing || require('../js/models/core/PublicKeyRing'); var PublicKeyRing = imports.PublicKeyRing || require('./js/models/core/PublicKeyRing');
//var Wallet = imports.Wallet || require('../js/models/core/Wallet'); //var Wallet = imports.Wallet || require('../js/models/core/Wallet');
var Copay = function(opts) { var API = function(opts) {
this._init(opts); this._init(opts);
}; };
Copay.prototype._init = function(opts) { API.prototype._init = function(opts) {
var self = this; var self = this;
opts = opts ? opts : {}; opts = opts ? opts : {};
@ -26,14 +26,14 @@ Copay.prototype._init = function(opts) {
//self.wallet = self.opts.wallet ? self.opts.wallet : new Wallet(); //self.wallet = self.opts.wallet ? self.opts.wallet : new Wallet();
}; };
Copay.prototype._command = function(command, args, callback) { API.prototype._command = function(command, args, callback) {
var self = this; var self = this;
if (command[0] == "_") if (!command || command[0] == "_")
return callback(new Error('invalid command')); return callback(new Error('invalid command'));
if (typeof self[command] == 'function') { if (typeof self[command] == 'function') {
var f = Copay.prototype[command]; var f = API.prototype[command];
if (f.argTypes[f.argTypes.length-1][1] == 'function') if (f.argTypes[f.argTypes.length-1][1] == 'function')
return self[command].apply(self, args.concat([callback])); return self[command].apply(self, args.concat([callback]));
else else
@ -43,8 +43,8 @@ Copay.prototype._command = function(command, args, callback) {
return callback(new Error('invalid command')); return callback(new Error('invalid command'));
}; };
Copay._checkArgTypes = function(command, args) { API._checkArgTypes = function(command, args) {
var f = Copay.prototype[command]; var f = API.prototype[command];
for (var i in args) { for (var i in args) {
if (typeof args[i] != f.argTypes[i][1]) if (typeof args[i] != f.argTypes[i][1])
return false; return false;
@ -53,63 +53,63 @@ Copay._checkArgTypes = function(command, args) {
}; };
function checkArgs(name, args) { function checkArgs(name, args) {
if (!Copay._checkArgTypes(name, args)) if (!API._checkArgTypes(name, args))
throw new Error('Invalid arguments'); throw new Error('Invalid arguments');
} }
Copay.prototype.echo = function echo(str, callback) { API.prototype.echo = function echo(str, callback) {
var self = this; var self = this;
checkArgs(arguments.callee.name, arguments); checkArgs(arguments.callee.name, arguments);
return callback(null, str); return callback(null, str);
}; };
Copay.prototype.echo.argTypes = API.prototype.echo.argTypes =
[ [
['str', 'string'], ['str', 'string'],
['callback', 'function'] ['callback', 'function']
]; ];
/* /*
Copay.prototype.getBalance = function(callback) { API.prototype.getBalance = function(callback) {
var self = this; var self = this;
checkArgs('getBalance', arguments); checkArgs('getBalance', arguments);
return callback(null, self.wallet.getBalance([])); return callback(null, self.wallet.getBalance([]));
}; };
Copay.prototype.getBalance.argTypes = API.prototype.getBalance.argTypes =
[ [
['callback', 'function'] ['callback', 'function']
]; ];
*/ */
Copay.prototype.getArgTypes = function getArgTypes(command, callback) { API.prototype.getArgTypes = function getArgTypes(command, callback) {
var self = this; var self = this;
checkArgs(arguments.callee.name, arguments); checkArgs(arguments.callee.name, arguments);
if (command[0] == '_' || typeof Copay.prototype[command] != 'function') if (command[0] == '_' || typeof API.prototype[command] != 'function')
return callback(new Error('Invalid command')); return callback(new Error('Invalid command'));
var argTypes = Copay.prototype[command].argTypes; var argTypes = API.prototype[command].argTypes;
return callback(null, argTypes); return callback(null, argTypes);
}; };
Copay.prototype.getArgTypes.argTypes = API.prototype.getArgTypes.argTypes =
[ [
['command', 'string'], ['command', 'string'],
['callback', 'function'] ['callback', 'function']
]; ];
Copay.prototype.getCommands = function getCommands(callback) { API.prototype.getCommands = function getCommands(callback) {
var self = this; var self = this;
checkArgs(arguments.callee.name, arguments); checkArgs(arguments.callee.name, arguments);
var fs = []; var fs = [];
for (var i in Copay.prototype) { for (var i in API.prototype) {
var f = Copay.prototype[i]; var f = API.prototype[i];
if (typeof f == 'function' && i[0] != "_") if (typeof f == 'function' && i[0] != "_")
fs.push(i); fs.push(i);
}; };
@ -117,30 +117,30 @@ Copay.prototype.getCommands = function getCommands(callback) {
return callback(null, fs); return callback(null, fs);
}; };
Copay.prototype.getCommands.argTypes = API.prototype.getCommands.argTypes =
[ [
['callback', 'function'] ['callback', 'function']
]; ];
Copay.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) { API.prototype.getPublicKeyRingId = function getPublicKeyRingId(callback) {
var self = this; var self = this;
checkArgs(arguments.callee.name, arguments); checkArgs(arguments.callee.name, arguments);
return callback(null, self.publicKeyRing.id); return callback(null, self.publicKeyRing.id);
}; };
Copay.prototype.getPublicKeyRingId.argTypes = API.prototype.getPublicKeyRingId.argTypes =
[ [
['callback', 'function'] ['callback', 'function']
]; ];
Copay.prototype.help = function help(callback) { API.prototype.help = function help(callback) {
this.getCommands.apply(this, arguments); this.getCommands.apply(this, arguments);
}; };
Copay.prototype.help.argTypes = API.prototype.help.argTypes =
[ [
['callback', 'function'] ['callback', 'function']
]; ];
module.exports = require('soop')(Copay); module.exports = require('soop')(API);

View File

@ -1,26 +1,26 @@
#!/usr/bin/env node #!/usr/bin/env node
var Copay = require('./Copay.js'); var API = require('../API.js');
var commander = require('commander'); var commander = require('commander');
var main = function() { var main = function() {
commander commander
.version("0.0.1") .version("0.0.1")
.option('-f, --file [file]', 'AES encrypted data file', 'copay.json.aes') .option('-f, --file [file]', 'AES encrypted data file', 'api.json.aes')
.option('-p, --pass [passphrase]', 'AES wallet passphrase') .option('-p, --pass [passphrase]', 'AES wallet passphrase')
.option('-c, --client', 'Issue command over RPC to copay daemon') .option('-c, --client', 'Issue command over RPC to api daemon')
.option('-d, --daemon', 'Run as daemon accepting RPC commands') .option('-d, --daemon', 'Run as daemon accepting RPC commands')
.option('--rpcport [port]', 'RPC port [18332]', Number, 18332) .option('--rpcport [port]', 'RPC port [18332]', Number, 18332)
.option('--rpcuser [user]', 'RPC user [user]', String, 'user') .option('--rpcuser [user]', 'RPC user [user]', String, 'user')
.option('--rpcpass [password]', 'RPC password [pass]', String, 'pass') .option('--rpcpass [password]', 'RPC password [pass]', String, 'pass')
.parse(process.argv); .parse(process.argv);
var copay = new Copay(commander); var api = new API(commander);
var args = commander.args; var args = commander.args;
try { try {
copay._command(args[0], args.slice(1), function(err, result) { api._command(args[0], args.slice(1), function(err, result) {
if (err) if (err)
return console.log("" + err); return console.log("" + err);