Skip to content

Commit

Permalink
In progress, attempting to fix ruby-grape#133.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Jan 4, 2013
1 parent 3c1e695 commit 79872d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/grape/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ def initialize
end

def call(env)
p "call: #{env}"
@route_set.call(env)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ def prepare_routes
def prepare_path(path)
parts = []
parts << settings[:root_prefix] if settings[:root_prefix]
parts << settings[:version] if settings[:version]

uses_path_versioning = settings[:version] && settings[:version_options][:using] == :path
namespace_is_empty = namespace && (namespace.to_s =~ /^\s*$/ || namespace.to_s == '/')
path_is_empty = path && (path.to_s =~ /^\s*$/ || path.to_s == '/')

parts << ':version' if uses_path_versioning
if !uses_path_versioning || (!namespace_is_empty || !path_is_empty)
if ! settings[:version] || ! namespace_is_empty || ! path_is_empty
parts << namespace.to_s if namespace
parts << path.to_s if path && '/' != path
format_suffix = '(.:format)'
else
format_suffix = '(/.:format)'
end

Rack::Mount::Utils.normalize_path(parts.join('/') + format_suffix)
end

Expand Down
1 change: 0 additions & 1 deletion lib/grape/middleware/versioner/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def default_options
def before
paramkey = options[:parameter]
potential_version = request.params[paramkey]

unless potential_version.nil?
if options[:versions] && !options[:versions].include?(potential_version)
throw :error, :status => 404, :message => "404 API Version Not Found", :headers => {'X-Cascade' => 'pass'}
Expand Down
26 changes: 14 additions & 12 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,16 @@ def app; subject end
end

describe 'root routes should work with' do
before do
def subject.enable_root_route!
self.get("/") {"root"}
end
end

after do
last_response.body.should eql 'root'
end

describe 'path versioned APIs' do
before do
subject.version 'v1', :using => :path
subject.enable_root_route!
subject.get "/" do
"root"
end
end

it 'without a format' do
Expand All @@ -217,20 +213,26 @@ def subject.enable_root_route!

it 'header versioned APIs' do
subject.version 'v1', :using => :header, :vendor => 'test'
subject.enable_root_route!
subject.get "/" do
"root"
end

versioned_get "/", "v1", :using => :header
end

it 'param versioned APIs' do
subject.version 'v1', :using => :param
subject.enable_root_route!
subject.get "/" do
"root"
end

versioned_get "/", "v1", :using => :param
end

it 'unversioned APIs' do
subject.enable_root_route!
subject.get "/" do
"root"
end

get "/"
end
Expand Down Expand Up @@ -1132,8 +1134,8 @@ def self.call(object, env)
end
it 'sets route paths' do
subject.routes.size.should >= 2
subject.routes[0].route_path.should == "/:version/version(.:format)"
subject.routes[1].route_path.should == "/p/:version/n1/n2/version(.:format)"
subject.routes[0].route_path.should == "/v1/version(.:format)"
subject.routes[1].route_path.should == "/p/v2/n1/n2/version(.:format)"
end
it 'sets route versions' do
subject.routes[0].route_version.should == 'v1'
Expand Down
6 changes: 2 additions & 4 deletions spec/shared/versioning_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
versioned_get '/awesome', 'v3', macro_options
last_response.status.should eql 404
end

context 'with different versions for the same endpoint' do
context 'without a prefix' do
it 'allows the same endpoint to be implemented' do
Expand All @@ -77,7 +76,6 @@
last_response.body.should == 'version v1'
end
end

context 'with a prefix' do
it 'allows the same endpoint to be implemented' do
subject.prefix 'api'
Expand All @@ -92,10 +90,10 @@
end
end

versioned_get '/api/version', 'v2', macro_options
versioned_get '/version', 'v2', macro_options.merge({ :prefix => 'api' })
last_response.status.should == 200
last_response.body.should == 'v2'
versioned_get '/api/version', 'v1', macro_options
versioned_get '/version', 'v1', macro_options.merge({ :prefix => 'api' })
last_response.status.should == 200
last_response.body.should == 'version v1'
end
Expand Down

0 comments on commit 79872d5

Please sign in to comment.