Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/blocknotes/sinatra-rest-api
Sinatra REST API generator
https://github.com/blocknotes/sinatra-rest-api
api rest ruby sinatra
Last synced: 7 days ago
JSON representation
Sinatra REST API generator
- Host: GitHub
- URL: https://github.com/blocknotes/sinatra-rest-api
- Owner: blocknotes
- License: isc
- Created: 2016-11-13T16:30:01.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-03-18T18:09:58.000Z (over 2 years ago)
- Last Synced: 2024-11-01T16:51:30.991Z (14 days ago)
- Topics: api, rest, ruby, sinatra
- Language: Ruby
- Homepage:
- Size: 64.5 KB
- Stars: 26
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Sinatra REST API [![Gem Version](https://badge.fury.io/rb/sinatra-rest-api.svg)](https://badge.fury.io/rb/sinatra-rest-api)
A Sinatra component that generates CRUD routes for database ORM models.
Features:
- supports Active Record, Mongoid and Sequel ORMs
- default actions already setup
- nested resources availableInstall: `gem install sinatra-rest-api` (or in Gemfile)
## Options
For *resource* DSL keyword:
- **actions**: list of actions to enable (array of symbols)
- **include**: list of associations to expose in list/read actions (array of symbols) or false to skip every association
- **limit**: set a fixed limit server side
- **plural**: plural model name used for routes (string)
- **singular**: singular model name for resource key (string)Routes parameters:
- **limit**: limit results (ex. 20)
- **offset**: results offset (ex. 40)## Examples
```rb
class Book < ActiveRecord::Base
endclass App < Sinatra::Application
register Sinatra::RestApiresource Book
resource Category do
actions [ :list, :read ]
end
end
```Generates:
```
GET: /books/:id.?:format?
PUT: /books/:id.?:format?
DELETE: /books/:id.?:format?
PUT: /books/?.?:format?
POST: /books/?.?:format?
DELETE: /books/?.?:format?
GET: /books/?.?:format?
GET: /categories/:id.?:format?
GET: /categories/?.?:format?
```#### More examples
See [examples](examples/). Execute: `thin start` in the folder of an example.
#### Using inside a Rails app
Let's create an Importer class to import data between 2 Rails app.
A simple way to add an API for every model on the source app:
- create a sinatra app in: `lib/sinatra_app/app.rb`:
```rb
Rails.application.eager_load! # if cache_classes is offclass MySinatraApp < Sinatra::Base
register Sinatra::RestApiActiveRecord::Base.descendants.each do |model| # Iterate all models
next if model.abstract_class
resource model, actions: [ :list, :read ], include: false
end
end
```- load the sinatra app editing `config/application.rb`:
```rb
module RailsSinatra
class Application < Rails::Application
config.after_initialize do
require_relative '../lib/sinatra_app/app'
end
end
end
```- setup the routes editing `config/routes.rb`:
```rb
Rails.application.routes.draw do
mount MySinatraApp.new => '/sinatra'
end
```- now you can access to every model under 'sinatra' path, example: '/sinatra/posts.json'
- on the destination app you can access these models using *ActiveResource* gem:
```rb
class Importer < ActiveResource::Base
self.site = 'http://127.0.0.1:3000/sinatra' # first app pathdef self.init( collection_name )
set_collection_name collection_name
self
end
end# List of tags names:
Importer.init( 'tags' ).all.map( &:name )
```## Tests
Run individual example apps tests:
- `rspec spec/app_active_record_spec.rb`
- `rspec spec/app_mongoid_spec.rb`
- `rspec spec/app_sequel_spec.rb`## Do you like it? Star it!
If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
## Contributors
- [Mattia Roccoberton](http://blocknot.es) - creator, maintainer
## License
[ISC](LICENSE)