forked from ydaniv/ok-webfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.coffee
36 lines (30 loc) · 988 Bytes
/
api.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
http = require 'http'
_ = require 'underscore'
log = console.log
RESOURCES = ['member','party', 'bill']
options =
host: "oknesset.uumpa.com"
port: 80
exports.get = get = (path = '', member = '', format = 'json', fn) ->
if format is 'json'
if _(RESOURCES).include path
o = _.extend options, {path: "/#{path}/#{member}/"}
log "Sending Request: #{o.host}#{o.path}"
# GET Request
http.get(o, (res) ->
log "Response Received: #{res.statusCode}"
fn(new Error("Error - Status Code: #{res.statusCode}")) unless res.statusCode is 200
data = ''
res.on 'data', (chunk) ->
data += chunk.toString 'UTF-8'
res.on 'end', ->
# All chunks received,
# parse to json
data = JSON.parse data
try
fn null, data
catch error
fn(error)
).on 'error', (e) ->
log e.message
else fn(new Error('Undefined Resource'))