All Projects → visualitypl → Jsonapi_parameters

visualitypl / Jsonapi_parameters

Licence: mit
Rails-way to consume JSON:API input

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Jsonapi parameters

Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (+282%)
Mutual labels:  api, json-api, jsonapi, json, rails
Jsonapi Rails
Rails gem for fast jsonapi-compliant APIs.
Stars: ✭ 242 (+384%)
Mutual labels:  api, json-api, jsonapi, json, rails
Jsonapi Rb
Efficiently produce and consume JSON API documents.
Stars: ✭ 219 (+338%)
Mutual labels:  api, json-api, jsonapi, json
Laravel5 Jsonapi
Laravel 5 JSON API Transformer Package
Stars: ✭ 313 (+526%)
Mutual labels:  api, json-api, jsonapi, json
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (+132%)
Mutual labels:  api, json-api, jsonapi, rails
Datoji
A tiny JSON storage service. Create, Read, Update, Delete and Search JSON data.
Stars: ✭ 222 (+344%)
Mutual labels:  api, json-api, json, rails
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (+128%)
Mutual labels:  api, json-api, jsonapi, json
Json Api Dart
JSON:API client for Dart/Flutter
Stars: ✭ 53 (+6%)
Mutual labels:  api, json-api, jsonapi, json
Graphiti
Stylish Graph APIs
Stars: ✭ 783 (+1466%)
Mutual labels:  api, json-api, rails, ruby-on-rails
Sabisu Rails
Simple and powerful engine for exploring your Rails api application
Stars: ✭ 129 (+158%)
Mutual labels:  api, json, rails
Kitsu Server
🚂 Rails API server for Kitsu
Stars: ✭ 145 (+190%)
Mutual labels:  api, json-api, rails
Fake api
The fastest way to prototype API in your Rails application
Stars: ✭ 119 (+138%)
Mutual labels:  api, rails, ruby-on-rails
Rki Covid Api
🦠🇩🇪📈 An API for the spread of covid-19 in Germany. Data from Robert-Koch-Institut.
Stars: ✭ 98 (+96%)
Mutual labels:  api, json-api, json
Api guard
JWT authentication solution for Rails APIs
Stars: ✭ 159 (+218%)
Mutual labels:  api, rails, ruby-on-rails
Flexirest
Flexirest - The really flexible REST API client for Ruby
Stars: ✭ 188 (+276%)
Mutual labels:  api, json, rails
Json Api Php
JSON-API (http://jsonapi.org) responses in PHP.
Stars: ✭ 426 (+752%)
Mutual labels:  api, json-api, json
Polr
🚡 A modern, powerful, and robust URL shortener
Stars: ✭ 4,147 (+8194%)
Mutual labels:  api, json-api, json
Element Api
Create a JSON API/Feed for your elements in Craft.
Stars: ✭ 493 (+886%)
Mutual labels:  api, json-api, json
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (+34%)
Mutual labels:  api, json-api, json
Pager Api
Easy API pagination for Rails
Stars: ✭ 86 (+72%)
Mutual labels:  api, json, rails

JsonApi::Parameters

Simple JSON:API compliant parameters translator.

Gem Version Maintainability Test Coverage CircleCI

Documentation

Usage

Installation

Add this line to your application's Gemfile:

gem 'jsonapi_parameters'

And then execute:

$ bundle

Or install it yourself as:

$ gem install jsonapi_parameters

Rails

Usually your strong parameters in controller are invoked this way:

def create
  model = Model.new(create_params)
  
  if model.save
    ...
  else
    head 500
  end
end

private

def create_params
  params.require(:model).permit(:name)
end

With jsonapi_parameters, the difference is just the params:

def create_params
  params.from_jsonapi.require(:model).permit(:name)
end

Relationships

JsonApi::Parameters supports ActiveRecord relationship parameters, including nested attributes.

Relationship parameters are being read from two optional trees:

  • relationships,
  • included

If you provide any related resources in the relationships table, this gem will also look for corresponding, included resources and their attributes. Thanks to that this gem supports nested attributes, and will try to translate these included resources and pass them along.

For more examples take a look at Relationships in the wiki documentation.

Plain Ruby / outside Rails

params = { # JSON:API compliant parameters here
	# ...
}

class Translator
  include JsonApi::Parameters
end
translator = Translator.new

translator.jsonapify(params)

Mime Type

As stated in the JSON:API specification correct mime type for JSON:API input should be application/vnd.api+json.

This gem's intention is to make input consumption as easy as possible. Hence, it registers this mime type for you.

Stack limit

In theory, any payload may consist of infinite amount of relationships (and so each relationship may have its own, included, infinite amount of nested relationships). Because of that, it is a potential vector of attack.

For this reason we have introduced a default limit of stack levels that JsonApi::Parameters will go down through while parsing the payloads.

This default limit is 3, and can be overwritten by specifying the custom limit.

Ruby

class Translator
    include JsonApi::Parameters
end

translator = Translator.new

translator.jsonapify(custom_stack_limit: 4)

# OR
 
translator.stack_limit = 4
translator.jsonapify.(...)

Rails

def create_params
    params.from_jsonapi(custom_stack_limit: 4).require(:user).permit(
        entities_attributes: { subentities_attributes: { ... } }
    )
end

# OR
 
def create_params
    params.stack_level = 4

    params.from_jsonapi.require(:user).permit(entities_attributes: { subentities_attributes: { ... } })
ensure
    params.reset_stack_limit!
end

Customization

If you need custom relationship handling (for instance, if you have a relationship named scissors that is plural, but it actually is a single entity), you can use Handlers to define appropriate behaviour.

Read more at Relationship Handlers.

Team

Project started by Jasiek Matusz.

Currently, jsonapi_parameters is maintained by Visuality's Open Source Commitee.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].