forked from homedepot/hubot-book-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook-list.coffee
357 lines (284 loc) · 10.2 KB
/
book-list.coffee
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Description
# Manages a list of books, using the google books api for data
#
# Commands:
# hubot booklist random - Returns a random book
# hubot booklist add <title> - adds the book to the booklist
# hubot booklist lookup <index> - retrieves book information
# hubot booklist - displays full booklist
# hubot booklist edit <index> <title> - edit book at index with new title
# hubot booklist review book <index> stars <rating> - rates the selected book
# hubot booklist add copy <index> <location> - add copy of book with owner and location details
# hubot booklist copies - retrieves booklist copies
#
# Author:
# Thomas Gamble & Paul Gaffney
#
# Title image by Alejandro Escamilla - hosted by unsplash.com with CCO-1.0 License
module.exports = (robot) ->
BOOK =
TITLE: 0
AUTHOR: 1
CATEGORY: 2
IMAGE: 3
RATING: 4
REVIEWCOUNT: 5
COPIES: 6
TITLE_IMAGE = 'https://goo.gl/g5Itaz'
BOOKCASE_URL = process.env.HUBOT_BOOKCASE_URL;
robot.hear /booklist initialize/i, (res) ->
if robot.brain.get('booklist')
emitString(res, "Booklist already exists")
else
robot.brain.set('booklist', [])
emitString(res, "Booklist Initialized")
robot.hear /booklist db (.*)$/i, (res) ->
booklist = getBookList()
if res.match[1] == "save"
return emitString(res, "Null booklist") if booklist is null
if booklist.length == 0
emitString(res,"no-books")
else
robot.http(BOOKCASE_URL + '/api/books')
.header("Content-Type", "application/json")
.header("Accept", "*/*")
.post(JSON.stringify(booklist)) (err, resp, body) ->
if err
emitString(res, "BACKUP ERROR -" + err)
emitString(res, BOOKCASE_URL)
else
emitString(res, "Backed up - " + body)
else if res.match[1] == "load"
if booklist and booklist.length > 0
return emitString(res, "Booklist already exists")
else
robot.http(BOOKCASE_URL + '/api/books/newest')
.get() (err, resp, body) ->
if err
emitString(res, "RELOAD ERROR - " + err)
else
robot.brain.set 'booklist', JSON.parse(body)
emitString(res, "Booklist re-loaded")
robot.hear /booklist add (?:(?!copy))(.*)$/i, (res) ->
rawBookToAdd = res.match[1]
rating = 0
nbrOfReviews = 0
addBook res, rawBookToAdd, rating, nbrOfReviews, null, (err) ->
return emitString(res,"ADD ERROR - #{err}") if err
formatBookInfo getLastBook(), "Added: ", (book, err) ->
return emitString(res,"ADD ERROR - #{err}") if err
robot.emit 'slack-attachment',
channel: res.envelope.room
content: book
robot.hear /booklist review book (\d{1,5}) stars (\d{1})/i, (res) ->
index = res.match[1]
reviewRating = parseInt(res.match[2], 10)
if reviewRating > 5
return emitString(res,"Ratings must be between 1 and 5")
maxIndex = getBookList().length - 1
if index > maxIndex
return emitString(res,"BOOK DOES NOT EXIST ERROR")
else
addReview index, reviewRating
formatBookInfo getBookAtIndex(index), "Reviewed: #{index} - ", (formattedBook, err) ->
return emitString(res,"EDIT ERROR - #{err}") if err
robot.emit 'slack-attachment',
channel: res.envelope.room
content: formattedBook
robot.hear /booklist random/i, (res) ->
booklist = getBookList()
if booklist.length == 0
return emitString(res, "no-books")
else
randomBook = res.random getBookList()
index = getBookList().indexOf(randomBook)
formatBookInfo randomBook, "Random - #{index}: ", (book, err) ->
return emitString(res,"RANDOM ERROR - #{err}") if err
robot.emit 'slack-attachment',
channel: res.envelope.room
content: book
robot.hear /booklist$/i, (res) ->
booklist = getBookList()
return emitString(res, "Null booklist") if booklist is null
if booklist.length == 0
return emitString(res,"no-books")
else
fields = []
booklist.map (book) ->
fields.push
title: "#{booklist.indexOf(book)} - #{book[BOOK.TITLE].value}"
value: "#{book[BOOK.AUTHOR].value}, #{book[BOOK.CATEGORY].value}"
payload =
title: "Booklist - #{getBookList().length} books"
thumb_url: TITLE_IMAGE
fields: fields
robot.emit 'slack-attachment',
channel: res.envelope.room
content: payload
robot.hear /booklist lookup (\d{1,20})$/i, (res) ->
index = res.match[1]
maxIndex = getBookList().length - 1
if index > maxIndex
return emitString(res,"LOOKUP ERROR")
else
formatBookInfo getBookAtIndex(index), "Index #{index}: ", (book, err) ->
return emitString(res,"LOOKUP ERROR - #{err}") if err
robot.emit 'slack-attachment',
channel: res.envelope.room
content: book
robot.hear /booklist edit (\d{1,20}) (.*)$/i, (res) ->
rating = 0
nbrOfRatings = 0
index = res.match[1]
newTitle = res.match[2]
maxIndex = getBookList().length - 1
if index > maxIndex
return emitString(res,"EDIT ERROR")
else
addBook res, newTitle, rating, nbrOfRatings, index, (err) ->
return emitString(res,"EDIT ERROR - #{err}") if err
formatBookInfo getBookAtIndex(index), "Updated: #{index} is ", (book, err) ->
return emitString(res,"EDIT ERROR - #{err}") if err
robot.emit 'slack-attachment',
channel: res.envelope.room
content: book
robot.hear /booklist add copy (\d{1,20}) (.*)$/i, (res) ->
index = res.match[1]
ownerId = getUserId(res)
location = res.match[2]
addCopy(index, ownerId, location)
formatBookInfo getBookAtIndex(index), "Index #{index}: ", (book, err) ->
return emitString(res,"LOOKUP ERROR - #{err}") if err
robot.emit 'slack-attachment',
channel: res.envelope.room
content: book
robot.hear /booklist copies$/i, (res) ->
booklist = getBookList()
return emitString(res, "Null booklist") if booklist is null
if booklist.length == 0
return emitString(res,"no-books")
else
fields = []
booklist.map (book) ->
copyList = ""
copies = []
if book[BOOK.COPIES]
copies = book[BOOK.COPIES].value
copies.map (copy) ->
copyList += "\t #{copies.indexOf(copy)} - Owner: <@#{copy.ownerId}>, Location: #{copy.location}\n"
fields.push
title: "#{booklist.indexOf(book)} - #{book[BOOK.TITLE].value}"
value: "Copies(#{copies.length})\n #{copyList}"
payload =
title: "Booklist - #{getBookList().length} books"
thumb_url: TITLE_IMAGE
fields: fields
robot.emit 'slack-attachment',
channel: res.envelope.room
content: payload
getBookAtIndex = (index) ->
getBookList()[index]
getBookList = ->
robot.brain.get('booklist')
addBook = (msg, title, rating, nbrOfReviews, index, cb) ->
bookEnhancementQuery msg, title, (data, err) ->
return cb err if err
formatInfo data, rating, nbrOfReviews, (enhancedBook, err) ->
return cb err if err
booklist = getBookList()
if index
getBookList()[index] = enhancedBook
else
booklist.push enhancedBook
cb err
addCopy = (index, ownerId, location) ->
book = getBookAtIndex(index)
copies = book[BOOK.COPIES].value
copies.push
ownerId: ownerId
location: location
addReview = (index, newRating) ->
book = getBookAtIndex(index)
newRating = parseInt(newRating, 10)
currentAverage = 0
nbrOfReviews = 0
if book[BOOK.RATING] and book[BOOK.REVIEWCOUNT]
currentAverage = book[BOOK.RATING].value
nbrOfReviews = book[BOOK.REVIEWCOUNT].value
newTotalOfAllRatings = currentAverage * nbrOfReviews + newRating
nbrOfReviews++
newAverage = newTotalOfAllRatings / nbrOfReviews
book[BOOK.RATING].value = newAverage
book[BOOK.REVIEWCOUNT].value = nbrOfReviews
getLastBook = ->
booklist = getBookList()
last = booklist.length - 1
getBookAtIndex(last)
getUserId = (res) ->
"#{res.message.user.id}"
emitString = (res, string="Error") ->
payload =
title: string
robot.emit 'slack-attachment',
channel: res.envelope.room
content: payload
formatBookInfo = (book, action, cb) ->
currentAverage = 0
if book[BOOK.RATING] and book[BOOK.REVIEWCOUNT]
currentAverage = book[BOOK.RATING].value
payload =
title: action + book[BOOK.TITLE].value
thumb_url: book[BOOK.IMAGE].value
fields: [
{ short: true, title: "Author", value: book[BOOK.AUTHOR].value }
{ short: true, title: "Category", value: book[BOOK.CATEGORY].value }
{ short: true, title: "Average Rating", value: currentAverage }
{ short: true, title: "Copies", value: book[BOOK.COPIES].value.length }
]
cb(payload, null)
formatInfo = (data, avgRating, nbrOfReviews, cb) ->
try
book = data.items[0].volumeInfo
author = book.authors[0]
title = book.title
category = if book.categories then book.categories[0] else "not set"
image = if book.imageLinks then book.imageLinks.thumbnail else TITLE_IMAGE
copies = []
catch err
return cb(enhancedBook, err)
enhancedBook = []
enhancedBook.push
key: "Title"
value: title
enhancedBook.push
key: "Author"
value: author
enhancedBook.push
key: "Category"
value: category
enhancedBook.push
key: "Image"
value: image
enhancedBook.push
key: "Average Rating"
value: avgRating
enhancedBook.push
key: "Number of Reviews"
value: nbrOfReviews
enhancedBook.push
key: "Copies"
value: copies
cb(enhancedBook, err)
bookEnhancementQuery = (res, search_terms, cb) ->
res.http("https://www.googleapis.com/books/v1/volumes?q='#{search_terms}'&maxResults=1")
.get() (err, resp, body) ->
if(err)
err = "Lookup Error - #{err}"
else
try
data = JSON.parse body
if not data.items
err = "Lookup Error - search failed for title: #{search_terms}"
catch err
err = "Lookup Error - #{err}"
cb(data, err)