Skip to content

Commit

Permalink
Tradeoff minor performance hit for readability.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Dec 28, 2012
2 parents a96e18a + cdcb95e commit 0f3f14d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 13 additions & 3 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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 = {}
Expand Down
12 changes: 12 additions & 0 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

1 comment on commit 0f3f14d

@aiwilliams
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks @dblock for getting this merged.

Please sign in to comment.