Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (40 sloc) 1.39 KB
# _ = require 'underscore'
valid_url = (url) ->
re = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i
url.match re
class exports.Router
constructor: (@maps = []) ->
register: (re, cb) ->
@maps.push
re: re
cb: cb
exports.handle = (router) -> (req, res) ->
if req.method is 'GET'
query = ((require 'url').parse req.url, yes).query
unless query.url? and valid_url query.url
res.writeHead 403, 'Content-Type': 'text/plain'
res.end 'url not specified'
else
url = query.url
for s in router.maps
if url.match s.re
s.cb url, (err, data) ->
if query.method? and query.method is 'jsonp'
if err? or not data?
res.writeHead 500, 'Content-Type': 'application/javascript'
res.end 'var rainbow_data = {"error": "Error processing url","detail":"' + err + '"};'
else
res.writeHead 200, 'Content-Type': 'application/javascript'
res.end "var rainbow_data = #{JSON.stringify data};"
# TODO: Add more methods (xml, html)
else
if err? or not data?
res.writeHead 500, 'Content-Type': 'application/json'
res.end '{"error": "Error processing url","detail":"' + err + '"}'
else
res.writeHead 200, 'Content-Type': 'application/json;charset=UTF-8'
res.end JSON.stringify data
break
else
res.writeHead 403, 'Content-Type': 'text/plain'
res.end 'GET Only'
Jump to Line
Something went wrong with that request. Please try again.