Skip to content

Commit

Permalink
Merge pull request #275 from walski/fix-86
Browse files Browse the repository at this point in the history
Fixes #86
  • Loading branch information
dblock committed Nov 14, 2012
2 parents d85c2b3 + e3c94ba commit 927629e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
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

0 comments on commit 927629e

Please sign in to comment.