Skip to content

Commit

Permalink
Merge pull request #3 from h2non/master
Browse files Browse the repository at this point in the history
Thanks for the patch! This managed to slip through somehow with the recent features.
  • Loading branch information
mrak committed Aug 30, 2013
2 parents ecfd7ae + b802119 commit 1a0c4f5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/portals/admin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ path = require 'path'
status = new ns.Server (path.resolve __dirname, '../../webroot')

module.exports.Admin = class Admin extends Portal

constructor : (endpoints) ->
@endpoints = endpoints
@contract = contract
Expand All @@ -14,7 +15,7 @@ module.exports.Admin = class Admin extends Portal
urlPattern : /^\/([1-9][0-9]*)?$/

goPong: (response) ->
response.writeHead 200, {'Content-Type' : 'text/plain'}
@writeHead response, 200, {'Content-Type' : 'text/plain'}
response.end 'pong'

goPUT : (request, response) ->
Expand Down Expand Up @@ -92,38 +93,38 @@ module.exports.Admin = class Admin extends Portal
@endpoints.create data, callback

ok : (response, result) ->
response.writeHead 200, {'Content-Type' : 'application/json'}
@writeHead response, 200, {'Content-Type' : 'application/json'}
if result?
response.end(JSON.stringify result)
else
response.end()

created : (response, request, id) ->
response.writeHead 201, {'Location' : "#{request.headers.host}/#{id}"}
@writeHead response, 201, {'Location' : "#{request.headers.host}/#{id}"}
response.end()

noContent : (response) ->
response.statusCode = 204
response.end()

badRequest : (response, errors) ->
response.writeHead 400, {'Content-Type' : 'application/json'}
@writeHead response, 400, {'Content-Type' : 'application/json'}
response.end JSON.stringify errors

notSupported : (response) ->
response.statusCode = 405
response.end()

notFound : (response) ->
response.writeHead 404, {'Content-Type' : 'text/plain'}
@writeHead response, 404, {'Content-Type' : 'text/plain'}
response.end()

saveError : (response) ->
response.writeHead 422, {'Content-Type' : 'text/plain'}
@writeHead response, 422, {'Content-Type' : 'text/plain'}
response.end()

serverError : (response) ->
response.writeHead 500, {'Content-Type' : 'text/plain'}
@writeHead response, 500, {'Content-Type' : 'text/plain'}
response.end()

urlValid : (url) ->
Expand Down
4 changes: 4 additions & 0 deletions src/portals/portal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports.Portal = class Portal
constructor: ->
@name = 'portal'

writeHead: (response, status_code, headers) ->
response.writeHead status_code, headers if !response.headersSent
return response

received: (request, response) ->
date = new Date()
hours = "0#{date.getHours()}".slice -2
Expand Down
4 changes: 2 additions & 2 deletions src/portals/stubs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ module.exports.Stubs = class Stubs extends Portal

callback = (err, endpointResponse) =>
if err
response.writeHead 404, {}
@writeHead response, 404, {}
@responded 404, request.url, 'is not a registered endpoint'
else
response.writeHead endpointResponse.status, endpointResponse.headers
@writeHead response, endpointResponse.status, endpointResponse.headers
response.write endpointResponse.body

@responded endpointResponse.status, request.url
Expand Down

0 comments on commit 1a0c4f5

Please sign in to comment.