Route-specific hooks with Grape

Using Grape for building APIs with Ruby, it's handy to have hooks firing for only a set of endpoints.

This is currently un-documented, but it turns out hooks nested under namespace (or one of its many aliases) will only apply to that scope.

Given a running Api defined with:

class Api < Grape::API
  before { puts 'hi' }
  get('/foo') {'foo' }

  namespace do
    before { puts 'sup' }
    get('/bar') { 'bar' }

Hitting foo, and then bar endpoints in succession results in:

hi - - [02/Dec/2015:23:16:43 -0500] "GET /foo HTTP/1.1" 200 3 0.0010
sup - - [02/Dec/2015:23:16:50 -0500] "GET /bar HTTP/1.1" 200 3 0.0015