### Pagination Example Request Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Example API request to fetch a specific page of results with a defined page size. ```bash # Get page 2 with 20 items per page GET /users?page[number]=2&page[size]=20 ``` -------------------------------- ### Install JSONAPI.rb Gem Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Instructions to add the JSONAPI.rb gem to your Rails application's Gemfile and install it. ```ruby gem "jsonapi.rb" ``` -------------------------------- ### User Serializer Example Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md An example of a serializer for the User model using ActiveModel::Serializer, defining attributes and relationships. ```ruby class UserSerializer < ActiveModel::Serializer attributes :id, :first_name, :last_name, :email, :created_at, :updated_at has_many :posts end ``` -------------------------------- ### Development Setup and Testing Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Provides commands for setting up the development environment, running tests, and checking code style using Bundler, RSpec, and RuboCop. ```bash bundle install bundle exec rspec # Run tests bundle exec rubocop # Check code style ``` -------------------------------- ### Include Relationships Example Request Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Example API request to include related 'posts' data in the response. ```bash # Include related posts GET /users?include=posts ``` -------------------------------- ### Basic Controller Usage Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Example of a Rails controller demonstrating basic JSONAPI.rb integration for filtering, pagination, and rendering. ```ruby class UsersController < ApplicationController include JSONAPI::Filtering include JSONAPI::Pagination def index allowed_fields = [ :first_name, :last_name, :created_at ] jsonapi_filter(User.all, allowed_fields) do |filtered| jsonapi_paginate(filtered.result) do |paginated| render jsonapi_paginate: paginated end end end def show user = User.find(params[:id]) render jsonapi: user end end ``` -------------------------------- ### Filtering and Sorting Examples Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Examples of API requests demonstrating filtering by name, sorting by multiple fields, and complex filtering with relationships. ```bash # Filter by first name containing "John" GET /users?filter[first_name_cont]=John # Sort by last name descending, then first name ascending GET /users?sort=-last_name,first_name # Complex filtering with relationships GET /users?filter[posts_title_matches_any]=Ruby,Rails&sort=-created_at ``` -------------------------------- ### Sorting by Post Count Example Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Example API request to sort users based on their post count in descending order. ```bash # Sort by post count GET /users?sort=-posts_count_sum ``` -------------------------------- ### Sorting with Expressions Example Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Shows how to enable and use sorting with aggregation expressions for advanced sorting criteria like post counts. ```ruby def index allowed_fields = [ :first_name, :posts_count ] options = { sort_with_expressions: true } jsonapi_filter(User.joins(:posts), allowed_fields, options) do |filtered| render jsonapi: filtered.result.group("users.id") end end ``` -------------------------------- ### Enable Rails Integration Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Code to initialize JSONAPI.rb in a Rails application by registering the JSON:API media type and renderers. ```ruby # config/initializers/jsonapi.rb require "jsonapi" JSONAPI::RailsApp.install! ``` -------------------------------- ### JSONAPI::Pagination Module Usage Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Demonstrates how to use the JSONAPI::Pagination module for implementing JSON:API compliant pagination in controllers. ```ruby class UsersController < ApplicationController include JSONAPI::Pagination def index jsonapi_paginate(User.all) do |paginated| render jsonapi_paginate: paginated end end private def jsonapi_meta(resources) { pagination: jsonapi_pagination_meta(resources), total: resources.respond_to?(:count) ? resources.count : resources.size } end end ``` -------------------------------- ### JSONAPI::Fetching Module Usage Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Shows how to use the JSONAPI::Fetching module to support relationship inclusion in API responses. ```ruby class UsersController < ApplicationController include JSONAPI::Fetching def index render jsonapi: User.all end private def jsonapi_include # Whitelist allowed includes super & [ "posts", "profile" ] end end ``` -------------------------------- ### JSONAPI::Filtering Module Usage Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Demonstrates how to use the JSONAPI::Filtering module in a controller for filtering and sorting API resources. ```ruby class UsersController < ApplicationController include JSONAPI::Filtering def index allowed_fields = [ :first_name, :last_name, :email, :posts_title ] jsonapi_filter(User.all, allowed_fields) do |filtered| render jsonapi: filtered.result end end end ``` -------------------------------- ### JSONAPI Serializer Parameters Configuration Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Illustrates how to pass custom parameters to JSONAPI serializers, such as the current user and flags for including private attributes. ```ruby def jsonapi_serializer_params { current_user: current_user, include_private: params[:include_private].present? } end ``` -------------------------------- ### JSONAPI Error Handling in Rails Controllers Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Demonstrates how to integrate JSONAPI::Errors into a Rails controller for handling user creation and updates, including custom error rendering. ```ruby class UsersController < ApplicationController include JSONAPI::Errors def create user = User.new(user_params) if user.save render jsonapi: user, status: :created else render jsonapi_errors: user.errors, status: :unprocessable_entity end end def update user = User.find(params[:id]) if user.update(user_params) render jsonapi: user else render jsonapi_errors: user.errors, status: :unprocessable_entity end end private def user_params params.require(:data).require(:attributes).permit(:first_name, :last_name, :email) end def render_jsonapi_internal_server_error(exception) # Custom exception handling (e.g., error tracking) # Sentry.capture_exception(exception) super(exception) end end ``` -------------------------------- ### Custom JSONAPI Page Size Configuration Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Defines a method to customize the page size for JSONAPI pagination, enforcing limits between 1 and 100. ```ruby def jsonapi_page_size(pagination_params) per_page = pagination_params[:size].to_i return 30 if per_page < 1 || per_page > 100 per_page end ``` -------------------------------- ### Custom Serializer Resolution in Rails Source: https://github.com/ismailakbudak/jsonapi/blob/main/README.md Shows how to specify a custom serializer for JSONAPI resources in a Rails controller and provides a fallback mechanism for serializer resolution. ```ruby class UsersController < ApplicationController def index render jsonapi: User.all, serializer_class: CustomUserSerializer end private def jsonapi_serializer_class(resource, is_collection) JSONAPI::RailsApp.serializer_class(resource, is_collection) rescue NameError # Fallback serializer logic "#{resource.class.name}Serializer".constantize end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.