Convert Figma logo to code with AI

cerebris logojsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.

2,317
535
2,317
245

Top Related Projects

Rails for API only applications

55,872

Ruby on Rails

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data

Quick Overview

JSONAPI Resources is a Ruby gem that provides a framework for building JSON API-compliant servers. It simplifies the process of creating and managing API endpoints, handling requests, and serializing responses according to the JSON API specification.

Pros

  • Adherence to JSON API Specification: The library closely follows the JSON API specification, ensuring consistent and standardized API responses.
  • Automatic Serialization: JSONAPI Resources automatically serializes and deserializes data, reducing boilerplate code.
  • Flexible Resource Definitions: Resources can be easily defined and customized to fit the specific needs of the application.
  • Efficient Querying and Filtering: The library provides powerful querying and filtering capabilities, allowing clients to retrieve only the data they need.

Cons

  • Steep Learning Curve: The library has a relatively steep learning curve, especially for developers new to the JSON API specification.
  • Limited Flexibility: While the library is flexible, it may not be suitable for highly complex or custom API requirements that deviate significantly from the JSON API standard.
  • Performance Concerns: Depending on the size and complexity of the API, the automatic serialization and deserialization features may impact performance.
  • Limited Community Support: Compared to some other Ruby web frameworks, the JSONAPI Resources community is relatively small, which may limit the availability of third-party plugins and resources.

Code Examples

Here are a few examples of how to use JSONAPI Resources:

  1. Defining a Resource:
class PostResource < JSONAPI::Resource
  attribute :title
  attribute :body
  has_many :comments
end
  1. Handling a Request:
class PostsController < JSONAPI::ResourceController
  def index
    posts = PostResource.all(params)
    render jsonapi: posts
  end

  def show
    post = PostResource.find(params[:id])
    render jsonapi: post
  end
end
  1. Customizing a Resource:
class PostResource < JSONAPI::Resource
  attribute :title
  attribute :body
  has_many :comments

  def self.updatable_fields(context)
    super - [:title]
  end
end
  1. Filtering and Sorting:
class PostsController < JSONAPI::ResourceController
  def index
    posts = PostResource.all(params)
    posts = posts.filter(params[:filter])
    posts = posts.sort_by(params[:sort])
    render jsonapi: posts
  end
end

Getting Started

To get started with JSONAPI Resources, follow these steps:

  1. Add the jsonapi-resources gem to your Gemfile:
gem 'jsonapi-resources'
  1. Run bundle install to install the gem.

  2. Generate a new resource:

rails generate jsonapi:resource post title body
  1. Define your resource relationships and any custom logic in the generated PostResource class.

  2. Set up your routes and controller actions to handle the API requests:

# config/routes.rb
Rails.application.routes.draw do
  jsonapi_resources :posts
end

# app/controllers/posts_controller.rb
class PostsController < JSONAPI::ResourceController
end
  1. Start your Rails server and begin interacting with your JSON API!

Competitor Comparisons

Rails for API only applications

Pros of rails-api/rails-api

  • Provides a lightweight and focused API-only Rails framework, which can be beneficial for building efficient and performant APIs.
  • Includes a built-in middleware stack optimized for API development, reducing the need for additional configuration.
  • Offers a simplified and streamlined codebase, making it easier to understand and maintain.

Cons of rails-api/rails-api

  • Lacks the comprehensive feature set and ecosystem of the full Rails framework, which may limit its suitability for more complex or feature-rich applications.
  • Has a smaller community and fewer third-party libraries and plugins compared to the full Rails framework.
  • May require more manual configuration and setup for certain features or integrations that are provided out-of-the-box in the full Rails framework.

Code Comparison

rails-api/rails-api

# config/routes.rb
Rails.application.routes.draw do
  scope '/api' do
    resources :posts
  end
end

# app/controllers/posts_controller.rb
class PostsController < ApplicationController
  def index
    @posts = Post.all
    render json: @posts
  end
end

cerebris/jsonapi-resources

# config/routes.rb
Rails.application.routes.draw do
  jsonapi_resources :posts
end

# app/resources/post_resource.rb
class PostResource < JSONAPI::Resource
  attributes :title, :body
end
55,872

Ruby on Rails

