轻量级、简单且可维护的 JSON:API 支持,适用于您的下一个 Ruby HTTP API。
So you say you need JSON:API support in your API...
- hey how did your hackathon go?
- not too bad, we got Babel set up
- yep…
- yep.
Here are some codes to help you build your next JSON:API compliable application easier and faster.
It's quite a hassle to setup a Ruby (Rails) web application to use and follow the JSON:API specifications.
The idea is simple, JSONAPI.rb offers a bunch of modules/mixins/glue, add them to your controllers, call some methods, profit!
Main goals:
The available features include:
fast_jsonapi)Mainly by leveraging JSON:API Serializer and Ransack.
Thanks to everyone who worked on these amazing projects!
I'm grateful for the following companies for supporting this project!
Add this line to your application's Gemfile:
gem 'jsonapi.rb'And then execute:
$ bundleOr install it yourself as:
$ gem install jsonapi.rbTo enable the support for Rails, add this to an initializer:
# config/initializers/jsonapi.rb
require 'jsonapi'
JSONAPI::Rails.install!This will register the mime type and the jsonapi and jsonapi_errors
renderers.
The jsonapi renderer will try to guess and resolve the serializer class based
on the object class, and if it is a collection, based on the first item in the
collection.
The naming scheme follows the ModuleName::ClassNameSerializer for an instance
of the ModuleName::ClassName.
Please follow the JSON:API Serializer guide on how to define a serializer.
To provide a different naming scheme implement the jsonapi_serializer_class
method in your resource or application controller.
Here's an example:
class CustomNamingController 30 || per_page < 1
per_page
endJSONAPI::Deserialization provides a helper to transform a JSONAPI document
into a flat dictionary that can be used to update an ActiveRecord::Base model.
Here's an example using the jsonapi_deserialize helper:
class MyController < ActionController::Base
include JSONAPI::Deserialization
def update
model = MyModel.find(params[:id])
if model.update(jsonapi_deserialize(params, only: [:attr1, :rel_one]))
render jsonapi: model
else
render jsonapi_errors: model.errors, status: :unprocessable_entity
end
end
endThe jsonapi_deserialize helper accepts the following options:
only: returns exclusively attributes/relationship data in the provided listexcept: returns exclusively attributes/relationship which are not in the listpolymorphic: will add and detect the _type attribute and class to the
defined list of polymorphic relationshipsThis functionality requires support for inflections. If your project uses
active_support or rails you don't need to do anything. Alternatively, we will
try to load a lightweight alternative to active_support/inflector provided
by the dry/inflector gem, please make sure it's added if you want to benefit
from this feature.
After checking out the repo, run bundle to install dependencies.
Then, run rake spec to run the tests.
To install this gem onto your local machine, run bundle exec rake install.
To release a new version, update the version number in version.rb, and then
run bundle exec rake release, which will create a git tag for the version,
push git commits and tags, and push the .gem file to
rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/stas/jsonapi.rb
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
暂无开放 Issues,或尚未同步最近议题。