Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #86 #275

Merged
merged 3 commits into from
Nov 14, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* [#265](https://github.com/intridea/grape/issues/264): Fix: The class ValidationError should be in the module "Grape::Exceptions". Fixes [#264](https://github.com/intridea/grape/issues/264) - [@thepumpkin1979](https://github.com/thepumpkin1979).
* [#269](https://github.com/intridea/grape/pull/269): Fix: LocalJumpError will not be raised when using explict return in API methods - [@simulacre](https://github.com/simulacre)
* [#86] (https://github.com/intridea/grape/issues/275): Fix Path based versioning not recognizing '/' route
* Your contribution here.

0.2.2
Expand Down
18 changes: 14 additions & 4 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,20 @@ def prepare_routes
def prepare_path(path)
parts = []
parts << settings[:root_prefix] if settings[:root_prefix]
parts << ':version' if settings[:version] && settings[:version_options][:using] == :path
parts << namespace.to_s if namespace
parts << path.to_s if path && '/' != path
Rack::Mount::Utils.normalize_path(parts.join('/') + '(.:format)')

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)
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

def namespace
Expand Down
47 changes: 47 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,53 @@ def app; subject end
last_response.body.should eql 'Created a Vote'
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!
end

it "without a format" do
versioned_get "/", "v1", :using => :path
end

it "with a format" do
get "/v1/.json"
end
end

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

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

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

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

it "unversioned APIs" do
subject.enable_root_route!

get "/"
end
end

it 'should allow for multiple paths' do
subject.get(["/abc", "/def"]) do
"foo"
Expand Down