API: extracted helper method to validate required parameters, code clean up

Added a helper method to check if required parameters are given in an API call. Can be used
to return a `400 Bad Request` return code if a required attribute is missing.
Code clean up and fixed tests.
This commit is contained in:
Sebastian Ziebell 2013-02-27 17:50:30 +01:00
parent 43d7596030
commit 7499f65014
10 changed files with 35 additions and 42 deletions

View file

@ -41,6 +41,17 @@ module Gitlab
abilities.allowed?(object, action, subject)
end
# Checks the occurrences of required attributes, each attribute must be present in the params hash
# or a Bad Request error is invoked.
#
# Parameters:
# keys (required) - A hash consisting of keys that must be present
def required_attributes!(keys)
keys.each do |key|
bad_request!(key) unless params[key].present?
end
end
def attributes_for_keys(keys)
attrs = {}
keys.each do |key|