Skip to content

Commit

Permalink
(Refactor) Aplicando eslint y ordenando
Browse files Browse the repository at this point in the history
  • Loading branch information
leocabeza committed May 25, 2017
1 parent c10aff0 commit b39b5ac
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
13 changes: 11 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"env": {
"es6": true,
"node": true
"node": true,
"jest": true
},
"extends": "airbnb",
"plugins": [
Expand All @@ -19,6 +20,14 @@
"exports": "ignore",
"functions": "ignore"
}
],
"class-methods-use-this": [
2,
{
"exceptMethods": [
"checkMessage"
]
}
]
}
}
}
23 changes: 16 additions & 7 deletions src/events/tweets.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ function getLastTrackedTweetId() {
* tweet tracked
*/
if (err.code === 'ENOENT') {
fs.writeFileSync('./config/last-tweetId.json', '');
fs.writeFileSync('./config/last-tweetId.json', '{}');
return false;
}
throw err;

throw new Error(`Error reading file last-tweetId.json: ${err}`);
}
}

Expand All @@ -55,6 +56,11 @@ function updateLastTrackedTweetId(lastTweetId) {
* @param {response} response HTTP response
*/
function getLatestTweets(error, tweets) {
// If error getting tweets then do nothing
if (error !== null) {
return;
}

// If there is no tweets then do nothing
if (tweets.length === 0) {
return;
Expand All @@ -63,17 +69,19 @@ function getLatestTweets(error, tweets) {
/**
* If the last tweet id is the same that the last tracked tweet id, do nothing
*
* Note: The Twitter API has a strange behavior, when you use
* the parameter since_id equal to the most
* Note: The Twitter API has a strange behavior,
* when you use the parameter since_id equal to the most
* recent tweet, it brings you the same tweet...
* But, when you use since_id equal to an older tweet, it doesn't include the since_id tweet
* But, when you use since_id equal to an older tweet,
* it doesn't include the since_id tweet
*/
if (getLastTrackedTweetId() === tweets[0].id) {
return;
}

// Update the last tracked tweet id with the last tweet gotten
updateLastTrackedTweetId(tweets[0].id);

tweets.map(tweet => eventEmitter.emit('newTweet', tweet));
}

Expand All @@ -100,8 +108,9 @@ function checkNewTweets() {
'statuses/user_timeline',
{ screen_name: twitterAccount, count: 1 },
(error, tweets) => {
const lastNgTweetId = tweets[0].id;
updateLastTrackedTweetId(lastNgTweetId);
if (error === null && tweets.length > 0) {
updateLastTrackedTweetId(tweets[0].id);
}
}
);
}
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const TelegramBot = require('./bot/telegram-bot');
const BotServer = require('./server/bot-server');
const Superfeedr = require('./events/superfeedr');

const telegramToken = require('./../config/config').telegramToken;
const server = require('./../config/config').server;

const morningEvent = require('./events/morning');
const blogEvent = require('./events/blog');
const twitterEvent = require('./events/tweets');
const superfeedr = new Superfeedr();
const Superfeedr = require('./events/superfeedr');

const chatUtility = require('./utils/chat');
const blogUtility = require('./utils/blog');
Expand All @@ -18,7 +17,12 @@ const apiAIUtility = require('./utils/api-ai');
const twitterUtility = require('./utils/tweets');
const githubUtility = require('./utils/github-release');

const superfeedr = new Superfeedr();
const bot = new TelegramBot(telegramToken);

// This informs the Telegram servers of the new webhook.
bot.setWebHook(`${server.url}/${telegramToken}`);

// eslint-disable-next-line no-unused-vars
const botServer = new BotServer(`/${bot.token}`, server.port)
.subscribe(bot)
Expand All @@ -27,9 +31,6 @@ const botServer = new BotServer(`/${bot.token}`, server.port)
let goodMorningGivenToday = false;
let minuteToCheck = generateRandom(0, 59);

// This informs the Telegram servers of the new webhook.
bot.setWebHook(`${server.url}/${telegramToken}`);

bot
.on('new_chat_participant', msg => chatUtility.sayHello(bot, msg))
.on('left_chat_participant', msg => chatUtility.sayGoodbye(bot, msg))
Expand Down
12 changes: 8 additions & 4 deletions src/server/bot-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class BotServer {

// We are receiving updates at the route below!
app.post(path, (req, res) => {
this.webhooks.forEach(webhook => webhook.checkMessage(req.body) && webhook.proccessMessage(req.body));
this.webhooks.forEach(webhook =>
webhook.checkMessage(req.body) && webhook.proccessMessage(req.body)
);

res.sendStatus(200).end();
});
Expand All @@ -23,10 +25,12 @@ class BotServer {
}

subscribe(webhook) {
if (!BotServer.isWebHook(webhook)) throw new Error('Invalid argument exception');
if (BotServer.isWebHook(webhook)) {
this.webhooks.push(webhook);
return this;
}

this.webhooks.push(webhook);
return this;
throw new Error('Invalid argument exception');
}

static isWebHook(webhook) {
Expand Down
15 changes: 9 additions & 6 deletions src/utils/github-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ const config = require('./../../config/config');
const githubReleaseMessage = require('./../../config/messages').githubRelease;
const sendMessage = require('./../utils/send-message');

checkForRelease = (repository, feed) => feed && feed.status.feed && feed.status.feed.search(repository) !== -1;
const checkForRelease = (repository, feed) =>
feed && feed.status.feed && feed.status.feed.search(repository) !== -1;

sendRelease = (bot, release, repository, changelogExist) => {
if (!release.items.length) return;
const sendRelease = (bot, release, repository, changelogExist) => {
if (release.items.length === 0) {
return;
}

const last = release.items[0];
const name = repository.match(/[\w\.-]+$/gi)[0]
const tag = last.id.match(/[\w\.-]+$/gi)[0];
const name = repository.match(/[\w.-]+$/gi)[0];
const tag = last.id.match(/[\w.-]+$/gi)[0];

sendMessage(
bot,
Expand All @@ -29,4 +32,4 @@ sendRelease = (bot, release, repository, changelogExist) => {
module.exports = {
checkForRelease,
sendRelease
}
};

0 comments on commit b39b5ac

Please sign in to comment.