{"id":16079208,"url":"https://github.com/pat/sliver-rails","last_synced_at":"2026-01-10T17:04:01.532Z","repository":{"id":27389334,"uuid":"30865484","full_name":"pat/sliver-rails","owner":"pat","description":null,"archived":false,"fork":false,"pushed_at":"2017-10-08T01:30:08.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-18T14:54:18.816Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-16T11:31:42.000Z","updated_at":"2016-11-15T05:14:52.000Z","dependencies_parsed_at":"2022-08-20T07:50:28.981Z","dependency_job_id":null,"html_url":"https://github.com/pat/sliver-rails","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/pat/sliver-rails","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pat%2Fsliver-rails","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pat%2Fsliver-rails/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pat%2Fsliver-rails/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pat%2Fsliver-rails/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pat","download_url":"https://codeload.github.com/pat/sliver-rails/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pat%2Fsliver-rails/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275623477,"owners_count":25498340,"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","status":"online","status_checked_at":"2025-09-17T02:00:09.119Z","response_time":84,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-10-09T10:41:27.219Z","updated_at":"2025-09-17T16:33:18.549Z","avatar_url":"https://github.com/pat.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sliver-Rails\n\nRails-focused extensions to the [Sliver](https://github.com/pat/sliver) API libary.\n\n## Installation\n\nAdd the gem to your Gemfile:\n\n```ruby\ngem 'sliver-rails', '~\u003e 0.2.1'\n```\n\n## Usage\n\nSlightly different to the pure Sliver approach, instead of including the `Sliver::Action` module, you should instead inherit from `Sliver::Rails::Action`.\n\n```ruby\napp = Sliver::API.new do |api|\n  api.connect :get, '/changes/:id', ChangeAction\nend\n\nclass ChangeAction \u003c Sliver::Rails::Action\n  def call\n    change = Change.find path_params['id']\n\n    response.status = 200\n    response.body   = [\"Change: #{change.name}\"]\n  end\nend\n```\n\n### Parameters\n\nJust like in Rails controllers, all incoming request parameters are treated as strong parameters, so you can be explicit about what you're expecting to receive.\n\n```ruby\nclass V1::Changes::Create \u003c Sliver::Rails::Action\n  def call\n    change = Change.create change_params\n\n    response.status = 200\n    response.body   = [\"Success\"]\n  end\n\n  private\n\n  def change_params\n    params.fetch(:change, {}).permit(:name, :description)\n  end\nend\n```\n\n### JSON Requests\n\nIf the content-type of the request has been set to `application/json`, then the request body data sent through in POST and PUT requests is parsed and available via the `params` method.\n\n### Inbuilt JSON Processor\n\nMost modern APIs will return JSON-formatted data - and so a Sliver Processor is provided to automatically translate response bodies into JSON. So, in the example below, the response body will be transformed to `\"{\\\"status\\\":\\\"OK\\\"}\"`, and the Content-Type header will be set to `application/json`.\n\n```ruby\nclass V1::Changes::Create \u003c Sliver::Rails::Action\n  def self.processors\n    [Sliver::Rails::Processors::JSONProcessor]\n  end\n\n  def call\n    change = Change.create change_params\n\n    response.status = 200\n    response.body   = {:status =\u003e \"OK\"}\n  end\n\n  private\n\n  def change_params\n    params.fetch(:change, {}).permit(:name, :description)\n  end\nend\n```\n\n### JSON templates and exposed variables\n\nThe JSON Processor can also use a Jbuilder template (provided you've got the `jbuilder` gem in your Gemfile) via the `use_template` method. Similarly to [decent_exposure](https://github.com/voxdolo/decent_exposure) (although without most of the features), anything specified with a `expose` call is available in the template view.\n\n```ruby\nclass V1::Changes::Create \u003c Sliver::Rails::Action\n  # This will use app/views/api/changes/show.jbuilder\n  use_template 'api/changes/show'\n\n  expose(:change) { Change.new change_params }\n\n  def self.processors\n    [Sliver::Rails::Processors::JSONProcessor]\n  end\n\n  def call\n    response.status = 200 if change.save\n\n    # If you use response.body, the template will not be used.\n    # response.body = {:status =\u003e 'OK'}\n  end\n\n  private\n\n  def change_params\n    params.fetch(:change, {}).permit(:name, :description)\n  end\nend\n\n# app/views/api/changes/show.jbuilder\njson.(change, :name, :description)\n```\n\nThis is a very simple example, but your Jbuilder templates can be as complex as you like. Blocks provided for `expose` will only be evaluated once and remembered - and if no block is provided, it looks for a private method of the same name (which is also only evaluated once when referenced from the template).\n\nThe default template is an instance of `Sliver::Rails::JBuilderTemplate`, which has one method available beyond what you've exposed: `routes`, which provides access to your Rails route helper methods. No other Rails helper methods are provided by default, but you can use your own template if you'd like:\n\n```ruby\n# config/initializers/sliver.rb\nclass ApiTemplate\n  include ActionView::Helpers::NumberHelper\n\n  def routes\n    Rails.application.routes\n  end\nend\n\nRails.application.config.after_initialize do\n  Sliver::Rails.template_class = ApiTemplate\nend\n```\n\n### Common directory and file structures\n\nThis is not enforced by this gem, but it's a habit we at [Inspire9](http://inspire9dev.com) have fallen into:\n\n* Separate APIs for separate versions.\n* `app/apis/v1.rb` contains the API definition\n* `app/apis/v1/posts/create.rb`, `app/apis/v1/posts/show.rb`, `app/apis/v1/posts/index.rb` and so forth covering CRUD behaviour for resources.\n* All endpoint classes inherit from an app-specific base class (covering things like `current_user`, setting guards and processors), and that base class inherits from `Sliver::Rails::Action`.\n\n## Contributing\n\nPlease note that this project now has a [Contributor Code of Conduct](http://contributor-covenant.org/version/1/0/0/). By participating in this project you agree to abide by its terms.\n\n1. Fork it ( https://github.com/pat/sliver-rails/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpat%2Fsliver-rails","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpat%2Fsliver-rails","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpat%2Fsliver-rails/lists"}