{"id":27068268,"url":"https://github.com/oreshinya/normalizr_ruby","last_synced_at":"2025-10-03T19:17:00.635Z","repository":{"id":56885738,"uuid":"66458994","full_name":"oreshinya/normalizr_ruby","owner":"oreshinya","description":"Normalizr format JSON generator for API server","archived":false,"fork":false,"pushed_at":"2017-03-01T06:41:05.000Z","size":22,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-15T12:15:45.557Z","etag":null,"topics":["api","json"],"latest_commit_sha":null,"homepage":"","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/oreshinya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-24T11:38:12.000Z","updated_at":"2020-03-31T23:41:33.000Z","dependencies_parsed_at":"2022-08-21T00:20:43.057Z","dependency_job_id":null,"html_url":"https://github.com/oreshinya/normalizr_ruby","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreshinya%2Fnormalizr_ruby","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreshinya%2Fnormalizr_ruby/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreshinya%2Fnormalizr_ruby/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oreshinya%2Fnormalizr_ruby/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oreshinya","download_url":"https://codeload.github.com/oreshinya/normalizr_ruby/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247399895,"owners_count":20932876,"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":["api","json"],"created_at":"2025-04-05T20:36:50.693Z","updated_at":"2025-10-03T19:16:55.595Z","avatar_url":"https://github.com/oreshinya.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NormalizrRuby\n[![Gem Version](https://badge.fury.io/rb/normalizr_ruby.svg)](https://badge.fury.io/rb/normalizr_ruby)\n[![Build Status](https://travis-ci.org/oreshinya/normalizr_ruby.svg?branch=master)](https://travis-ci.org/oreshinya/normalizr_ruby)\n\n[Normalizr](https://github.com/paularmstrong/normalizr) format JSON generator for API server.\n\n## Installation\n\n```ruby\ngem \"normalizr_ruby\"\n```\n\n## Getting Started\n\nInclude `NormalizrRuby::Normalizable` in your `ApplicationController`:\n\n```ruby\nclass ApplicationController \u003c ActionController::API\n  include NormalizrRuby::Normalizable\nend\n```\n\nCreate a new normalizr's schema:\n\n```\nbundle exec rails g normalizr_ruby:schema user\n```\n\nThis will generate a schema in `app/schemas/user_schema.rb` for your `User` model:\n\n```ruby\nclass UserSchema \u003c NormalizrRuby::Schema\n  attribute :id\nend\n```\n\nYou can customize it like this:\n\n```ruby\nclass UserSchema \u003c NormalizrRuby::Schema\n  attribute :id\n  attribute :first_name\n  attribute :last_name\n  attribute :full_name, if: :has_last_name?\n  association :team\n  association :comments, schema: CustomCommentSchema, if: :has_last_name?\n\n  def full_name\n    \"#{object.first_name} #{object.last_name}\"\n  end\n\n  def has_last_name?\n    object.last_name.present?\n  end\nend\n```\n\nRender json like this:\n\n```ruby\nclass UsersController \u003c Applicationcontroller\n  def index\n    users = User.all\n    render json: users\n  end\nend\n```\n\nYou will get response like this:\n\n```json\n{\n  \"result\": [1, 2],\n  \"entities\": {\n    \"teams\": {\n      \"1\": { \"id\": 1, \"name\": \"Team A\" }\n    },\n    \"users\": {\n      \"1\": { \"id\": 1, \"firstName\": \"Michael\", \"lastName\": \"Jackson\", \"fullName\": \"Michael Jackson\", \"team\": 1, \"comments\": [2, 4] },\n      \"2\": { \"id\": 2, \"firstName\": \"Tom\", \"lastName\": null, \"team\": 1 }\n    },\n    \"comments\": {\n      \"2\": { \"id\": 2, \"body\": \"Hello :)\" },\n      \"4\": { \"id\": 4, \"body\": \"World ;)\" }\n    }\n  }\n}\n```\n\n## Documents\n\n### Schema\n#### `::attribute(key, options)`\nAdd an attribute to normalized entity.\n`if` option can make an attribute conditional, and it takes a symbol of a method name on the schema.\n\n#### `::association(key, options)`\nAdd an association to normalized entity, and add associated entity to entities.\n`if` option can make an association conditional, and it takes a symbol of a method name on the schema.\n`schema` option can make specifying a schema.\n\n#### `#object`\nYou can refer unnormalized resource.\n\n#### `#props`\nYou can refer arbitray options.\n\nFor example:\n\n```ruby\nclass UserSchema \u003c NormalizrRuby::Schema\n  attribute :id\n  attribute :hoge\n\n  def hoge\n    props[:arbitray_option]\n  end\nend\n\ndef UsersController \u003c Applicationcontroller\n  def show\n    user = User.find(params[:id])\n    render json: user, normalizr: { arbitray_option: 1 }\n  end\nend\n```\n\n#### `#context`\nYou can refer any values in all schemas\n\nFor example:\n\n```ruby\nclass UserSchema \u003c NormalizrRuby::Schema\n  attribute :id\n  attribute :is_current_user\n\n  def is_current_user \n    context[:current_user].id == object.id\n  end\nend\n```\n\n### Render options\n\nYou can pass `normalizr`, and can pass any options as `props`.\nBut `schema` option can make specifying a schema.\n\n```ruby\nrender json: user, normalizr: { schema: CustomUserSchema, hoge: 1, fuga: 2 }\n```\n\n### How to use context\n\nYou have to implement `normalizr_context` in any controller.\n\n```ruby\nclass ApplicationController \u003c ActionController::API\n  include NormalizrRuby::Normalizable\n\n  def normalizr_context\n    { current_user: User.first }\n  end\nend\n```\n\n### Configuration\n\nYou can specify key transform in your `config/initializers/normalizr_ruby.rb`.\n\n```ruby\nNormalizrRuby.config.key_transform = :camel_lower # default\n```\n\n| Option | Result |\n|----|----|\n| `:camel` | ExampleKey |\n| `:camel_lower` | exampleKey |\n| `:dash` | example-key |\n| `:unaltered` | the original, unaltered key |\n| `:underscore` | example_key |\n| `nil` | use the default |\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foreshinya%2Fnormalizr_ruby","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foreshinya%2Fnormalizr_ruby","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foreshinya%2Fnormalizr_ruby/lists"}