{"id":13493333,"url":"https://github.com/blocknotes/sinatra-rest-api","last_synced_at":"2025-04-22T10:21:37.587Z","repository":{"id":53137063,"uuid":"73628171","full_name":"blocknotes/sinatra-rest-api","owner":"blocknotes","description":"Sinatra REST API generator","archived":false,"fork":false,"pushed_at":"2022-03-18T18:09:58.000Z","size":66,"stargazers_count":26,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-03T18:58:00.875Z","etag":null,"topics":["api","rest","ruby","sinatra"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blocknotes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["blocknotes"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":null}},"created_at":"2016-11-13T16:30:01.000Z","updated_at":"2022-04-03T20:27:31.000Z","dependencies_parsed_at":"2022-08-24T01:40:53.580Z","dependency_job_id":null,"html_url":"https://github.com/blocknotes/sinatra-rest-api","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Fsinatra-rest-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Fsinatra-rest-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Fsinatra-rest-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blocknotes%2Fsinatra-rest-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blocknotes","download_url":"https://codeload.github.com/blocknotes/sinatra-rest-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235059577,"owners_count":18929289,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","rest","ruby","sinatra"],"created_at":"2024-07-31T19:01:14.281Z","updated_at":"2025-01-22T03:53:02.943Z","avatar_url":"https://github.com/blocknotes.png","language":"Ruby","readme":"# Sinatra REST API [![Gem Version](https://badge.fury.io/rb/sinatra-rest-api.svg)](https://badge.fury.io/rb/sinatra-rest-api)\n\nA Sinatra component that generates CRUD routes for database ORM models.\n\nFeatures:\n- supports Active Record, Mongoid and Sequel ORMs\n- default actions already setup\n- nested resources available\n\nInstall: `gem install sinatra-rest-api` (or in Gemfile)\n\n## Options\n\nFor *resource* DSL keyword:\n- **actions**: list of actions to enable (array of symbols)\n- **include**: list of associations to expose in list/read actions (array of symbols) or false to skip every association\n- **limit**: set a fixed limit server side\n- **plural**: plural model name used for routes (string)\n- **singular**: singular model name for resource key (string)\n\nRoutes parameters:\n- **limit**: limit results (ex. 20)\n- **offset**: results offset (ex. 40)\n\n## Examples\n\n```rb\nclass Book \u003c ActiveRecord::Base\nend\n\nclass App \u003c Sinatra::Application\n  register Sinatra::RestApi\n\n  resource Book\n  resource Category do\n    actions [ :list, :read ]\n  end\nend\n```\n\nGenerates:\n\n```\nGET:    /books/:id.?:format?\nPUT:    /books/:id.?:format?\nDELETE: /books/:id.?:format?\nPUT:    /books/?.?:format?\nPOST:   /books/?.?:format?\nDELETE: /books/?.?:format?\nGET:    /books/?.?:format?\nGET:    /categories/:id.?:format?\nGET:    /categories/?.?:format?\n```\n\n#### More examples\n\nSee [examples](examples/). Execute: `thin start` in the folder of an example.\n\n#### Using inside a Rails app\n\nLet's create an Importer class to import data between 2 Rails app.\n\nA simple way to add an API for every model on the source app:\n\n- create a sinatra app in: `lib/sinatra_app/app.rb`:\n\n```rb\nRails.application.eager_load!  # if cache_classes is off\n\nclass MySinatraApp \u003c Sinatra::Base\n  register Sinatra::RestApi\n\n  ActiveRecord::Base.descendants.each do |model|  # Iterate all models\n    next if model.abstract_class\n    resource model, actions: [ :list, :read ], include: false\n  end\nend\n```\n\n- load the sinatra app editing `config/application.rb`:\n\n```rb\nmodule RailsSinatra\n  class Application \u003c Rails::Application\n    config.after_initialize do\n      require_relative '../lib/sinatra_app/app'\n    end\n  end\nend\n```\n\n- setup the routes editing `config/routes.rb`:\n\n```rb\nRails.application.routes.draw do\n  mount MySinatraApp.new =\u003e '/sinatra'\nend\n```\n\n- now you can access to every model under 'sinatra' path, example: '/sinatra/posts.json'\n\n- on the destination app you can access these models using *ActiveResource* gem:\n\n```rb\nclass Importer \u003c ActiveResource::Base\n  self.site = 'http://127.0.0.1:3000/sinatra'  # first app path\n\n  def self.init( collection_name )\n    set_collection_name collection_name\n    self\n  end\nend\n\n# List of tags names:\nImporter.init( 'tags' ).all.map( \u0026:name )\n```\n\n## Tests\n\nRun individual example apps tests:\n- `rspec spec/app_active_record_spec.rb`\n- `rspec spec/app_mongoid_spec.rb`\n- `rspec spec/app_sequel_spec.rb`\n\n## Do you like it? Star it!\n\nIf you use this component just star it. A developer is more motivated to improve a project when there is some interest.\n\n## Contributors\n\n- [Mattia Roccoberton](http://blocknot.es) - creator, maintainer\n\n## License\n\n[ISC](LICENSE)\n","funding_links":["https://github.com/sponsors/blocknotes"],"categories":["Writing APIs"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblocknotes%2Fsinatra-rest-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblocknotes%2Fsinatra-rest-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblocknotes%2Fsinatra-rest-api/lists"}