Skip to content

Instantly share code, notes, and snippets.

@joonty
Created August 26, 2015 15:41
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 joonty/1ab9f5635587c302d7f4 to your computer and use it in GitHub Desktop.
Save joonty/1ab9f5635587c302d7f4 to your computer and use it in GitHub Desktop.
API rails constraint
class ApiConstraint
attr_reader :version
def initialize(options)
@version = options.fetch(:version)
end
def matches?(request)
request
.headers
.fetch(:accept)
.include?("version=#{version}")
end
end
Rails.application.routes.draw do
namespace :api, path: "/", constraints: { subdomain: "api" }, format: :json do
scope module: :v1, constraints: ApiConstraint.new(version: 1) do
# Put your v1 controllers here
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment