Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

abc_jsonapi

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abc_jsonapi


Version published
Maintainers
1
Created
Source

AbcJsonapi

Minimalistic gem for JSON Serialization according to https://jsonapi.org spec.

Inspired by https://github.com/Netflix/fast_jsonapi

Contributions are welcome.

Installation

Add this line to your application's Gemfile:


gem 'abc_jsonapi'

And then execute:

$ bundle

Or install it yourself as:

$ gem install abc_jsonapi

Features

  • Relationships (belongs_to, has_many, has_one)
  • Compound documents ("include" option)
  • Custom include strategies. It is useful when serializing a collection of records to avoid N+1 problem.
  • "Virtual" attributes or overriding default attribute serializing behavior

Usage

Syntax is very similar to Active Model Serializer.

The serializer is able to work with any ruby ​​objects. Not only ActiveRecord. It calls objects methods to get all the data.

Model Example
class Author
  attr_reader :first_name, :last_name, :public_name, :contact_id
end
Serializer Definition
class AuthorSerializer
  include AbcJsonapi::Serializer
  resource_type :people (optional)
  attributes :first_name, :last_name, :public_name
  belongs_to :contact
  has_many :books
end
Custom resource type

Default resource type of direct serializing model is taken from serializer filename. In case of AuthorSerializer with disabled pluralize_resources it will be "author". Resource type of compound documents is downcased class of related object.

Attributes

Jsonapi attributes may be declared with class method of serializer - attributes:

attributes :first_attribute, :second, *other attributes*

attributes arguments will be called on serializing model as methods.

Also there is attribute method to declare single property. You can pass a block to define the way it should be returned.

attribute :date_of_birth  do |object|
  object.date_of_birth.strftime("%FT%T.%3N%:z") if object.date_of_birth.present?
end
Serializer usage

Return ruby hash:

AuthorSerializer.new(resource).serializable_hash

Return json:

AuthorSerializer.new(resource).serialized_json

Compound Documents

To include relationships you can pass include option at the serializer initialization stage.

options = {}
options[:include] = :books # or "books"
AuthorSerializer.new(resource, options).serialized_json

Configuration

Global config options
  • transform_keys (true/false) - convert keys to any case or not. Default value: true
  • key_transform_method ("camel" or "snake") - certain key transform method to use with transform_keys option enabled. Default value: "camel"
  • pluralize_resources (true/false) - pluralize all resources types in response by default or not. Default value: false
Usage
AbcJsonapi.configure do |config|
  config.transform_keys = true
  config.key_transform_method = "camel"
  config.pluralize_resources  =  true
end

Development

TODO:

  • Write specs
  • Use custom include relationship strategies for downloading relationships json block (solve N+1 problem when serializing collection).
  • Meta per resource
  • Add other jsonapi features

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ezname/abc_jsonapi.

License

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

FAQs

Package last updated on 22 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc