Skip to content

Instantly share code, notes, and snippets.

@alexfedoseev alexfedoseev/api_constraints.rb Secret
Created Apr 30, 2015

Embed
What would you like to do?
Rails API routing: iteration 3
# lib/api_constraints.rb
class ApiConstraints
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(req)
@default || req.headers['Accept'].include?("application/vnd.isomorphic-comments.v#{@version}+json")
end
end
# config/routes.rb
require 'api_constraints'
Rails.application.routes.draw do
namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/' do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resources :comments
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.