Skip to content
Permalink
Browse files

Integrate Insulter API (Fixes #2)

* Integrate Insulter

* Add unit tests for insulter and rename cityName to actionTarget
  • Loading branch information...
ryzokuken authored and yashmehrotra committed Oct 16, 2016
1 parent 5919912 commit cc09ccc54793606074418a05e7fbba86ee2d9a5f
Showing with 55 additions and 6 deletions.
  1. +7 −4 chatbot.js
  2. +5 −2 constants.js
  3. +13 −0 services/insultService.js
  4. +30 −0 test/insultServiceSpec.js
@@ -14,14 +14,15 @@ const places = require('./services/mapService');
const quotation = require('./services/quoteService.js');
const define = require('./services/defineService.js');
const karma = require('./services/karmaService.js');
const insulter = require('./services/insultService.js');

const DEPLOY_FLAG = process.env.DEPLOY;
const TEST_FLAG = process.env.TEST;


const getBotReply = (username, message, parsedMessage) => {
const startsWithString = utils.getStartsWith(message);
const cityName = message.substr(message.indexOf(' ') + 1);
const actionArgument = message.substr(message.indexOf(' ') + 1);
if (startsWithString === constants.BOT_ACTIONS.HELP) {
api.postBotReply(utils.generateBotHelp(), username);
} else if (startsWithString === constants.BOT_ACTIONS.JOKE) {
@@ -41,11 +42,13 @@ const getBotReply = (username, message, parsedMessage) => {
username,
message.slice(constants.BOT_ACTIONS.WIKI.length + 1));
} else if (startsWithString === constants.BOT_ACTIONS.WEATHER) {
weather.getWeather(api.postBotReply, username, cityName);
weather.getWeather(api.postBotReply, username, actionArgument);
} else if (startsWithString === constants.BOT_ACTIONS.PLACES) {
places.getPlaces(api.postBotReply, username, cityName);
places.getPlaces(api.postBotReply, username, actionArgument);
} else if (startsWithString === constants.BOT_ACTIONS.DEFINE) {
define.define(api.postBotReply, username, cityName);
define.define(api.postBotReply, username, actionArgument);
} else if (startsWithString === constants.BOT_ACTIONS.INSULT) {
insulter.getInsult(api.postBotReply, actionArgument);
} else if (utils.getStartsWith(parsedMessage) === constants.BOT_ACTIONS.KARMA) {
const msgBody = parsedMessage.split(' ');
const karmaUser = msgBody[1];
@@ -15,7 +15,8 @@ const BOT_ACTIONS = {
WEATHER: 'weather',
PLACES: 'locate',
DEFINE: 'define',
KARMA: 'karma'
KARMA: 'karma',
INSULT: 'insult'
};

const META_HANDSHAKE_SUFFIX_URL = '/meta/handshake';
@@ -47,6 +48,7 @@ const WIKI_ARTICLE_PREFIX_URL = 'https://en.wikipedia.org/?curid=';
const KELVIN_CELCIUS_OFFSET = 273;
const DEFINE_API_URL = 'https://mashape-community-urban-dictionary.p.mashape.com/define';
const DEFINE_API_KEY = 'MRn5Ke2MMTmshHp839whoCom3Nx2p1Fsdo5jsnBBhSTxU3Zdo2';
const INSULTS_API_URL = 'https://quandyfactory.com/insult/json';

module.exports = {
BOT_MENTION_NAME,
@@ -70,5 +72,6 @@ module.exports = {
WIKI_ARTICLE_PREFIX_URL,
KELVIN_CELCIUS_OFFSET,
DEFINE_API_URL,
DEFINE_API_KEY
DEFINE_API_KEY,
INSULTS_API_URL
};
@@ -0,0 +1,13 @@
'use strict';
const request = require('request');
const constants = require('../constants');

module.exports = {
getInsult: (callback, username) => {
request(constants.INSULTS_API_URL, (error, response, body) => {
if (!error && response.statusCode === 200) {
return callback(JSON.parse(body).insult, username);
}
});
}
};
@@ -0,0 +1,30 @@
'use strict';
const chai = require('chai');
const expect = chai.expect;
const spies = require('chai-spies');
const request = require('request');
const constants = require('../constants');
const insultService = require('../services/insultService');

describe('Module insultService', () => {
chai.use(spies);

it('should fetch the insult correctly from the api', () => {
request.get(constants.INSULTS_API_URL, (error, response, body) => {
expect(response.statusCode).to.equal(200);
expect(JSON.parse(body).insult).to.be.a('string');
expect(error).to.equal(null);
});
});

it('should check for the callback', (done) => {
const USERNAME = 'username';
const callback = (insult, username) => {
expect(username).to.equal(USERNAME);
expect(insult).to.be.a('string');
done();
};
const spy = chai.spy(callback);
insultService.getInsult(spy, USERNAME);
});
});

0 comments on commit cc09ccc

Please sign in to comment.
You can’t perform that action at this time.