如何在Sinatra应用程序中使用api约束进行版本控制?

时间:2014-08-24 13:02:22

标签: ruby api routes sinatra rack

我有简单的Sinatra应用程序。

# app/routes/v2/accounts.rb
module Oauth2Provider
  module Routes
    module V2
      class Accounts < Base
        get '/accounts' do
          content_type :json
          'version 2'
        end
      end
    end
  end
end
# app/routes/v1/accounts.rb
module Oauth2Provider
  module Routes
    module V1
      class Accounts < Base
        get '/accounts' do
          content_type :json
          'version 1'
        end
      end
    end
  end
end

# app.rb
require 'rubygems'
require 'bundler'

# Setup load paths
Bundler.require
$: << File.expand_path('../', __FILE__)
$: << File.expand_path('../lib', __FILE__)

require 'sinatra/base'
require 'app/models'
require 'app/routes'

module Oauth2Provider
  class App < Sinatra::Application
    configure do
      set :database, {
        adapter: "sqlite3",
        database: "db/oauthdb.sqlite3"
      }
    end

    use Oauth2Provider::Routes::V1::Accounts
    use Oauth2Provider::Routes::V2::Accounts
  end
end

include Oauth2Provider::Models

# app/routes.rb
module Oauth2Provider
  module Routes
    autoload :Base,     'app/routes/base'
    # autoload :Accounts, 'app/routes/v2/accounts'
    module V1
      autoload :Accounts, 'app/routes/v1/accounts'
    end
    module V2
      autoload :Accounts, 'app/routes/v2/accounts'
    end
  end
end

我试图使用这个gem =&gt; https://github.com/techwhizbang/rack-api-versioning

更新:我还找到https://github.com/brandur/sinatra-router但无法弄清楚如何将其添加到我的应用

但是如何在我的代码中实现这一点,以根据标题选择正确的模块进行渲染。

0 个答案:

没有答案