diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 92819499d5..8a12bc8ac7 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -7,6 +7,7 @@ * [#295](https://github.com/intridea/grape/issues/295): Storing original API source block in endpoint's `source` attribute - [@dblock](https://github.com/dblock). * [#298](https://github.com/intridea/grape/pull/298): Fix: subsequent calls to `body_params` would fail due to IO read - [@justinmcp](https://github.com/justinmcp). * [#301](https://github.com/intridea/grape/issues/301): Fix: symbol memory leak in cookie and formatter middleware - [@dblock](https://github.com/dblock). +* [#300](https://github.com/intridea/grape/issues/300): Fix Grape::API.routes to include mounted api routes - [@aiwilliams](https://github.com/aiwilliams). * Your contribution here. 0.2.3 (24/12/2012) diff --git a/lib/grape/endpoint.rb b/lib/grape/endpoint.rb index 0237a8d670..90fee53a28 100644 --- a/lib/grape/endpoint.rb +++ b/lib/grape/endpoint.rb @@ -56,12 +56,12 @@ def initialize(settings, options = {}, &block) end def routes - @routes ||= prepare_routes + @routes ||= endpoints ? endpoints.collect(&:routes).flatten : prepare_routes end def mount_in(route_set) - if options[:app] && options[:app].respond_to?(:endpoints) - options[:app].endpoints.each{|e| e.mount_in(route_set)} + if endpoints + endpoints.each{|e| e.mount_in(route_set)} else routes.each do |route| route_set.add_route(self, { @@ -347,6 +347,16 @@ def route protected + # Return the collection of endpoints within this endpoint. + # This is the case when an Grape::API mounts another Grape::API. + def endpoints + if options[:app] && options[:app].respond_to?(:endpoints) + options[:app].endpoints + else + nil + end + end + def run(env) @env = env @header = {} diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 969d3af1e3..72da6eb775 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -1399,6 +1399,18 @@ def self.call(object, env) last_response.status.should eql 202 last_response.body.should == 'rescued from doh!' end + + it 'collects the routes of the mounted api' do + subject.namespace :cool do + app = Class.new(Grape::API) + app.get('/awesome') {} + app.post('/sauce') {} + mount app + end + subject.routes.size.should == 2 + subject.routes.first.route_path.should =~ /\/cool\/awesome/ + subject.routes.last.route_path.should =~ /\/cool\/sauce/ + end end end