voltrb / volt

A Ruby web framework where your Ruby runs on both server and client
MIT License
3.22k stars 196 forks source link

Auto API generation #324

Open jfahrer opened 8 years ago

jfahrer commented 8 years ago

Summary

This PR implements a basic version of a HttpController for automatic JSON API generation.

What's new

Volt::RestfullBaseController

For server side controllers you can now inherit from Volt::RestfullBaseController. You can either pass in the model as a param or set it via model :name in the controller. The later overwrites the param. The following helpers are available:

The controller also sets up your resource based on the action:

A server side controller that inherits from Volt::SimpleJsonController automatically generates a simple JSON API for your models. You can either set the model via model :name in the controller or pass in the model via the params. You can do so in your routes file:

rest '/api/issues', component: 'api', controller: 'api', model: 'issue'
rest '/api/todos', component: 'api', controller: 'api', model: 'todo'
get '/api/cats', component: 'api', controller: 'api', action: index, model: 'cats'

Volt::SimpleJsonController implements the index, show, create, update and destroy actions for you. For updating and creation you need set the Content-Type of the request to 'aplication/json'. The JSON itself has to be prefixed with the model name. For example: { "todo": { "name": "the_name" }}

Volt::SimpleJsonController inherits from Volt::RestfullBaseController. So you have the same helpers available.

What's missing