All Projects → patrixr → jsonapi-serializer-formats

patrixr / jsonapi-serializer-formats

Licence: MIT license
💎 Gem to enrich jsonapi-serializer with multiple formats

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to jsonapi-serializer-formats

grape-jwt-authentication
A reusable Grape JWT authentication concern
Stars: ✭ 31 (+55%)
Mutual labels:  ruby-gem, gem, concern
Devise masquerade
Extension for devise, enable login as functionality. Add link to the masquerade_path(resource) and use it.
Stars: ✭ 380 (+1800%)
Mutual labels:  ruby-gem, gem
Stitches
Create a Microservice in Rails with minimal ceremony
Stars: ✭ 371 (+1755%)
Mutual labels:  ruby-gem, gem
Api Fuzzer
API Fuzzer which allows to fuzz request attributes using common pentesting techniques and lists vulnerabilities
Stars: ✭ 238 (+1090%)
Mutual labels:  ruby-gem, gem
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (+40%)
Mutual labels:  ruby-gem, gem
rspec n
A ruby gem that runs RSpec N times.
Stars: ✭ 37 (+85%)
Mutual labels:  ruby-gem, gem
Unimidi
Realtime MIDI IO for Ruby
Stars: ✭ 229 (+1045%)
Mutual labels:  ruby-gem, gem
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (+470%)
Mutual labels:  serializer, jsonapi
churnalizer
Analyze your Ruby app for Churn vs Complexity
Stars: ✭ 17 (-15%)
Mutual labels:  ruby-gem, gem
pixitar
🧝 Pixitar is an avatar generation library written in Ruby.
Stars: ✭ 20 (+0%)
Mutual labels:  ruby-gem, gem
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (+5%)
Mutual labels:  ruby-gem, gem
make model searchable
Adds simlpe search functionality to models
Stars: ✭ 27 (+35%)
Mutual labels:  ruby-gem, gem
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (+855%)
Mutual labels:  serializer, jsonapi
log-symbols
A ruby 💎gem💎 for generating log symbols
Stars: ✭ 14 (-30%)
Mutual labels:  ruby-gem, gem
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (+480%)
Mutual labels:  serializer, jsonapi
Motion
Reactive frontend UI components for Rails in pure Ruby
Stars: ✭ 498 (+2390%)
Mutual labels:  ruby-gem, gem
json-api-serializer
Node.js/browser framework agnostic JSON API (http://jsonapi.org/) serializer.
Stars: ✭ 141 (+605%)
Mutual labels:  serializer, jsonapi
laravel5-jsonapi-dingo
Laravel5 JSONAPI and Dingo together to build APIs fast
Stars: ✭ 29 (+45%)
Mutual labels:  serializer, jsonapi
memo wise
The wise choice for Ruby memoization
Stars: ✭ 486 (+2330%)
Mutual labels:  ruby-gem, gem
syobocal
Simle gem for Syboi Calendar
Stars: ✭ 13 (-35%)
Mutual labels:  ruby-gem, gem

JSON:API Serializer format module

A module to enrich JSON:API Serializers with configurable formats.

Version License RSpec

Installation

Add this line to your Gemfile:

gem 'jsonapi-serializer-formats'

Execute:

$ bundle install

Serializer extension

After the module is included in your existing serializer, the format keyword is now available for you to use

class MovieSerializer
  include JSONAPI::Serializer
  include JSONAPI::Formats

  attributes :name

  format :detailed do
    attribute   :year
    attribute   :rating

    has_many :actors
  end
end

Rendering

Default behaviour

When no filter is specified, only the attributes declared at the root will be rendered.

hash = MovieSerializer.new(movie).serializable_hash

Output

{
  "data": {
    "id": "3",
    "type": "movie",
    "attributes": {
      "name": "pulp fiction"
    }
  }
}

Specifying a format

When a format is specified during render, the attributes defined under that format will be included

hash = MovieSerializer.new(movie, { params: { format: :detailed }}).serializable_hash

Output

{
  "data": {
    "id": "3",
    "type": "movie",
    "attributes": {
      "name": "pulp fiction",
      "year": "1994",
      "rating": 5
    },
    "relationships": {
      "actors": {
        "data": [...]
      }
    }
  }
}

Nested formats

You can nest formats one into another. A nested format will only render if all of its parent formats have been enabled during rendering

class MovieSerializer
  include JSONAPI::Serializer
  include JSONAPI::Formats

  attributes :name

  format :detailed do
    attribute   :year

    format :admin do
      attribute :view_count
    end
  end
end
hash = MovieSerializer.new(movie, { params: { format: [:detailed, :admin] } }).serializable_hash

Renders

{
  "data": {
    "id": "3",
    "type": "movie",
    "attributes": {
      "name": "pulp fiction",
      "year": "1994",
      "view_count": 98776
    }
  }
}

LICENSE

MIT License

Copyright (c) 2021 Patrick R

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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].