-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext-meetup.js
More file actions
38 lines (32 loc) · 997 Bytes
/
next-meetup.js
File metadata and controls
38 lines (32 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict'
const fetch = require('node-fetch')
const moment = require('moment')
const cache = require('memory-cache')
const URL = 'https://api.meetup.com/Hamburg-AngularJS-Meetup/events?photo-host=public&page=1&sig_id=195379857&status=upcoming&sig=a9872ee1923a5237a19da19d196f588e0ebcc5ac'
const CACHE_TTL = 1000 * 60 * 60
const ensure200 = res => {
if (res.status !== 200) {
throw new Error(`Unable to get next meetup from Meetup API: ${res.status} - ${res.statusText}`)
}
return res
}
const formatDate = json => {
return {
date: moment(parseInt(json[0].time))
.format('MMMM DD, YYYY')
}
}
module.exports = () => {
let nextMeetup = cache.get('next-meetup')
if (nextMeetup) {
return Promise.resolve(nextMeetup)
}
return fetch(URL)
.then(res => ensure200(res))
.then(res => res.json())
.then(json => {
nextMeetup = formatDate(json)
cache.put('next-meetup', nextMeetup, CACHE_TTL)
return Promise.resolve(nextMeetup)
})
}