Last active
September 15, 2019 23:59
-
-
Save dudo/15c948f490b6f0f162c1dd60a0458ca9 to your computer and use it in GitHub Desktop.
rails api versioned
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 | |
def initialize(options) | |
@version = options[:version] | |
@default = options[:default] | |
end | |
def matches?(request) | |
@default || 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, 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