Skip to content

Commit 46ab071

Browse files
committed
package to retrieve upcomming events of hackafe
1 parent 41fd39f commit 46ab071

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

.gitignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ npm-debug.log*
77
pids
88
*.pid
99
*.seed
10+
*.pid.lock
1011

1112
# Directory for instrumented libs generated by jscoverage/JSCover
1213
lib-cov
@@ -33,8 +34,14 @@ jspm_packages
3334
# Optional npm cache directory
3435
.npm
3536

37+
# Optional eslint cache
38+
.eslintcache
39+
3640
# Optional REPL history
3741
.node_repl_history
3842

43+
# Output of 'npm pack'
44+
*.tgz
3945

40-
.idea/
46+
# Idea
47+
.idea/

config.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
apiKey: 'd5c03d2f89476c7f77dc5f5c4d2fbe00',
3+
boardId: 'GHda069L'
4+
};

index.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
var debug = require('debug')('hackafe-events');
2+
var config = require('./config');
3+
var Promise = require('promise');
4+
var get = require('simple-get');
5+
6+
var FACEBOOK_EVENT_PATTERN = /((https?:)?\/\/(www.)?facebook\.com\/events\/\d+)/i;
7+
var FORUM_THREAD_PATTERN = /((https?:)?\/\/frm\.hackafe\.org\/t\/[-a-zA-Z0-9]+\/\d+)/i;
8+
9+
var labelCache = {};
10+
11+
module.exports = function (opts, cb) {
12+
if (typeof opts === 'function') {
13+
cb = opts;
14+
opts = {};
15+
}
16+
17+
return getBoardVisibleCards(config.boardId)
18+
.then(function (cards) {
19+
return Promise.all(cards.map(transform));
20+
})
21+
.then(function (events) {
22+
return events.filter(upcomming);
23+
})
24+
.nodeify(cb);
25+
};
26+
27+
function tfetch(opts) {
28+
if (typeof opts === 'string') opts = {url: opts};
29+
opts.url += '?key=' + config.apiKey;
30+
opts.json = true;
31+
return new Promise(function (resolve, reject) {
32+
debug('fetching %s', opts.url);
33+
get.concat(opts, function (err, res, data) {
34+
if (err) return reject(err);
35+
debug('fetched %s', opts.url);
36+
resolve(data);
37+
});
38+
});
39+
}
40+
41+
function getBoardVisibleCards(boardId) {
42+
return tfetch('https://api.trello.com/1/boards/' + config.boardId + '/cards/visible');
43+
}
44+
45+
function getCardAttachment(cardId, attachmentId) {
46+
return tfetch('https://api.trello.com/1/cards/' + cardId + '/attachments/' + attachmentId);
47+
}
48+
49+
function getLabel(labelId) {
50+
return labelCache[labelId] = labelCache[labelId] || tfetch('https://api.trello.com/1/labels/' + labelId);
51+
}
52+
53+
function transform(card) {
54+
return Promise.resolve({
55+
id: card.id,
56+
desc: card.desc,
57+
name: card.name,
58+
start: card.due && new Date(card.due),
59+
url: card.url
60+
})
61+
.then(function fetchCover(event) {
62+
if (!card.idAttachmentCover) return event;
63+
return getCardAttachment(card.id, card.idAttachmentCover)
64+
.then(function (attachment) {
65+
event.cover = attachment.url;
66+
return event;
67+
})
68+
})
69+
.then(function extractFacebookEvent(event) {
70+
var match = event.desc &&
71+
event.desc.match(FACEBOOK_EVENT_PATTERN);
72+
if (match)
73+
event.facebookEvent = match[0];
74+
return event;
75+
})
76+
.then(function extractForumThread(event) {
77+
var match = event.desc &&
78+
event.desc.match(FORUM_THREAD_PATTERN);
79+
if (match)
80+
event.forumThread = match[0];
81+
return event;
82+
})
83+
.then(function resolveLabels(event) {
84+
if (!card.idLabels) return event;
85+
return Promise.all(card.idLabels.map(getLabel))
86+
.then(function (labels) {
87+
event.labels = labels;
88+
return event;
89+
});
90+
})
91+
}
92+
93+
function upcomming(event) {
94+
return event.start && event.start.getTime() >= new Date().getTime();
95+
}

package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "hackafe-events",
3+
"version": "0.0.1",
4+
"description": "Retrieve Hackafe events",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"hackafe",
11+
"events"
12+
],
13+
"author": "Geno Roupsky",
14+
"license": "ISC",
15+
"dependencies": {
16+
"debug": "^2.2.0",
17+
"hackafe-trello-config": "*",
18+
"promise": "^7.1.1",
19+
"simple-get": "^2.3.0"
20+
}
21+
}

0 commit comments

Comments
 (0)