Comparing version 0.1.1 to 0.1.2
@@ -8,3 +8,3 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -21,2 +21,6 @@ var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _bluebird = require('bluebird'); | ||
var _bluebird2 = _interopRequireDefault(_bluebird); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -28,3 +32,3 @@ | ||
var listeners = {}; | ||
global._ghee_listeners = {}; | ||
@@ -38,3 +42,4 @@ var Ghee = exports.Ghee = function () { | ||
this.slack = RtmClient || new _client2.default.RtmClient(token, { | ||
dataStore: new _client2.default.MemoryDataStore() | ||
useRtmConnect: true, | ||
dataStore: false | ||
}); | ||
@@ -71,10 +76,7 @@ | ||
value: function _is_registered(msg) { | ||
var _msg$split = msg.split(' '); | ||
var _msg$split = msg.split(' '), | ||
_msg$split2 = _slicedToArray(_msg$split, 1), | ||
prefix = _msg$split2[0]; | ||
var _msg$split2 = _slicedToArray(_msg$split, 1); | ||
var prefix = _msg$split2[0]; | ||
if (prefix.substring(1) in listeners) { | ||
if (prefix.substring(1) in global._ghee_listeners) { | ||
return true; | ||
@@ -98,31 +100,25 @@ } | ||
if (msg.text.startsWith('<@' + self.id + '>') || msg.text.startsWith('@' + self.name) || msg.text.startsWith(self.name) || msg.text.startsWith(self.prefix)) { | ||
var _msg$text$split = msg.text.split(' '); | ||
var _msg$text$split = msg.text.split(' '), | ||
_msg$text$split2 = _toArray(_msg$text$split), | ||
prefix = _msg$text$split2[0], | ||
method = _msg$text$split2[1], | ||
params = _msg$text$split2.slice(2); | ||
var _msg$text$split2 = _toArray(_msg$text$split); | ||
var prefix = _msg$text$split2[0]; | ||
var method = _msg$text$split2[1]; | ||
var params = _msg$text$split2.slice(2); | ||
if (method in listeners) { | ||
if (method in global._ghee_listeners) { | ||
self._sendMessage(msg, method, params); | ||
} | ||
} else if (msg.text.startsWith('.') && self._is_registered(msg.text)) { | ||
var _msg$text$split3 = msg.text.split(' '); | ||
var _msg$text$split3 = msg.text.split(' '), | ||
_msg$text$split4 = _toArray(_msg$text$split3), | ||
_prefix = _msg$text$split4[0], | ||
_params = _msg$text$split4.slice(1); | ||
var _msg$text$split4 = _toArray(_msg$text$split3); | ||
var _prefix = _msg$text$split4[0]; | ||
var _params = _msg$text$split4.slice(1); | ||
var _method = _prefix.substring(1); | ||
self._sendMessage(msg, _method, _params); | ||
} else if ('catch_all' in listeners) { | ||
} else if ('catch_all' in global._ghee_listeners) { | ||
self._sendMessage(msg, 'catch_all', msg.text); | ||
} | ||
if ('*' in listeners) { | ||
if ('*' in global._ghee_listeners) { | ||
self._sendMessage(msg, '*', msg.text); | ||
@@ -152,26 +148,24 @@ } | ||
var from = this.slack.dataStore.getUserById(msg.user); | ||
var channel = this.slack.dataStore.getChannelGroupOrDMById(msg.channel); | ||
return _bluebird2.default.join(this.web.users.info(msg.user), this.web.conversations.info(msg.channel), function (from, channel) { | ||
var response = this[listeners[method]](params, from, channel, msg); | ||
var response = _this[global._ghee_listeners[method]](params, from.user, channel.channel, msg); | ||
if (response) { | ||
if (isPromise(response)) { | ||
response.then(function (data) { | ||
if (isAttachment(data)) { | ||
_this._sendAttachment(data, msg.channel); | ||
} else { | ||
_this.slack.sendMessage(data, msg.channel); | ||
} | ||
}, function (text) { | ||
_this.slack.sendMessage(':warning: ' + text, msg.channel); | ||
}); | ||
} else if (isAttachment(response)) { | ||
this._sendAttachment(response, msg.channel); | ||
} else { | ||
this.slack.sendMessage(response, msg.channel); | ||
if (response) { | ||
if (isPromise(response)) { | ||
response.then(function (data) { | ||
if (isAttachment(data)) { | ||
_this._sendAttachment(data, msg.channel); | ||
} else { | ||
_this.slack.sendMessage(data, msg.channel); | ||
} | ||
}, function (text) { | ||
_this.slack.sendMessage(':warning: ' + text, msg.channel); | ||
}); | ||
} else if (isAttachment(response)) { | ||
_this._sendAttachment(response, msg.channel); | ||
} else { | ||
_this.slack.sendMessage(response, msg.channel); | ||
} | ||
} | ||
} | ||
return; | ||
}); | ||
} | ||
@@ -186,6 +180,6 @@ }]); | ||
return function (_target, _key) { | ||
listeners[target] = _key; | ||
global._ghee_listeners[target] = _key; | ||
}; | ||
} else { | ||
listeners[key] = key; | ||
global._ghee_listeners[key] = key; | ||
} | ||
@@ -192,0 +186,0 @@ } |
{ | ||
"name": "ghee", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "ES2016+ Slack Bot Framework", | ||
"main": "lib/ghee.js", | ||
"scripts": { | ||
"prepublish": "node_modules/babel-cli/bin/babel.js src --out-dir lib", | ||
"prepublish": "./node_modules/.bin/babel src --out-dir lib", | ||
"test": "./node_modules/.bin/mocha", | ||
@@ -32,6 +32,5 @@ "coverage": "./node_modules/.bin/istanbul --include-all-sources cover -x **/lib/** ./node_modules/.bin/_mocha --", | ||
"devDependencies": { | ||
"babel": "6.5.2", | ||
"babel-cli": "^6.14.0", | ||
"babel-core": "^6.14.0", | ||
"babel-preset-es2015": "^6.14.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-plugin-transform-decorators-legacy": "^1.3.4", | ||
"chai": "3.5.0", | ||
@@ -48,5 +47,8 @@ "chai-as-promised": "^6.0.0", | ||
"presets": [ | ||
"es2015" | ||
"env" | ||
], | ||
"plugins": [ | ||
"transform-decorators-legacy" | ||
] | ||
} | ||
} |
@@ -9,7 +9,2 @@ export class Mock_RtmClient { | ||
this.dataStore = { | ||
getUserById: sandbox.spy(), | ||
getChannelGroupOrDMById: sandbox.spy() | ||
} | ||
this.sendMessage = sandbox.spy(); | ||
@@ -16,0 +11,0 @@ |
@@ -8,3 +8,19 @@ export class Mock_WebClient { | ||
} | ||
this.users = { | ||
info: () => { | ||
return Promise.resolve({ | ||
user: { } | ||
}) | ||
} | ||
} | ||
this.conversations = { | ||
info: () => { | ||
return Promise.resolve({ | ||
channel: { } | ||
}) | ||
} | ||
} | ||
} | ||
} |
@@ -41,2 +41,17 @@ import chai from 'chai'; | ||
catch_all(msg) { } | ||
@ghee | ||
test_decorated_method(args, from, channel, msg) { | ||
return 'test_decorated_method'; | ||
} | ||
@ghee('test_named_decorated_method') | ||
test_an_alternative_named_decorated_method(args, from, channel, msg) { | ||
return 'test_named_decorated_method'; | ||
} | ||
@ghee('*') | ||
test_star_decorated_method(args, from, channel, msg) { | ||
return 'test_star_decorated_method'; | ||
} | ||
}; | ||
@@ -164,5 +179,11 @@ | ||
let sendMessage = null; | ||
let star_backup = null; | ||
beforeEach(() => { | ||
sendMessage = sinon.spy(instance, '_sendMessage'); | ||
if ('*' in global._ghee_listeners) { | ||
star_backup = global._ghee_listeners['*']; | ||
delete global._ghee_listeners['*']; | ||
} | ||
}); | ||
@@ -172,2 +193,6 @@ | ||
instance._sendMessage.restore(); | ||
if (star_backup) { | ||
global._ghee_listeners['*'] = star_backup; | ||
} | ||
}); | ||
@@ -283,4 +308,2 @@ | ||
ghee(instance, 'catch_all'); | ||
let catch_all = sinon.spy(instance, 'catch_all'); | ||
instance._is_registered('.catch_all').should.be.true; | ||
@@ -295,4 +318,17 @@ | ||
sendMessage.should.be.calledWithExactly(msg, 'catch_all', msg.text); | ||
catch_all.should.be.called; | ||
}); | ||
it('will send to a star decorated method if it is registered', () => { | ||
if (star_backup) { | ||
global._ghee_listeners['*'] = star_backup; | ||
} | ||
let msg = { | ||
text: 'test' | ||
}; | ||
parser(msg); | ||
sendMessage.should.be.calledWithExactly(msg, '*', msg.text); | ||
}); | ||
}); | ||
@@ -330,3 +366,7 @@ | ||
let test_rejected_promise_registered_method = sinon.spy(instance, 'test_rejected_promise_registered_method'); | ||
let test_decorated_method = sinon.spy(instance, 'test_decorated_method'); | ||
let test_an_alternative_named_decorated_method = sinon.spy(instance, 'test_an_alternative_named_decorated_method'); | ||
let test_star_decorated_method = sinon.spy(instance, 'test_star_decorated_method'); | ||
//let test_no_return_regsitered_method = sinon.spy(instance, 'test_no_return_registered_method'); | ||
let msg = { | ||
@@ -345,30 +385,61 @@ user: 12345, | ||
instance._sendMessage(msg, method, params); | ||
instance._sendMessage(msg, method, params).then(() => { | ||
test_registered_method.should.be.called; | ||
test_registered_method.should.have.returned(method); | ||
test_registered_method.should.be.called; | ||
test_registered_method.should.have.returned(method); | ||
instance.slack.sendMessage.should.be.calledWithExactly(method, msg.channel); | ||
}); | ||
instance.slack.sendMessage.should.be.calledWithExactly(method, msg.channel); | ||
}); | ||
it('calls a registered method with a resolved promise', () => { | ||
// TODO: Figure out how to see if slack.sendMessage was called | ||
let method = 'test_resolved_promise_registered_method'; | ||
instance._sendMessage(msg, method, params); | ||
test_resolved_promise_registered_method.should.be.called; | ||
test_resolved_promise_registered_method.should.have.returned(sinon.match.instanceOf(Promise)); | ||
instance._sendMessage(msg, method, params).then(() => { | ||
test_resolved_promise_registered_method.should.be.called; | ||
test_resolved_promise_registered_method.should.have.returned(sinon.match.instanceOf(Promise)); | ||
}); | ||
}); | ||
it('calls a registered method with a rejected promise', () => { | ||
// TODO: Figure out how to see if slack.sendMessage was called | ||
let method = 'test_rejected_promise_registered_method'; | ||
instance._sendMessage(msg, method, params); | ||
instance._sendMessage(msg, method, params).then(() => { | ||
test_rejected_promise_registered_method.should.be.called; | ||
test_rejected_promise_registered_method.should.have.returned(sinon.match.instanceOf(Promise)); | ||
}); | ||
}); | ||
test_rejected_promise_registered_method.should.be.called; | ||
test_rejected_promise_registered_method.should.have.returned(sinon.match.instanceOf(Promise)); | ||
it('calls a decorated method', () => { | ||
let method = 'test_decorated_method'; | ||
instance._sendMessage(msg, method, params).then(() => { | ||
test_decorated_method.should.be.called; | ||
test_decorated_method.should.have.returned(method); | ||
instance.slack.sendMessage.should.be.calledWithExactly(method, msg.channel); | ||
}); | ||
}); | ||
it('calls a named decorated method', () => { | ||
let method = 'test_named_decorated_method'; | ||
instance._sendMessage(msg, method, params).then(() => { | ||
test_an_alternative_named_decorated_method.should.be.called; | ||
test_an_alternative_named_decorated_method.should.have.returned(method); | ||
instance.slack.sendMessage.should.be.calledWithExactly(method, msg.channel); | ||
}); | ||
}); | ||
it('calls a * decorated catch all method', () => { | ||
let method = 'test_star_decorated_method'; | ||
instance._sendMessage(msg, '*', params).then(() => { | ||
test_star_decorated_method.should.be.called; | ||
test_star_decorated_method.should.have.returned(method); | ||
instance.slack.sendMessage.should.be.calledWithExactly(method, msg.channel); | ||
}); | ||
}); | ||
}); | ||
@@ -375,0 +446,0 @@ }); |
Sorry, the diff of this file is not supported yet
160878
11
20
1610