cylon/lib/adaptor.js

78 lines
1.9 KiB
JavaScript
Raw Normal View History

"use strict";
2013-10-25 05:25:42 +08:00
2014-12-16 03:15:29 +08:00
var Basestar = require("./basestar"),
Utils = require("./utils"),
_ = require("./utils/helpers");
2014-04-25 06:31:47 +08:00
function formatErrorMessage(name, message) {
return ["Error in connection", "'" + name + "'", "- " + message].join(" ");
}
2014-05-07 09:18:20 +08:00
// Public: Creates a new Adaptor
//
// opts - hash of acceptable params
// name - name of the Adaptor, used when printing to console
// connection - Connection the adaptor will use to proxy commands/events
//
// Returns a new Adaptor
2014-07-11 00:51:23 +08:00
var Adaptor = module.exports = function Adaptor(opts) {
Adaptor.__super__.constructor.apply(this, arguments);
2014-07-11 00:51:23 +08:00
opts = opts || {};
2014-05-07 09:18:20 +08:00
this.name = opts.name;
// the Robot the adaptor belongs to
this.robot = opts.robot;
// some default options
this.host = opts.host;
this.port = opts.port;
// misc. details provided in args hash
this.details = {};
_.each(opts, function(opt, name) {
2015-03-12 03:31:45 +08:00
if (!_.includes(["robot", "name", "adaptor", "events"], name)) {
2015-02-20 09:23:41 +08:00
this.details[name] = opt;
}
}, this);
2014-05-07 09:18:20 +08:00
};
Utils.subclass(Adaptor, Basestar);
// Public: Basic #connect function. Must be overwritten by a descendent class
//
// Returns nothing, throws an error
Adaptor.prototype.connect = function() {
var message = formatErrorMessage(
this.name,
"Adaptor#connect method must be overwritten by descendant classes."
);
throw new Error(message);
};
// Public: Basic #disconnect function. Must be overwritten by a descendent class
//
// Returns nothing, throws an error
Adaptor.prototype.disconnect = function() {
var message = formatErrorMessage(
this.name,
"Adaptor#disconnect method must be overwritten by descendant classes."
);
throw new Error(message);
};
// Public: Expresses the Connection in JSON format
//
// Returns an Object containing Connection data
Adaptor.prototype.toJSON = function() {
return {
name: this.name,
adaptor: this.constructor.name || this.name,
details: this.details
};
};