{"id":15802080,"url":"https://github.com/zhandao/out_put","last_synced_at":"2026-01-21T16:32:04.474Z","repository":{"id":56887119,"uuid":"152965062","full_name":"zhandao/out_put","owner":"zhandao","description":"Render JSON response in a unified format","archived":false,"fork":false,"pushed_at":"2019-04-23T17:19:16.000Z","size":15,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-25T09:01:31.996Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhandao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-14T11:04:19.000Z","updated_at":"2019-04-23T17:18:46.000Z","dependencies_parsed_at":"2022-08-21T00:20:29.058Z","dependency_job_id":null,"html_url":"https://github.com/zhandao/out_put","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fout_put","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fout_put/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fout_put/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhandao%2Fout_put/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhandao","download_url":"https://codeload.github.com/zhandao/out_put/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247631832,"owners_count":20970069,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T01:41:51.658Z","updated_at":"2026-01-21T16:32:04.435Z","avatar_url":"https://github.com/zhandao.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OutPut\n\n[![Gem Version](https://badge.fury.io/rb/out_put.svg)](https://badge.fury.io/rb/out_put)\n\nRender JSON response in a unified format\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'out_put'\n```\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\n### 1. Config (it's optional)\n\ninitializers: `out_put.rb`\n\n```ruby\nOutPut::Config.tap do |config|\n  config.project_code = 101000 # MUST padding three zero here\n  config.pagination_for = :list\nend\n```\n\n`project_code` (defaults to 0) + `code` will be the final code\n\n### 2. Basic\n\nAdd this line in your (base) controller:\n\n```ruby\ninclude OutPut\n```\n\nTo render a json response, call `output` method, it's very easy:\n\n```ruby\noutput 0, 'success'\n# the same as above:\noutput code: 0, msg: 'success'\n# will render by the default format:\n# {\n#   result: { code: 0, message: 'success' }\n# }\n\nok # =\u003e code: 0, message: 'success'\n```\n\n### 3. Response `data` filed\n\n```ruby\noutput 0, foo: 'bar', list: [ 1, 2, 3 ]\n# will render by the default format:\n# {\n#   result: { code: 0, message: '' }\n#     data: { foo: 'bar', list: [1,2,3] }\n# }\n\n# or\nok_with foo: 'bar'\n```\n\n### 4. Set HTTP status\n\n```ruby\noutput 0, 'success', http: 200\n```\n\n### 5. Error Response\n\nYou don't need to pass your project code like '101', after **config**:\n\n```ruby\nerror 700, 'the 7th api error 0' # =\u003e the final code will be 101700\n# or\nerror_with 700, 'msg', foo: 'bar'\n```\n\n`error` is an alias of `output`\n\n### 6. `output` any objects which have implemented serialization method `info`\n\n```ruby\nBusinessError.record_not_found.info # =\u003e { code: ..., msg: ... }\n\noutput BusinessError.record_not_found\n```\n\n[About `business_error`](https://github.com/zhandao/business_error/)\n\n### 7. Just render the given data without default format\n\n```ruby\noutput only: { foo: 'bar' }\n# will render: { foo: 'bar' }\n\n# response an array\noutput only: [ 1, 2, 3 ]\n```\n\n### 8. cache\n\n```ruby\noutput cache: 24.hours do\n  { list: User.all }\nend # will render: { list: [...] }\n\noutput foo: 'bar', cache: 24.hours do\n  { list: User.all }\nend # will render: { foo: 'bar', list: [...] }\n\noutput only: { foo: 'bar' }, cache: 24.hours do\n  { only: { list: User.all } }\nend # will ONLY render: { foo: 'bar', list: [...] }\n```\n\nNote: the cache key will be `\"output/#{action_name}\"`\n\n### X. Other\n\n#### X.a automatically set `total`:\n\nif `config.pagination_for = :list`:\n\n```ruby\noutput 0, list: [ 1, 2, 3 ]\n# will render:\n# {\n#   result: { code: 0, message: '' }\n#     data: { total: 3, list: [1,2,3] }\n# }\n```\n\n#### X.b use `build_with` to pass an variable `@view` to your view\n\n```ruby\nbuild_with key: 'val'\nbuild_with code=0, msg='success', key: 'val'\n\n# the in your .jbuilder\njson.result do\n  json.code @view[:code] || 0\n  json.msg  @view[:msg]  || 'success'\nend\njson.data do\n  json.merge! @view.data # =\u003e expect code and msg, for example { key: 'val' }\n  # or\n  json.key @view[:key]\nend\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhandao%2Fout_put","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhandao%2Fout_put","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhandao%2Fout_put/lists"}