Many JSONAPI gems are being developed, mostly focusing on serialization. Few have implemented the parameters defined in the FetchData specifications. http://jsonapi.org/format/#fetching
This gem aims to focus on doing just that specific task. Making it easy to add FetchData parameters to gems which don't have support for them. The specific string delimiters noted in FetchData parameters, are not supported in Rack by default. We attempt to parse the parameter values into a hash structure. Trying to mimick the hash style notation used in ActiveRecord Querying.
Add this line to your application's Gemfile:
gem 'jsonapi-fetchdata'
And then execute:
$ bundle
Or install it yourself as:
$ gem install jsonapi-fetchdata
GET /posts
def index
JSONAPI::FetchData::Collection.find(Post, parameters)
end
GET /posts/1
def show
JSONAPI::FetchData::Resource.find(Post, parameters)
end
Convert Rack request.parameters to JSONAPI format Add as middleware to Rails in config/application.rb
config.middleware.insert_after ActionDispatch::ParamsParser, 'JSONAPI::FetchData::Middleware'
Change maximum returned resource objects, when no pagination parameters have been provided. 50 by default.
JSONAPI::FetchData.config.max_per_page = 5
or
JSONAPI::FetchData.configure do |config|
config.max_per_page = 5
end
Parser registration and processing Rack parameters to JSONAPI
Implementing parsing of delimination for each of the FetchData parameters, include, fields, sort, filter and page.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/mvkampen/jsonapi-fetchdata.
The gem is available as open source under the terms of the MIT License.