Skip to content

Commit

Permalink
Merge pull request ruby-grape#195 from ppadron/master
Browse files Browse the repository at this point in the history
Updating "Describing and Inspecting an API" to reflect actual behavior
  • Loading branch information
dblock committed Jul 3, 2012
2 parents eb1055e + ef39b39 commit 575e6fc
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -650,13 +650,13 @@ Parameters can also be tagged to the method declaration itself.

``` ruby
class StringAPI < Grape::API
get "split/:string", { :params => [ "token" ], :optional_params => [ "limit" ] } do
get "split/:string", { :params => { "token" => "a token" }, :optional_params => { "limit" => "the limit" } } do
params[:string].split(params[:token], (params[:limit] || 0))
end
end

StringAPI::routes[0].route_params # yields an array [ "string", "token" ]
StringAPI::routes[0].route_optional_params # yields an array [ "limit" ]
StringAPI::routes[0].route_params # yields a hash {"string" => "", "token" => "a token"}
StringAPI::routes[0].route_optional_params # yields a hash {"limit" => "the limit"}
```

It's possible to retrieve the information about the current route from within an API call with `route`.
Expand All @@ -670,6 +670,33 @@ class MyAPI < Grape::API
end
```

You can use this information to create a helper that will check if the request has
all required parameters:

``` ruby
class MyAPI < Grape::API

helpers do
def validate_request!
# skip validation if no parameter is declared
return unless route.route_params
route.route_params.each do |k, v|
if !params.has_key? k
error!("Missing field: #{k}", 400)
end
end
end
end

before { validate_request! }

desc "creates a new item resource", :params => { :name => 'name is a required parameter' }
post :items do
...
end
end
```

## Anchoring

Grape by default anchors all request paths, which means that the request URL
Expand Down

0 comments on commit 575e6fc

Please sign in to comment.