-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.coffee
33 lines (29 loc) · 1.08 KB
/
index.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
puts = console.log
inspect = require('sys').inspect
http = require 'http'
urlencode = (str) ->
str = (str+'').toString()
encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+')
@version = '0.0.1'
@host = 'www.tera-wurfl.com'
@port = 80
@path = '/Tera-Wurfl/webservice.php'
@query = (ua, fields, callback) =>
ua = urlencode(ua)
fields = fields.join '|'
wurfl = http.createClient @port, @host
request = wurfl.request 'GET', "#{@path}?ua=#{ua}&search=#{fields}&format=json", 'host': @host
request.end()
request.on 'response', (response) ->
if response.statusCode isnt 200
callback [{'Expected response with status code HTTP 200 OK, got: ': response.statusCode}], null
else
response.setEncoding 'utf8'
data = ''
response.on 'data', (chunk) -> data += chunk
response.on 'end', ->
try
data = JSON.parse data
callback data.errors, data.capabilities
catch e
callback [{'Error parsing response as JSON': e}], null