Skip to content

Commit c0a7255

Browse files
committed
Added support for .json :format in route matching.
1 parent cd122ad commit c0a7255

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/grape/api.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,13 @@ def compile_path(path)
222222
parts = []
223223
parts << prefix if prefix
224224
parts << ':version' if version
225-
parts << namespace if namespace
226-
parts << path
225+
parts << namespace.to_s if namespace
226+
parts << path.to_s if path && '/' != path
227+
parts.last << '(.:format)'
227228
Rack::Mount::Utils.normalize_path(parts.join('/'))
228229
end
229230
end
230231

231232
reset!
232233
end
233-
end
234+
end

spec/grape/api_spec.rb

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def app; subject end
9292
subject.version :v1
9393

9494
subject.namespace :awesome do
95-
compile_path('hello').should == '/rad/:version/awesome/hello'
95+
compile_path('hello').should == '/rad/:version/awesome/hello(.:format)'
9696
end
9797
end
9898

@@ -117,9 +117,9 @@ def app; subject end
117117
it 'should be callable with nil just to push onto the stack' do
118118
subject.namespace do
119119
version 'v2'
120-
compile_path('hello').should == '/:version/hello'
120+
compile_path('hello').should == '/:version/hello(.:format)'
121121
end
122-
subject.send(:compile_path, 'hello').should == '/hello'
122+
subject.send(:compile_path, 'hello').should == '/hello(.:format)'
123123
end
124124

125125
%w(group resource resources).each do |als|
@@ -159,6 +159,26 @@ def app; subject end
159159
get '/def'
160160
last_response.body.should == 'foo'
161161
end
162+
163+
it 'should allow for format' do
164+
subject.get("/abc") do
165+
"json"
166+
end
167+
168+
get '/abc.json'
169+
last_response.body.should == '"json"'
170+
end
171+
172+
it 'should allow for format in namespace with no path' do
173+
subject.namespace :abc do
174+
get do
175+
"json"
176+
end
177+
end
178+
179+
get '/abc.json'
180+
last_response.body.should == '"json"'
181+
end
162182

163183
it 'should allow for multiple verbs' do
164184
subject.route([:get, :post], '/abc') do
@@ -340,4 +360,4 @@ def two
340360
last_response.status.should == 200
341361
end
342362
end
343-
end
363+
end

0 commit comments

Comments
 (0)