bploetz / versionist

A plugin for versioning Rails based RESTful APIs.
MIT License
971 stars 51 forks source link

[VERSIONIST] attempt to set more than one default api version #23

Closed ticktricktrack closed 12 years ago

ticktricktrack commented 12 years ago

At the moment I have found 2 ways to define the routes, each giving me a different error: (version was generated with module Api::V2)

1) This one uses scope '/api/ and the full module "Api::V1", the generators work fine, but default version doesn't

scope '/api', :defaults => {:format => :json}  do
  api_version(:module => "Api::V1", :header=>"Accept", :value=>"application/mycompany.com; version=1", :default => true) do
    match 'test' => 'foos#test', via: :get
  end
end

returns:

[VERSIONIST] attempt to set more than one default api version
snip
versionist (0.2.2) lib/versionist/versioning_strategy/base.rb:14:in `initialize'
versionist (0.2.2) lib/versionist/versioning_strategy/header.rb:12:in `initialize'
versionist (0.2.2) lib/versionist/routing.rb:29:in `new'
versionist (0.2.2) lib/versionist/routing.rb:29:in `configure_header'
versionist (0.2.2) lib/versionist/routing.rb:17:in `api_version'
snip 

whereas my other configuration option using namespace instead of scope does support default values, but the generators will fail:

namespace :api, :defaults => {:format => :json}  do
  api_version(:module => "V1", :header=>"Accept", :value=>"application/mycompany.com; version=1", :default => true) do
    match 'test' => 'foos#test', via: :get
  end
end

produces 

```bash
rails g versionist:new_controller foos Api::V1
.rvm/gems/ruby-1.9.3-head@v2/gems/versionist-0.2.2/lib/generators/versionist/new_controller/new_controller_generator.rb:19:in `block in new_controller': API version doesn't exist in config/routes.rb. Please run 'rails generate versionist:new_api_version' generator first (RuntimeError)
bploetz commented 12 years ago

Regarding the first error, can you include:

As for the second error when running the generator, the way the generator verifies that the API version is already present is that it looks for the module passed on the command line in an api_version() method in config/routes.rb, and if it doesn't find it you get the error above. Since you passed Api::V1 on the command line, but the api_version() in your config/routes.rb only has :module => "V1" (missing the Api:: bit), it's erroring out. Versionist doesn't currently have the smarts to look for an API version across namespace/scope and api_version. Pull requests welcome. :-)

Having said that, does this get you what you're looking for? It works for me locally.

api_version(:module => "Api::V1", :header=>"Accept", :value=>"application/mycompany.com; version=1", :default => true, :defaults => {:format => :json}) do
  scope :path => '/api' do
    match 'test' => 'foos#test', via: :get
  end
end
ticktricktrack commented 12 years ago

Just getting back to my API, no more delays, no more other more important features. I hate having to read me back in to my own reported issues instead of being able to fix them. I'm going to solve this now!

ticktricktrack commented 12 years ago

You were absolutely right, works fine now.