Giter Site home page Giter Site logo

sinatra-rest-api's Introduction

Sinatra REST API Gem Version

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 available

Install: 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

class Book < ActiveRecord::Base
end

class App < Sinatra::Application
  register Sinatra::RestApi

  resource 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. 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:
Rails.application.eager_load!  # if cache_classes is off

class MySinatraApp < Sinatra::Base
  register Sinatra::RestApi

  ActiveRecord::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:
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:
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:

class Importer < ActiveResource::Base
  self.site = 'http://127.0.0.1:3000/sinatra'  # first app path

  def 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

License

ISC

sinatra-rest-api's People

Contributors

blocknotes avatar dependabot[bot] avatar

Stargazers

Yasin ATEŞ avatar  avatar  avatar Travis Dahlke avatar bxio avatar Jonathan Wong avatar Matthew J. Schultz avatar Dominic Hobus avatar Silver Kits avatar Christopher Voltz avatar Mario Luo avatar Orkhan ALIYEV avatar  avatar Adem ilter avatar taira horaguchi avatar TalaatMagdy avatar Sebastian Kessler avatar Andy Chang avatar Samuel Lin avatar Daniil Franks avatar Ripley Ludicrous avatar Carlos Azuaje avatar Lakshminp avatar Kent Gruber avatar Davide Da Rech avatar Davide Cenzi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.