Pros of Rails

  • Rails provides a comprehensive and opinionated framework for building web applications, with a focus on rapid development and convention over configuration.
  • The Rails ecosystem offers a vast array of plugins and libraries, making it easier to integrate common functionality into your application.
  • Rails has a strong emphasis on developer productivity, with features like scaffolding and code generators that can speed up the development process.

Cons of Rails

  • Rails can be perceived as heavyweight and complex, especially for smaller or simpler projects that may not require the full feature set.
  • The opinionated nature of Rails can make it challenging to deviate from the "Rails way" of doing things, which may not always align with the specific requirements of a project.
  • Rails has a steeper learning curve compared to some other web frameworks, particularly for developers new to the Ruby language.

Code Comparison

Rails (5 lines):

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end
end

JSONAPI::Resources (5 lines):

class PostResource < JSONAPI::Resource
  attributes :title, :body
  has_many :comments
end

class CommentResource < JSONAPI::Resource
  attributes :text
  has_one :post
end

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data

Pros of railsadminteam/rails_admin

  • Comprehensive Admin Interface: Rails Admin provides a comprehensive and feature-rich admin interface for your Rails application, allowing you to manage your data and perform various administrative tasks with ease.
  • Customizable: The framework offers a high degree of customization, enabling you to tailor the admin interface to your specific needs and preferences.
  • Active Community: Rails Admin has an active community of contributors and users, ensuring ongoing support, bug fixes, and feature enhancements.

Cons of railsadminteam/rails_admin

  • Opinionated: Rails Admin can be considered more opinionated than JsonAPI Resources, as it imposes a specific structure and approach to your admin interface.
  • Dependency on Rails: Rails Admin is tightly coupled with the Ruby on Rails framework, making it less portable to other web frameworks or languages.

Code Comparison

JsonAPI Resources:

class PostResource < JSONAPI::Resource
  attributes :title, :body, :created_at, :updated_at
  has_one :author
  has_many :comments
end

Rails Admin:

RailsAdmin.config do |config|
  config.model 'Post' do
    list do
      field :title
      field :body
      field :created_at
      field :updated_at
    end
  end
end

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

JSONAPI::Resources Gem Version Build Status Code Climate

Join the chat at https://gitter.im/cerebris/jsonapi-resources

JSONAPI::Resources, or "JR", provides a framework for developing an API server that complies with the JSON:API specification.

Like JSON:API itself, JR's design is focused on the resources served by an API. JR needs little more than a definition of your resources, including their attributes and relationships, to make your server compliant with JSON API.

JR is designed to work with Rails 5.1+, and provides custom routes, controllers, and serializers. JR's resources may be backed by ActiveRecord models or by custom objects.

Documentation

Full documentation can be found at http://jsonapi-resources.com, including the v0.10 alpha Guide specific to this version.

Demo App

We have a simple demo app, called Peeps, available to show how JR is used.

Client Libraries

JSON:API maintains a (non-verified) listing of client libraries which should be compatible with JSON:API compliant server implementations such as JR.

Installation

Add JR to your application's Gemfile:

gem 'jsonapi-resources'

And then execute:

bundle

Or install it yourself as:

gem install jsonapi-resources

For further usage see the v0.10 alpha Guide

Contributing

  1. Submit an issue describing any new features you wish it add or the bug you intend to fix
  2. Fork it ( http://github.com/cerebris/jsonapi-resources/fork )
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Run the full test suite (rake test)
  5. Fix any failing tests
  6. Commit your changes (git commit -am 'Add some feature')
  7. Push to the branch (git push origin my-new-feature)
  8. Create a new Pull Request

Did you find a bug?

  • Ensure the bug was not already reported by searching on GitHub under Issues.

  • If you're unable to find an open issue addressing the problem, open a new one. Be sure to include a title and clear description, as much relevant information as possible, and a code sample or an executable test case demonstrating the expected behavior that is not occurring.

  • If possible, use the relevant bug report templates to create the issue. Simply copy the content of the appropriate template into a .rb file, make the necessary changes to demonstrate the issue, and paste the content into the issue description or attach as a file:

License

Copyright 2014-2021 Cerebris Corporation. MIT License (see LICENSE for details).