web3.js/lib/web3/method.js

159 lines
4.1 KiB
JavaScript
Raw Normal View History

2015-03-22 10:12:52 -07:00
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file method.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
2015-03-26 10:11:05 -07:00
var RequestManager = require('./requestmanager');
2015-03-22 10:12:52 -07:00
var utils = require('../utils/utils');
var errors = require('./errors');
var Method = function (options) {
this.name = options.name;
this.call = options.call;
this.params = options.params || 0;
this.inputFormatter = options.inputFormatter;
this.outputFormatter = options.outputFormatter;
};
2015-03-22 11:48:25 -07:00
/**
* Should be used to determine name of the jsonrpc method based on arguments
*
* @method getCall
* @param {Array} arguments
* @return {String} name of jsonrpc method
*/
2015-03-22 10:12:52 -07:00
Method.prototype.getCall = function (args) {
return utils.isFunction(this.call) ? this.call(args) : this.call;
};
2015-03-22 11:48:25 -07:00
/**
* Should be used to extract callback from array of arguments. Modifies input param
*
* @method extractCallback
* @param {Array} arguments
* @return {Function|Null} callback, if exists
*/
2015-03-22 10:12:52 -07:00
Method.prototype.extractCallback = function (args) {
if (utils.isFunction(args[args.length - 1])) {
return args.pop(); // modify the args array!
}
};
2015-03-22 11:48:25 -07:00
/**
* Should be called to check if the number of arguments is correct
*
* @method validateArgs
* @param {Array} arguments
* @throws {Error} if it is not
*/
2015-03-22 10:12:52 -07:00
Method.prototype.validateArgs = function (args) {
if (args.length !== this.params) {
throw errors.InvalidNumberOfParams();
2015-03-22 10:12:52 -07:00
}
};
2015-03-22 11:48:25 -07:00
/**
* Should be called to format input args of method
*
* @method formatInput
* @param {Array}
* @return {Array}
*/
2015-03-22 10:12:52 -07:00
Method.prototype.formatInput = function (args) {
2015-03-23 05:42:36 -07:00
if (!this.inputFormatter) {
return args;
}
return this.inputFormatter.map(function (formatter, index) {
return formatter ? formatter(args[index]) : args[index];
});
2015-03-22 10:12:52 -07:00
};
2015-03-22 11:48:25 -07:00
/**
* Should be called to format output(result) of method
*
* @method formatOutput
* @param {Object}
* @return {Object}
*/
2015-03-22 10:12:52 -07:00
Method.prototype.formatOutput = function (result) {
2015-03-23 05:42:36 -07:00
return this.outputFormatter && result !== null ? this.outputFormatter(result) : result;
2015-03-22 10:12:52 -07:00
};
2015-03-22 11:48:25 -07:00
/**
* Should attach function to method
*
* @method attachToObject
* @param {Object}
* @param {Function}
*/
2015-03-26 10:11:05 -07:00
Method.prototype.attachToObject = function (obj) {
var func = this.send.bind(this);
func.call = this.call; // that's ugly. filter.js uses it
2015-03-22 10:12:52 -07:00
var name = this.name.split('.');
if (name.length > 1) {
obj[name[0]] = obj[name[0]] || {};
obj[name[0]][name[1]] = func;
} else {
obj[name[0]] = func;
}
};
2015-03-26 10:11:05 -07:00
/**
* Should create payload from given input args
*
* @method toPayload
* @param {Array} args
* @return {Object}
*/
Method.prototype.toPayload = function (args) {
var call = this.getCall(args);
var callback = this.extractCallback(args);
var params = this.formatInput(args);
this.validateArgs(params);
return {
method: call,
params: params,
callback: callback
};
};
/**
* Should send request to the API
*
* @method send
* @param list of params
* @return result
*/
Method.prototype.send = function () {
var payload = this.toPayload(Array.prototype.slice.call(arguments));
if (payload.callback) {
var self = this;
return RequestManager.getInstance().sendAsync(payload, function (err, result) {
payload.callback(null, self.formatOutput(result));
});
}
return this.formatOutput(RequestManager.getInstance().send(payload));
};
2015-03-22 10:12:52 -07:00
module.exports = Method;