This commit is contained in:
Daniel Saewitz
2013-02-21 14:18:53 -05:00
parent b92145f5a3
commit ddff29ccf0
11 changed files with 234 additions and 72 deletions
+25
View File
@@ -0,0 +1,25 @@
Request = require './request'
qs = require 'querystring'
makeUrl = (path, params) ->
if typeof params is "object"
params = qs.stringify(params)
else
params = ''
return "http://localhost:3000/#{path}?#{params}"
# url, params (optional), callback
get = (obj, callback) ->
obj.params ||= {}
obj.path ||= ''
requestUrl = makeUrl obj.path, obj.params
request = new Request requestUrl
request.done (res) ->
callback null, JSON.parse(res)
request.fire()
module.exports = { get }
+74
View File
@@ -0,0 +1,74 @@
http = require 'http'
# ### Request
#
# The Request class is just a wrapper
# around the `http.request` function,
# providing a cleaner and more reusable
# interface.
#
# `constructor(options)`
#
# `options` can be anything `http.request` takes.
#
# Callbacks can be queued up by calling `request.done`
# and passing it a callback function to be invoked when the
# http request has completed
#
# Example:
#
# callback = (json) ->
# doSomethingWithJSON( json )
#
# request = new Request('http://api.phish.net/api.js?')
# request.done (response) ->
# callback JSON.parse(response)[0]
#
# request.fire(data) # data refers to the post data
#
class Request
constructor: (@options) ->
@callbacks = {}
fire: (data) ->
request = http.request(@options, @_handler)
request.on 'error', (err) =>
for callback in @callbacks['fail']
callback(err)
# Send data with request
if data
request.write data
request.end()
this
done: (callback) ->
push.call(this, 'done', callback)
this
fail: (callback) ->
push.call(this, 'fail', callback)
this
###########
# PRIVATE #
###########
_handler: (response) =>
data = ''
response.on 'data', (chunk) ->
data += chunk
response.on 'end', =>
for callback in @callbacks['done']
callback(data)
push = (type, callback) ->
@callbacks[type] ?= []
@callbacks[type].push callback
module.exports = Request