GrapeResource
GrapeResource is opinionated resource generator for your Grape API mounted inside Rails.
By using GrapeResource, you could generate Grape endpoints just like rails generate scaffold controller. GrapeResource only generates API resource. You need to create a model by yourself.
Dependencies
- Grape (mounted on Rails)
- Grape Entity
- Rspec
Installation
Add this line to your application's Gemfile:
gem 'grape_resource'
And then execute:
$ bundle
Or install it yourself as:
$ gem install grape_resource
Usage
Initialize
Initialize GrapeResource with command :
$ rails g grape_resource:install
Configuration
Modify GrapeResource configuration inside config/initializers/grape_resource.rb
config.directorywill be your endpoint directory. Default to :controllers/API/v1/config.class_name_prefixwill be your class name prefix for your endpoint. Default to :API::V1config.entry_point_routeswill be your entry point for your routes. Default to :# Routes entry pointconfig.success_formatterwill be your success formatter. Default to :formatter :json, ::API::SuccessFormatterconfig.error_formatterwill be your error formatter. Default to :error_formatter :json, ::API::ErrorFormatter
Generator
To generate REST API :
$ rails g grape_resource:rest class_name
Example :
$ rails g grape_resource:rest article
This will give you :
- GET /
- GET /:id
- POST /
- PUT /:id
- DELETE /:id
To generate namespaced API :
$ rails g grape_resource:namespace namespace api_endpoint1 api_endpoint2:method
Example :
$ rails g grape_resource:namespace article popular like:post similar:get:member trending:get:collection
While method is one of :
get(default)postputdelete
Command above will give you :
- GET /namespace/api_endpoint1
- METHOD /namespace/api_endpoint2
Endpoint generators above contains :
routes.rbThis is your endpoint entry point. You could mount it to your main entry pointentitiesdirectory. It contains grape entity for your endpoint.resourcesdirectory. It contains your endpoint.specdirectory. It contains rspec test for your endpoint.
Development
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.
Release
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.
You could use gem release.
- Install gem release using command
gem install gem-release. - Update version using command
gem bump minor - Build gem using command
gem build grape_resource.gemspec - Release gem using command
gem release
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/extrainteger/grape_resource. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the GrapeResource project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.