From da62a6ce3c1bafb16b1de00ea6332c5e3b8ede8d Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Thu, 14 Nov 2013 21:29:33 -0500 Subject: [PATCH 1/4] Pretty print JSON data See travis-ci/travis-ci#1555. JSON data are pretty-formatted for humans. --- lib/travis/api/app/helpers/respond_with.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/travis/api/app/helpers/respond_with.rb b/lib/travis/api/app/helpers/respond_with.rb index 611f789f..18df03ad 100644 --- a/lib/travis/api/app/helpers/respond_with.rb +++ b/lib/travis/api/app/helpers/respond_with.rb @@ -10,7 +10,7 @@ class Travis::Api::App def respond_with(resource, options = {}) result = respond(resource, options) - result = result.to_json if result && response.content_type =~ /application\/json/ + result = JSON.pretty_generate(result) if result && response.content_type =~ /application\/json/ halt result || 404 end From 0e406b3ed1b1b2c43588b436410d1ddac6466651 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Fri, 15 Nov 2013 09:19:25 -0500 Subject: [PATCH 2/4] Accept `pretty` parameter for pretty formatting If the parameter is equal to `true` (in any case--`TRUE`, `True`) or a positive integer, return pretty formatted JSON data. No tests are necessary. --- lib/travis/api/app/helpers/respond_with.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/travis/api/app/helpers/respond_with.rb b/lib/travis/api/app/helpers/respond_with.rb index 18df03ad..66751047 100644 --- a/lib/travis/api/app/helpers/respond_with.rb +++ b/lib/travis/api/app/helpers/respond_with.rb @@ -10,7 +10,13 @@ class Travis::Api::App def respond_with(resource, options = {}) result = respond(resource, options) - result = JSON.pretty_generate(result) if result && response.content_type =~ /application\/json/ + if result && response.content_type =~ /application\/json/ + if params[:pretty].downcase == 'true' || params[:pretty].to_i > 0 + JSON.pretty_generate(result) + else + result.to_json + end + end halt result || 404 end From 4556fdf8e117fae95734d51cab7d9049e2d9f756 Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Fri, 15 Nov 2013 09:43:07 -0500 Subject: [PATCH 3/4] Account for absence of `pretty` parameter --- lib/travis/api/app/helpers/respond_with.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/travis/api/app/helpers/respond_with.rb b/lib/travis/api/app/helpers/respond_with.rb index 66751047..0fb9c751 100644 --- a/lib/travis/api/app/helpers/respond_with.rb +++ b/lib/travis/api/app/helpers/respond_with.rb @@ -11,10 +11,10 @@ class Travis::Api::App def respond_with(resource, options = {}) result = respond(resource, options) if result && response.content_type =~ /application\/json/ - if params[:pretty].downcase == 'true' || params[:pretty].to_i > 0 - JSON.pretty_generate(result) + if !params[:pretty].nil? && (params[:pretty].downcase == 'true' || params[:pretty].to_i > 0) + result = JSON.pretty_generate(result) else - result.to_json + result = result.to_json end end halt result || 404 From 85aebf684bf958a3bbe50b5d994210f53fe318ac Mon Sep 17 00:00:00 2001 From: Hiro Asari Date: Mon, 18 Nov 2013 13:12:45 -0500 Subject: [PATCH 4/4] Add specs for pretty print JSON They only check that the response includes `\n`, which should not happen otherwise. --- spec/unit/endpoint/builds_spec.rb | 39 ++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/spec/unit/endpoint/builds_spec.rb b/spec/unit/endpoint/builds_spec.rb index 51e06eb7..1c7654e8 100644 --- a/spec/unit/endpoint/builds_spec.rb +++ b/spec/unit/endpoint/builds_spec.rb @@ -1,5 +1,42 @@ require 'spec_helper' describe Travis::Api::App::Endpoint::Builds do - it 'has to be tested' + include Travis::Testing::Stubs + + it 'works with default options' do + get('/repos.json', {}).should be_ok + end + + context '/repos.json is requested' do + before :each do + @plain_response_body = get('/repos.json').body + end + + context 'when `pretty=true` is given' do + it 'prints pretty formatted data' do + response = get('/repos.json?pretty=true') + response.should be_ok + response.body.should_not eq(@plain_response_body) + response.body.should match(/\n/) + end + end + + context 'when `pretty=1` is given' do + it 'prints pretty formatted data' do + response = get('/repos.json?pretty=1') + response.should be_ok + response.body.should_not eq(@plain_response_body) + response.body.should match(/\n/) + end + end + + context 'when `pretty=bogus` is given' do + it 'prints plain-formatted data' do + response = get('/repos.json?pretty=bogus') + response.should be_ok + response.body.should eq(@plain_response_body) + end + end + end + end