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

Beautify API JSON during development #328

Merged
merged 4 commits into from
Oct 2, 2019

Conversation

ashmaroli
Copy link
Member

@ashmaroli ashmaroli commented Mar 18, 2017

Simplify reading API in development by outputting JSON tree view eliminating the need for browser
add-ons for Chrome and Firefox

Before

{ "collections_api": "http://localhost:4000/_api/collections",  "configuration_api": 
"http://localhost:4000/_api/configuration",  "data_api": "http://localhost:4000/_api/data",
 "pages_api": "http://localhost:4000/_api/pages",  "static_files_api": 
"http://localhost:4000/_api/static_files"}

After

{
  "collections_api": "http://localhost:4000/_api/collections",
  "configuration_api": "http://localhost:4000/_api/configuration",
  "data_api": "http://localhost:4000/_api/data",
  "pages_api": "http://localhost:4000/_api/pages",
  "static_files_api": "http://localhost:4000/_api/static_files"
}

@ashmaroli
Copy link
Member Author

Additional beautification with the neatjson gem if you don't mind additional dependency

{
  "collections_api"    :  "http://localhost:4000/_api/collections",
  "configuration_api"  :  "http://localhost:4000/_api/configuration",
  "data_api"           :  "http://localhost:4000/_api/data",
  "pages_api"          :  "http://localhost:4000/_api/pages",
  "static_files_api"   :  "http://localhost:4000/_api/static_files"
}

@benbalter
Copy link
Contributor

Nice. I believe you can accomplish this without monkeypatching by using the Sinatra:JSON API to specify the encoder: http://www.sinatrarb.com/contrib/json.html#label-Encoders

@ashmaroli
Copy link
Member Author

you can accomplish this without monkeypatching

I opted to monkeypatch instead, because with this, the switch is located at one place, rather than having to repeat the custom encoder at all end-points.

@mertkahyaoglu
Copy link
Member

Is this good to go?

@benbalter
Copy link
Contributor

the switch is located at one place, rather than having to repeat the custom encoder at all end-points.

Couldn't you include it once in `server.rb?

@ashmaroli
Copy link
Member Author

Couldn't you include it once in server.rb?

@benbalter Yes. Ultimately, the :json method gets patched when RACK_ENV == "development". I felt the approach in this PR leaves a cleaner footprint: server.rb wont have a native :json method defined in the release candidate version.

@mertkahyaoglu
Copy link
Member

I liked the idea 👍 But this might be a little bit overkill for a feature that will be available for only development. I think it is better to keep the api minimal (less files, less modules etc.). That might be the reason @benbalter suggested not to monkey-patch an extra module.

@ashmaroli
Copy link
Member Author

I restricted it to just development to keep the extra \n out of production API since only a handful of users might even look to read API output from the gem.

¯\_(ツ)_/¯

@ashmaroli ashmaroli merged commit 167b2bf into jekyll:master Oct 2, 2019
@ashmaroli ashmaroli deleted the beautify-api branch October 2, 2019 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants