Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding pretty JSON #262

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dependencies": {
"advisable": "~0.2.0",
"async": "^0.9.x",
"blueprint-transactions": "^0.0.2",
"chai": "^2.1.0",
"clone": "^1.0.0",
"coffee-script": "^1.9.1",
Expand All @@ -34,13 +35,13 @@
"node-uuid": "~1.4.2",
"optimist": "~0.6.1",
"pitboss-ng": "^0.3.1",
"prettyjson": "^1.1.3",
"proxyquire": "~1.4.x",
"request": "^2.53.0",
"setimmediate": "^1.0.2",
"spawn-sync": "^1.0.11",
"uri-template": "~1.0.0",
"winston": "^1.0.0",
"blueprint-transactions": "^0.0.2"
"winston": "^1.0.0"
},
"devDependencies": {
"body-parser": "^1.12.0",
Expand Down
22 changes: 14 additions & 8 deletions src/prettify-response.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
html = require 'html'
prettyjson = require 'prettyjson'

logger = require './logger'

Expand All @@ -12,24 +13,29 @@ prettifyResponse = (response) ->
logger.debug "Could not stringify: " + obj
obj

prettifySchema = (schema, contentType) ->
if typeof contentType != 'undefined' && contentType.indexOf('application/json') > -1
schema = prettyjson.render JSON.parse(schema)
else
schema = stringify schema
schema

prettifyBody = (body, contentType) ->
switch contentType
when 'application/json'
body = stringify body
when 'text/html'
body = html.prettyPrint body, {indent_size: 2}
if typeof contentType != 'undefined' && contentType.indexOf('application/json') > -1
body = prettyjson.render JSON.parse(body)
else
body = html.prettyPrint body, {indent_size: 2}
body


contentType = (response?.headers['content-type'] || response?.headers['Content-Type']) if response?.headers?

stringRepresentation = ""

for own key, value of response
if key is 'body'
value = '\n' + prettifyBody value, contentType
else if key is 'schema'
value = '\n' + stringify value
else if key is 'schema' || key is 'bodySchema'
value = '\n' + prettifySchema value, contentType
else if key is 'headers'
header = '\n'
for own hkey, hval of value
Expand Down