Skip to content

Commit

Permalink
Moar refactoring, added todo for #2
Browse files Browse the repository at this point in the history
  • Loading branch information
tarlepp committed Jan 5, 2015
1 parent ba2db0f commit b9fd2e6
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ module.exports = {
},
"messages": {
"success": "${timeAgo} ${item.title} - ${messages} - ${shortUrl}",
"errorThreshold": "You did try to fetch more feed items that is allowed! Maximum feed item count is ${config.messageCount.maximum}",
"errorNoFeedItems": "Sorry no GitHub public activity feed items for ${username} - ${url}"
"errors": {
"threshold": "You did try to fetch more feed items that is allowed! Maximum feed item count is ${config.messageCount.maximum}",
"request": "Oh noes error with request - ${error}",
"feedParser": "Oh noes error with FeedParser - ${error}",
"noItems": "Sorry no GitHub public activity feed items for ${username} - ${url}"
}
}
}
}
Expand Down Expand Up @@ -88,11 +92,19 @@ messages = GitHub messages from feed item (commit messages, comment title
shortUrl = Feed item link value as in "shorturl" form
```

#### messages.errorThreshold
#### messages.errors.threshold
Message that is emitted to user when he/she has requested more feed items that are allowed. Note that this message is
sent to user as a private message.

#### messages.errorNoFeedItems
#### messages.errors.request
Message that is emitted to user whenever HTTP request error occurs when plugin is processing GitHub feed URL. Note that
this message is sent to user as a private message.

#### messages.errors.feedParser
Message that is emitted to user whenever FeedParser error occurs when plugin is processing feed item. Note that this
message is sent to user as a private message.

#### messages.errors.noItems
Message that is emitted to user when he/she has requested GitHub feeds from user that is a real GitHub user but he/she
doesn't have feed items yet. Note that this message is sent to user as a private message.

Expand All @@ -113,3 +125,8 @@ Note that default count of items is one (1) and it can be configured.
* [Moment.js](http://momentjs.com/) - Parse, validate, manipulate, and display dates in JavaScript.
* [cheerio](https://github.com/cheeriojs/cheerio) - Fast, flexible, and lean implementation of core jQuery designed specifically for the server.
* [node-shorturl](https://github.com/jdub/node-shorturl) - shorturl is a simple, asynchronous client library for common URL shortener services.

## Future plans
Add more GitHub specified actions to this plugin like;
* Repository issue/pr/author/milestone lists (latest, etc.)
* More? Do you have an idea? please make PR here!
61 changes: 41 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
var FeedParser = require('feedparser');
var request = require('request');
var cheerio = require('cheerio')
var cheerio = require('cheerio');
var moment = require('moment');
var shorturl = require('shorturl');
var _ = require('lodash');
Expand All @@ -15,6 +15,8 @@ var _ = require('lodash');
*
* @todo Refactor regexp part of plugin
* @todo Add messages for req and feedparser errors
* @todo What else messages there can be on feed? Now this supports only commits and comments
* @todo Move default configuration to separated file.
*
* @param {Object} options Plugin options object, description below.
* db: {mongoose} the mongodb connection
Expand Down Expand Up @@ -43,8 +45,12 @@ module.exports = function init(options) {
* },
* messages: {
* success: string,
* errorThreshold: string,
* errorNoFeedItems: string
* errors: {
* threshold: string,
* request: string,
* feedParser: string,
* noItems: string
* }
* }
* }}
*/
Expand All @@ -60,8 +66,12 @@ module.exports = function init(options) {
},
"messages": {
"success": "${timeAgo} ${item.title} - ${messages} - ${shortUrl}",
"errorThreshold": "You did try to fetch more feed items that is allowed! Maximum feed item count is ${config.messageCount.maximum}",
"errorNoFeedItems": "Sorry no GitHub public activity feed items for ${username} - ${url}"
"errors": {
"threshold": "You did try to fetch more feed items that is allowed! Maximum feed item count is ${config.messageCount.maximum}",
"request": "Oh noes error with request - ${error}",
"feedParser": "Oh noes error with FeedParser - ${error}",
"noItems": "Sorry no GitHub public activity feed items for ${username} - ${url}"
}
}
};

Expand Down Expand Up @@ -90,12 +100,6 @@ module.exports = function init(options) {
username = matches[1];
}

if (itemCount > pluginConfig.messageCount.maximum) {
channel.say(pluginConfig.messages.errorThreshold, from);

return;
}

// Default template variables
var templateVars = {
"url": "https://github.com/" + username + ".atom",
Expand All @@ -104,13 +108,22 @@ module.exports = function init(options) {
"config": pluginConfig
};

// User did try to get more feed items that is allowed
if (itemCount > pluginConfig.messageCount.maximum) {
channel.say(pluginConfig.messages.errors.threshold, from);

return;
}

// Make new request and initialize FeedParser
var req = request(templateVars.url);
var feedparser = new FeedParser();
var feedParser = new FeedParser();

// On request error send message to user
req.on("error", function onError(error) {
channel.say("Oh noes error with request - " + error, from);
templateVars.error = error;

channel.say(pluginConfig.messages.errors.request, from);
});

// On request response pipe stream to FeedParser
Expand All @@ -122,12 +135,14 @@ module.exports = function init(options) {
return this.emit("error", new Error("Bad status code"));
}

stream.pipe(feedparser);
stream.pipe(feedParser);
});

// On FeedParser error send message to user
feedparser.on("error", function onError(error) {
channel.say("Oh noes error with FeedParser - " + error, from);
feedParser.on("error", function onError(error) {
templateVars.error = error;

channel.say(pluginConfig.messages.errors.feedParser, from);
});

// Initialize counter
Expand All @@ -137,18 +152,22 @@ module.exports = function init(options) {
var target = itemCount > pluginConfig.messageCount.threshold ? from : undefined;

// On FeedParser result
feedparser.on("readable", function iterator() {
feedParser.on("readable", function iterator() {
var stream = this;
var item;

// Iterate each feed item
while (item = stream.read()) {
if (i < itemCount) {
// Add extra template variables
templateVars.item = item;
templateVars.formattedDate = moment(item.date).format(pluginConfig.moment.format);
templateVars.timeAgo = moment(item.date).fromNow();

// Parse item description as 'jQuery' object
var $ = cheerio.load(item.description);

// Initialize "real" GitHub messages
var messages = [];

// Check possible commit messages
Expand All @@ -165,7 +184,8 @@ module.exports = function init(options) {

templateVars.messages = messages.length ? messages.join(', ') : 'No detailed info';

shorturl(item.link, function(shortUrl) {
// Fetch shorturl for current feed item
shorturl(item.link, function done(shortUrl) {
templateVars.shortUrl = shortUrl;

channel.say(_.template(pluginConfig.messages.success, templateVars), target);
Expand All @@ -179,9 +199,10 @@ module.exports = function init(options) {
});

// And after all is done
feedparser.on("end", function onEnd() {
feedParser.on('end', function onEnd() {
// Didn't find any feed items, so sent message about that to user
if (i === 0) {
channel.say(_.template(pluginConfig.messages.errorNoFeedItems, templateVars), from);
channel.say(_.template(pluginConfig.messages.noItems, templateVars), from);
}
});
}
Expand Down

0 comments on commit b9fd2e6

Please sign in to comment.