Created
August 26, 2015 15:41
-
-
Save joonty/1ab9f5635587c302d7f4 to your computer and use it in GitHub Desktop.
API rails constraint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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