Skip to content

Commit 339ac98

Browse files
author
Michael Bleigh
committed
Added first part of inherits, but will have to refactor compiling to happen after everything else before this will work.
1 parent 6c60b4c commit 339ac98

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/grape/api.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ def http_digest(options = {}, &block)
188188
def mount(mounts)
189189
mounts.each_pair do |app, path|
190190
next unless app.respond_to?(:call)
191+
192+
if app.is_a?(Class) && app < Grape::API
193+
app.inherit(settings_stack)
194+
end
195+
191196
route_set.add_route(app,
192197
path_info: compile_path(path, false)
193198
)
@@ -357,7 +362,12 @@ def build_endpoint(&block)
357362
def inherited(subclass)
358363
subclass.reset!
359364
end
360-
365+
366+
def inherit(other_stack)
367+
settings_stack.unshift *other_stack
368+
raise settings_stack.inspect
369+
end
370+
361371
def route_set
362372
@route_set ||= Rack::Mount::RouteSet.new
363373
end

spec/grape/api_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,5 +778,46 @@ class CommunicationError < RuntimeError; end
778778
last_response.body.should == 'MOUNTED'
779779
end
780780
end
781+
782+
context 'with another Grape API' do
783+
class ExampleProtectedAPI < Grape::API
784+
get '/protected' do
785+
"YOU'RE IN!"
786+
end
787+
end
788+
789+
class ExampleV1API < Grape::API
790+
get "/old" do
791+
"YOU'RE OLD! #{version}"
792+
end
793+
end
794+
795+
before do
796+
subject.scope :v1 do
797+
version :v1
798+
mount ExampleV1API => '/'
799+
end
800+
801+
subject.scope :authenticated do
802+
before do
803+
error!("Not Authorized", 401) unless params[:authorized] == 'true'
804+
end
805+
mount ExampleProtectedAPI => '/private'
806+
end
807+
end
808+
809+
it 'should be able to access the old API namespaced properly' do
810+
get '/v1/old'
811+
last_response.body.should == "YOU'RE OLD! v1"
812+
end
813+
814+
it 'should run before filters that are inherited' do
815+
get '/private/protected'
816+
last_response.status.should == 401
817+
get '/private/protected?authorized=true'
818+
last_response.status.should == 200
819+
last_response.body.should == "YOU'RE IN!"
820+
end
821+
end
781822
end
782823
end

0 commit comments

Comments
 (0)