{"id":21646167,"url":"https://github.com/oddcamp/rails_http_options","last_synced_at":"2025-07-17T18:15:31.472Z","repository":{"id":56890585,"uuid":"110713660","full_name":"oddcamp/rails_http_options","owner":"oddcamp","description":"Simple gem that allows you to handle HTTP OPTIONS in Rails. Ideal for API introspection.","archived":false,"fork":false,"pushed_at":"2017-11-21T09:58:26.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-06T14:05:31.018Z","etag":null,"topics":["api","http","introspection","options","rails"],"latest_commit_sha":null,"homepage":"https://blog.kollegorna.se/clean-http-options-handling-in-rails-d8832c98ca51","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/oddcamp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-14T16:11:40.000Z","updated_at":"2017-11-30T21:35:10.000Z","dependencies_parsed_at":"2022-08-20T15:20:43.417Z","dependency_job_id":null,"html_url":"https://github.com/oddcamp/rails_http_options","commit_stats":null,"previous_names":["kollegorna/rails_http_options"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oddcamp%2Frails_http_options","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oddcamp%2Frails_http_options/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oddcamp%2Frails_http_options/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oddcamp%2Frails_http_options/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oddcamp","download_url":"https://codeload.github.com/oddcamp/rails_http_options/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243701469,"owners_count":20333631,"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","http","introspection","options","rails"],"created_at":"2024-11-25T06:38:01.654Z","updated_at":"2025-03-15T08:13:29.051Z","avatar_url":"https://github.com/oddcamp.png","language":"Ruby","readme":"# RailsHttpOptions\n\nSimple gem that allows you to handle HTTP OPTIONS in Rails.\nIdeal for API introspection, like fetching request/response schemas and other\nmeaningful for the client information.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rails_http_options'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install rails_http_options\n\n## Usage\nInclude `RailsHttpOptions` in your application controller.\nThen add a Rails route in your routes.rb denoting that you want to handle all\nHTTP OPTIONS in `option` method, defined by this gem.\n\n```ruby\n  match '*path', {\n    controller: 'application',\n    action: 'options',\n    constraints: { method: 'OPTIONS' },\n    via: [:options]\n  }\n```\n\nThen in any of your resource-based controller, add your options response:\n\n```ruby\n  class Api::V1::UsersController \u003c ApplicationController\n    options do\n      {\n        schemas: {\n          accepts: Company.json_schema,\n          returns: Company.json_schema\n        },\n        meta: { max_per_page: 100 }\n      }\n    end\n  end\n```\n\nYou can also respond differently depending on request information.\n\nSpecifically, you get route details param\ncoming from Rails routing mechanism and it's a simple hash.\nHowever, you have access to the regular request/response context inside the block,\nbecause just before is being called its context is changed to a controller's method,\ndefined by this gem. Hence, you can access\n[request](http://api.rubyonrails.org/classes/ActionDispatch/Request.html),\n[response](http://api.rubyonrails.org/v5.0.1/classes/ActionDispatch/Response.html),\n[params](http://api.rubyonrails.org/classes/ActionController/Parameters.html) etc,\nlike a regular controller method.\n\n\n```ruby\n  class Api::V1::UsersController \u003c ApplicationController\n    options do |route_details|\n      if route_details[:id] #member route\n        {\n          schemas: { #params is available through context switching\n            accepts: Company.find(params[:id]).introspection_schema,\n            returns: Company.find(params[:id]).introspection_schema,\n          }\n        }\n      else #collection route\n        {\n          schemas: {\n            returns: [Company.introspection_schema]\n          },\n          meta: { max_per_page: 100 }\n        }\n      end\n    end\n  end\n```\n\nThe response is always in JSON, but if you need something else (like yaml or XML),\nyou can always override the default\n[`options`](https://github.com/kollegorna/rails_http_options/blob/master/lib/rails_http_options.rb#L16-L25) method.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/kollegorna/rails_http_options.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foddcamp%2Frails_http_options","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foddcamp%2Frails_http_options","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foddcamp%2Frails_http_options/lists"}