From 21e5b7886dfc97f0a828c4b7d1545c1083d51522 Mon Sep 17 00:00:00 2001 From: Pedro Padron Date: Tue, 3 Jul 2012 03:06:17 -0300 Subject: [PATCH 1/3] parameters must be specified with a Hash instead of an Array --- README.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.markdown b/README.markdown index 5136252908..310cefe5b0 100644 --- a/README.markdown +++ b/README.markdown @@ -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`. From c0bf125e00816cc382e06350866c28c4fd97776f Mon Sep 17 00:00:00 2001 From: Pedro Padron Date: Tue, 3 Jul 2012 03:07:55 -0300 Subject: [PATCH 2/3] adding parameter validation example to the README file --- README.markdown | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.markdown b/README.markdown index 310cefe5b0..c84c2c7ae7 100644 --- a/README.markdown +++ b/README.markdown @@ -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 + if route.route_params.nil?; return end + 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 From ef39b3967b76a5817756c02798ee759d13958de2 Mon Sep 17 00:00:00 2001 From: Pedro Padron Date: Tue, 3 Jul 2012 14:54:49 -0300 Subject: [PATCH 3/3] README: making request validation example more readable --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index c84c2c7ae7..c7ea07f29b 100644 --- a/README.markdown +++ b/README.markdown @@ -679,7 +679,7 @@ class MyAPI < Grape::API helpers do def validate_request! # skip validation if no parameter is declared - if route.route_params.nil?; return end + return unless route.route_params route.route_params.each do |k, v| if !params.has_key? k error!("Missing field: #{k}", 400)