{"id":13339963,"url":"https://github.com/jgraichen/rails-rfc6570","last_synced_at":"2025-10-25T16:43:35.971Z","repository":{"id":17689588,"uuid":"20495516","full_name":"jgraichen/rails-rfc6570","owner":"jgraichen","description":"Pragmatical access to your Rails routes as RFC6570 URI templates.","archived":false,"fork":false,"pushed_at":"2024-03-09T17:24:06.000Z","size":135,"stargazers_count":7,"open_issues_count":2,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-05-10T02:40:29.959Z","etag":null,"topics":["api","rails","restful","rfc-6570","routing","ruby","uri-template"],"latest_commit_sha":null,"homepage":"","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/jgraichen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-06-04T18:48:14.000Z","updated_at":"2024-05-30T07:06:55.366Z","dependencies_parsed_at":"2024-03-09T18:39:01.733Z","dependency_job_id":null,"html_url":"https://github.com/jgraichen/rails-rfc6570","commit_stats":{"total_commits":104,"total_committers":7,"mean_commits":"14.857142857142858","dds":0.5,"last_synced_commit":"37cd2e479909bd32c0988cb3587333c2d6edf622"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Frails-rfc6570","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Frails-rfc6570/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Frails-rfc6570/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jgraichen%2Frails-rfc6570/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jgraichen","download_url":"https://codeload.github.com/jgraichen/rails-rfc6570/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244962917,"owners_count":20539246,"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","rails","restful","rfc-6570","routing","ruby","uri-template"],"created_at":"2024-07-29T19:21:23.146Z","updated_at":"2025-10-25T16:43:35.950Z","avatar_url":"https://github.com/jgraichen.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rails::RFC6570\n\n[![Gem Version](https://img.shields.io/gem/v/rails-rfc6570?logo=ruby)](https://rubygems.org/gems/rails-rfc6570)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/jgraichen/rails-rfc6570/test.yml?logo=github)](https://github.com/jgraichen/rails-rfc6570/actions/workflows/test.yml)\n\nPragmatic access to your Rails routes as RFC6570 URI templates.\n\nTested with Rails 6.1, 7.0, 7.1, 7.2, 8.0 and Ruby 2.7, 3.0, 3.1, 3.2, 3.3, and 3.4.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rails-rfc6570', '~\u003e 3.5'\n```\n\n## Usage\n\n**Rails::RFC6570** gives you direct access to your Rails routes as RFC6570 URI templates using the [addressable](https://github.com/sporkmonger/addressable) gem. It further patches `Addressable::Template` with a `#as_json` and `#to_s` so that you can simply pass the template objects or even partial expanded templates to your render call, decorator or serializer.\n\nThe following examples print a JSON index resource just like `https://api.github.com`:\n\n```ruby\nclass ApplicationController \u003c ActionController::API\n  def index\n    render json: rfc6570_routes(ignore: %w(format), path_only: false)\n  end\nend\n```\n\n**Pro Tip**: Append `_url` to the route names: `rfc6570_routes.transform_keys {|k| \"#{k}_url\" }`.\n\nBy default, the `format` placeholder is ignored and the HTTP host will be included in the URI template.\n\nAdditionally, you can specify a list of query parameters in your controllers:\n\n```ruby\nclass UserController \u003c ApplicationController\n\n  rfc6570_params index: [:query, :email, :active]\n  def index\n    # ...\n  end\n\n  def show\n    # ...\n  end\n\n  # ...\nend\n```\n\nGiven the above and this routes\n\n```ruby\nRails::Application.routes.draw do\n  resources :users, except: [:new, :edit]\n  root to: 'application#index'\nend\n```\n\nthe root action will return something similar to the following JSON:\n\n```json\n{\n  \"users\": \"http://localhost:3000/users{?query,email,active}\",\n  \"user\": \"http://localhost:3000/users/{id}\",\n  \"root\": \"http://localhost:3000/\"\n}\n```\n\nYou can also access your RFC6570 routes pragmatically everywhere you can access Rails' URL helpers e.g. in a decorator.\n\nYou can use this to e.g. partial expand templates for nested resources:\n\n```ruby\nmodule ApplicationHelpers\n  include Rails.application.routes.url_helpers\nend\n\nclass UserDecorator \u003c Draper::Decorator\n  def as_json(opts)\n    {\n      id: object.id,\n      self_url: user_url(object),\n      posts_url: user_posts_rfc6570.partial_expand(user_id: object.id),\n    }\n  end\nend\n```\n\nThis gem does not support every construct possible with route matchers especially nested groups cannot be expressed in URI templates. They are expanded into separate groups. It also makes some assumptions when converting splat matchers like swallowing a multiple slashes. An error is raised when routes with OR-clauses are tried to be converted.\n\nYou can also combine **Rails::RFC6570** with [`rack-link_headers`](https://github.com/jgraichen/rack-link_headers) and provide hypermedia linking everywhere!\n\n```ruby\nclass UserController \u003c ApplicationController\n  respond_to :json\n\n  def show\n    @user = User.find\n    response.link user_url(@user), rel: :self\n    response.link user_posts_rfc6570.partial_expand(user_id: @user.id), rel: :posts\n    response.link profile_rfc6570.expand(user_id: @user.id), rel: :profile\n\n    respond_with @user\n  end\nend\n```\n\n## Contributing\n\n1. [Fork it](http://github.com/jgraichen/rails-routes/fork)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Add specs\n4. Commit your changes (`git commit -am 'Add some feature'`)\n5. Push to the branch (`git push origin my-new-feature`)\n6. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgraichen%2Frails-rfc6570","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjgraichen%2Frails-rfc6570","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjgraichen%2Frails-rfc6570/lists"}