-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqr-generator.coffee
49 lines (40 loc) · 1.2 KB
/
qr-generator.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
# Description:
# Generate QR codes as PNG with size 128x128 pixels
# Using service from [QR Code Generator](http://goqr.me/api/doc/create-qr-code/)
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot qr gen <data>
#
# Author:
# eelcokoelewijn
# http(s)://api.qrserver.com/v1/create-qr-code/?data=[URL-encoded-text]&size=[pixels]x[pixels]
# Nevertheless up to 900 characters should work in general.
url = require 'url'
baseUrl = 'https://api.qrserver.com/v1/create-qr-code'
size = '128x128'
module.exports = (robot) ->
robot.respond /qr gen (.+)/i, (msg) ->
data = msg.match[1]
if data.length > 900
msg.send 'Maximum length for data is 900 characters.'
return
urlObj = makeUrlObj data
hackUrlObj = adapterHack urlObj, robot.adapterName
msg.send url.format hackUrlObj
makeUrlObj = (data) ->
urlObj = url.parse baseUrl
urlObj.query = {data: data, size: size}
return urlObj
adapterHack = (urlObj, adapterName) ->
# If the adapter name is null, the URL object of the argument is returned unchanged.
return urlObj if adapterName is null
# Switch by adapter.
if /hipchat/.test adapterName.toLowerCase()
urlObj.hash = '.png'
return urlObj