-
Notifications
You must be signed in to change notification settings - Fork 0
/
beer
executable file
·34 lines (30 loc) · 994 Bytes
/
beer
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
#!/usr/bin/env node
const sink = require('stream-sink')
const request = require('request')
const out = (d) => {process.stdout.write(JSON.stringify(d))}
const token = 'MDowMjc5ZTg3NC0wNDdjLTExZTYtOTYzZS01ZjYyNDg5MmVkOGE6Nmk1bnFtSUNIanhyUXNBNEVjZEJHYlpTcFRwQTRnalNXeDcx'
process.stdin.pipe(sink())
.on('data', (input) => {
input = JSON.parse(input).text
if (!input) return out({status: 'error', text: 'missing input'})
input = input.trim().toLowerCase()
request('https://lcboapi.com/products.json?'
+ `q=${encodeURIComponent(input)}`
+ `&access_key=${token}`
, {json: true}, (err, _, body) => {
if (err) return out({status: 'error', text: err.message})
const result = body.result
.map((beer) => [
beer.name
, '(' + (beer.price_in_cents / 100) + '$)'
, 'is a'
, beer.primary_category
, 'from'
, beer.origin
, 'with'
, (beer.alcohol_content / 100) + '%'
].join(' '))
.join('\n')
out({status: 'ok', text: result})
})
})