intridea/grape — pull request #126 by dblock (2012-01-25 14:39:45+00:00)
Title: Added access to `route` from within an API.

This loops the loop with API introspection by making route information available within an API call.

For example, we can now declare what the API takes for `:sort` values and disallow anything that's not in the list.

class MyAPI < Grape::API
desc "Returns sorted stuff.",
{ :params =>
{ :sort => [ "name", "created_at" ] }
}
get "/stuff"
allowed_sorts = route.route_params[:sort]
error!("Can't sort by #{params[:sort]}, allowed sorts are #{allowed_sorts}.", 400) unless allowed_sorts.include?(params[:sort])
Stuff.desc(params[:sort])
end
end

I thought about calling this `route_info`, but decided against it since you get back the actual route

@jch or @mbleigh pls take a look