Skip to content

Instantly share code, notes, and snippets.

@dudo
Last active September 15, 2019 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dudo/15c948f490b6f0f162c1dd60a0458ca9 to your computer and use it in GitHub Desktop.
Save dudo/15c948f490b6f0f162c1dd60a0458ca9 to your computer and use it in GitHub Desktop.
rails api versioned
class ApiConstraint
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(request)
@default || request.headers.fetch(:accept).include?("version=#{version}")
end
end
Rails.application.routes.draw do
namespace :api, defaults: {format: 'json'} do
scope module: :v1, constraints: ApiConstraint.new(version: 1, default: true) do
resources ..., shallow: true do
resources ...
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment