Browse files

Merge pull request #31 from fabn/prefix-support

Version prefix support
  • Loading branch information...
2 parents 04822cf + 676379b commit 8647c7a6f4da09694e6ae20644eaf7beb07c8423 @Sutto committed Sep 29, 2012
Showing with 42 additions and 0 deletions.
  1. +5 −0 lib/rocket_pants/routing.rb
  2. +37 −0 spec/rocket_pants/routing_spec.rb
View
5 lib/rocket_pants/routing.rb
@@ -11,6 +11,11 @@ def rocket_pants(options = {}, &blk)
versions.each do |version|
raise ArgumentError, "Got invalid version: '#{version}'" unless version =~ /\A\d+\Z/
end
+ if (optional_prefix = options.delete(:allow_prefix))
+ versions.map! { |v| "#{optional_prefix}?#{v}" }
+ elsif (required_prefix = options.delete(:require_prefix))
+ versions.map! { |v| "#{required_prefix}#{v}" }
+ end
versions_regexp = /(#{versions.uniq.join("|")})/
raise ArgumentError, 'please provide atleast one version' if versions.empty?
options = options.deep_merge({
View
37 spec/rocket_pants/routing_spec.rb
@@ -50,6 +50,43 @@ def expect_options(*args)
end
+ context 'api routes with prefix support' do
+
+ before :each do
+ draw_routes do
+ api :version => 1, :allow_prefix => 'v' do
+ get 'a', :to => 'test#echo'
+ end
+ api :versions => %w(2 3) do
+ get 'b', :to => 'test#echo'
+ end
+ api :versions => %w(4 5), :require_prefix => 'v' do
+ get 'c', :to => 'test#echo'
+ end
+ end
+ end
+
+ it 'should recognise a path with and without prefix when allow_option is given' do
+ recognize_path('/v1/a').should == {:controller => 'test', :action => 'echo', :version => 'v1', :format => 'json'}
+ recognize_path('/1/a').should == {:controller => 'test', :action => 'echo', :version => '1', :format => 'json'}
+ end
+
+ it 'should not recognise a path when prefix is given' do
+ expect { recognize_path('/v2/b') }.to raise_error(ActionController::RoutingError)
+ expect { recognize_path('/v3/b') }.to raise_error(ActionController::RoutingError)
+ recognize_path('/2/b').should == {:controller => 'test', :action => 'echo', :version => '2', :format => 'json'}
+ recognize_path('/3/b').should == {:controller => 'test', :action => 'echo', :version => '3', :format => 'json'}
+ end
+
+ it 'should recognise a path only with version when require_prefix is given' do
+ recognize_path('/v4/c').should == {:controller => 'test', :action => 'echo', :version => 'v4', :format => 'json'}
+ recognize_path('/v5/c').should == {:controller => 'test', :action => 'echo', :version => 'v5', :format => 'json'}
+ expect { recognize_path('/4/c') }.to raise_error(ActionController::RoutingError)
+ expect { recognize_path('/5/c') }.to raise_error(ActionController::RoutingError)
+ end
+
+ end
+
it 'should not let you draw a route without a version' do
expect_options.to raise_error(ArgumentError)
expect_options(:version => []).to raise_error(ArgumentError)

2 comments on commit 8647c7a

@mbhnyc

Hey guys just curious - is prefix support not in the change log or docs because it's not "ready to go" yet? Or is that just an oversight?

@Sutto
Owner
Please sign in to comment.