Skip to content

JSON:API parser for Ruby on Rails

License

Notifications You must be signed in to change notification settings

ekampp/json-pie

Repository files navigation

JSON::Pie

Parse JSON:API data structures into Rails ActiveRecord structures.

Tests Linting

Installation

gem "json-pie"

Usage

Your models:

class User < ActiveRecord::Base
  has_many :articles
end

class Article < ActiveRecord::Base
  belongs_to :user
end

In your controller:

class ArticleController
  def create
    article = JSON::Pie.parse params
    article.save!
    render json: article, status: :created
  end
end

Then send this JSON structure to your application will create a new article with #<User @id=1 ...> as the author.

{
  "data": {
    "type": "article",
    "attributes": {
      "title": "New article"
    },
    "relationships": {
      "user": {
        "data": {
          "type": "user",
          "id": 1
        }
      }
    }
  }
}

Decoupling from your models

It's fairly easy to decouple the publis JSON:API that you parse from the actual data structure beneath by using the options.

JSON::Pie.parse(params, **options)
option description
type_map A hash that maps JSON:API types to the actual models in your system. E.g. { author: :user }

About

JSON:API parser for Ruby on Rails

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages