Why the next one?
- build up a playground for your ideas, prototypes, testing behaviour … whatever
no assumtions aboutyou can choose, if you want to use a backend/ORM, ergo no restrictions, only a pure grape/rack skeleton with a nice documentation
$ gem install grape-starter
Create a new project
$ grape-starter new awesome_api
with following options:
-f, --force # overwrites existend project -p foobar, --prefix=foobar # sets the prefix of the API (default: none) -o sequel, --orm=sequel # create files for the specified ORM, available: sequel, activerecord (ar) (default: none)
This command creates a folder named
awesome_api containing the skeleton. With following structure:
├── <Standards> ├── api │ ├── base.rb # the main API class, all other endpoints would be mounted in it │ ├── endpoints # contains the endpoint file for a resource │ │ └── root.rb # root is always available, it exposes all routes/endpoints, disable by comment it out in base.rb │ └── entities # contains the entity representation of the reource, if wanted │ └── route.rb ├── config # base configuration │ └── … ├── config.ru # Rack it up ├── lib # contains the additional lib file for a resource │ ├── models │ │ └── version.rb │ └── models.rb ├── public # for serving static files │ └── … ├── script # setup / server / test etc. │ └── … └── spec # RSpec └── …
--orm flag adds follwing files and directories to above project structure:
├── .config ├── config │ … │ ├── database.yml │ └── initializers │ └── database.rb … ├── db │ └── migrations …
Don't forget to adapt the
config/database.yml to your needs
and also to check the Gemfile for the right gems.
.config the choosen ORM would be stored.
To run it, go into awesome_api folder, start the server
$ cd awesome_api $ ./script/server *port
More could be found in README.
$ grape-starter add foo [http methods]
This adds endpoint and lib file and belonging specs, and a mount entry in base.rb.
Using it with following options:
-e, --entity # a grape entity file will also be created -m, --migration # adds also a migration file, if an ORM is used -o, --orm # sets the parent class of libe file (e.g: `Foo < Sequel::Model` for Sequel)
to add CRUD endpoints for resource foo. By given http methods only this one would be generated.
For available methods see:
grape-starter add foo post get
get_one to create the
get endpoint, it would be respected, if it is the singular or plural form of it, so the plural form would look like:
grape-starter add foos post get
this will use
get_specific to create the
get :id endpoints.
true, the lib class would be created as child class of a specific ORM class,
so for example for Sequel, it would be wirtten:
Foo < Sequel::Model instead of
Foo, hereby the using ORM would be taken from the configuration, which was stored by project creation.
Remove a resource
$ grape-starter rm foo
to remove previous generated files for a resource.
Any contributions are welcome on GitHub at https://github.com/LeFnord/grape-starter.
Adding a new ORM template
To add an new ORM, it needs following steps:
- A template class, with predefined methods …
module Starter module Templates module <YOUR NAME> def model_klass # the class, from which self inherit, e.g. 'Sequel::Model' end def initializer # provide your string end def config # provide your string end def rakefile # provide your string end def gemfile # provide your string end end end end
- An additional switch in the
Starter::Names.lib_klass_namemethods to choose the right template.
- An entry in the description of the
newcommand, when it would be called with
The gem is available as open source under the terms of the MIT License.