-
Notifications
You must be signed in to change notification settings - Fork 15
/
get-history-list.js
57 lines (42 loc) · 1.45 KB
/
get-history-list.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict'
function parseDate( dateStr ) {
const date = new Date( dateStr )
if ( isNaN( date.getTime())) {
throw new Error( `"${dateStr}" could not be parsed using Date constructor` )
}
return date
}
const emojis = {
motion: '🏃',
ding: '🛎',
on_demand: '📱'
}
module.exports = bottle => bottle.service( 'getHistoryList', getHistoryList,
'restClient',
'apiUrls'
)
function getHistoryList( restClient, apiUrls ) {
class HistoryItem {
constructor( jsonFromResponse ) {
// a naieve copy like this could one day create a bug if Ring add property
// names to their client API that shaddow our OOP methods
Object.assign( this, jsonFromResponse )
this.created_at = parseDate( jsonFromResponse.created_at )
}
get apiUri() {
return apiUrls.dings().ding( this )
}
async videoUrl() {
const response = await restClient.authenticatedRequest( 'GET', this.apiUri.recording())
return response.url
}
toString() {
return `[${this.kind} ${emojis[ this.kind ] || ''} at "${this.doorbot.description}" ${this.created_at}]`
}
}
return async() => {
const historyListUrl = apiUrls.doorbots().history()
const historyItems = await restClient.authenticatedRequest( 'GET', historyListUrl )
return historyItems.map( h => new HistoryItem( h ))
}
}