jsonapi.rb icon indicating copy to clipboard operation
jsonapi.rb copied to clipboard

Plain ruby examples

Open alexandru-calinoiu opened this issue 4 years ago • 1 comments

Expected Behavior

Will like the readme to have plain ruby examples

Actual Behavior

The readme looks very rails oriented

Steps to Reproduce the Problem

None

Specifications

  • Version:
  • Ruby version:

alexandru-calinoiu avatar May 26 '20 05:05 alexandru-calinoiu

We've been enjoying this gem for Rails controllers but now moving to Grape API's it would be smashing if there was an easy to use interface. So a wee plus one for this.

In general for Grape it is as easy as not having the initialiser to fire it and instead including the filters in your API as follows

class API < Grape::API
  API_BASE_URL = "http://localhost:9001"

  content_type :jsonapi, "application/vnd.api+json"
  default_format :jsonapi
  format :jsonapi

  helpers JSONAPI::Pagination, JSONAPI::Filtering

  resources :users do
    get do
      allowed = [:id, :email]
      filtered = jsonapi_filter(User.includes(:documents, :addresses).all, allowed)
      users = jsonapi_paginate(filtered.result)

      pagination_meta  = jsonapi_pagination_meta(users)
      pagination_links = jsonapi_pagination(users)

      if pagination_meta.present?
        options[:meta]  = { pagination: pagination_meta }
        options[:links] = pagination_links
      end

      UserSerializer.new(users, options).serializable_hash
    end
end

code-bunny avatar Aug 02 '23 21:08 code-bunny