Skip to content

Commit

Permalink
:fix: Fix the response body.
Browse files Browse the repository at this point in the history
Fixed the response body to contain valid options.
  • Loading branch information
edloidas committed May 18, 2016
1 parent 66fd638 commit 01bfdc8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
8 changes: 1 addition & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ bot.onText( message.type.start.regexp, ( msg ) => {

function commonRollHandler( type, msg, match ) {
const { id, username } = msg.from;

try {
const values = message.parse( match[ 2 ], type );
const { view, result } = dice.namedRoll( type, values );

const resp = message.getResponse( username, view, result );
const options = message.type[ type ];

const { resp, options } = message.getMessageBody( type, username, match[ 2 ]);
bot.sendMessage( id, resp, options );
} catch ( err ) {
bot.sendMessage( id, message.getErrorMessage( username ));
Expand Down
14 changes: 12 additions & 2 deletions src/message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const compact = require( 'lodash' ).compact;
const dice = require( './dice' );

function Message() {
this.type = {
Expand Down Expand Up @@ -67,11 +68,20 @@ Message.prototype.matchAndParse = function matchAndParse( msg, type ) {
};

Message.prototype.getResponse = function getResponse( username, view, result ) {
return `${ username } \`(${ view })\` *${ result }*`;
return `@${ username } \`(${ view })\` *${ result }*`;
};

Message.prototype.getErrorMessage = function getResponse( username ) {
return `${ username } : \`(${ this.error })\``;
return `@${ username } : \`(${ this.error })\``;
};

Message.prototype.getMessageBody = function getMessageBody( type, username, matchedValues ) {
const values = this.parse( matchedValues, type );
const { view, result } = dice.namedRoll( type, values );

const resp = this.getResponse( username, view, result );
const options = this.type[ type ].options;
return { resp, options };
};

module.exports = new Message();
24 changes: 24 additions & 0 deletions test/message/getMessageBody.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const should = require( 'should' );
const message = require( '../../src/message' );

before(( done ) => {
// HACK: bypass ESLint no-unused-var error
should.exist( should );
done();
});

describe( 'Message.getMessageBody', () => {
const msg = 'generate valid response';
describe( msg, () => {
[ 'roll', 'sroll', 'droll', 'random' ].forEach(( type ) => {
it( type, ( done ) => {
const body = message.getMessageBody( type, 'me', '2 3 4' );
should.exist( body );
should.exist( body.resp );
should.exist( body.options );
JSON.stringify( body.options ).should.be.equal( '{"parse_mode":"Markdown"}' );
done();
});
});
});
});

0 comments on commit 01bfdc8

Please sign in to comment.