2015-12-31 09:19:13 -05:00
|
|
|
module API
|
|
|
|
# Projects variables API
|
|
|
|
class Variables < Grape::API
|
|
|
|
before { authenticate! }
|
2016-02-01 17:58:04 -05:00
|
|
|
before { authorize! :admin_build, user_project }
|
2015-12-31 09:19:13 -05:00
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
# Get project variables
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# page (optional) - The page number for pagination
|
|
|
|
# per_page (optional) - The value of items per page to show
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/variables
|
|
|
|
get ':id/variables' do
|
|
|
|
variables = user_project.variables
|
|
|
|
present paginate(variables), with: Entities::Variable
|
|
|
|
end
|
|
|
|
|
2016-01-13 12:47:39 -05:00
|
|
|
# Get specific variable of a project
|
2015-12-31 09:19:13 -05:00
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
2016-01-07 07:49:38 -05:00
|
|
|
# key (required) - The `key` of variable
|
2015-12-31 10:25:49 -05:00
|
|
|
# Example Request:
|
2016-01-07 07:49:38 -05:00
|
|
|
# GET /projects/:id/variables/:key
|
|
|
|
get ':id/variables/:key' do
|
|
|
|
key = params[:key]
|
2016-01-13 06:47:11 -05:00
|
|
|
variable = user_project.variables.find_by(key: key.to_s)
|
2015-12-31 09:19:13 -05:00
|
|
|
|
2016-01-13 06:47:11 -05:00
|
|
|
return not_found!('Variable') unless variable
|
2015-12-31 11:03:11 -05:00
|
|
|
|
2016-01-13 06:47:11 -05:00
|
|
|
present variable, with: Entities::Variable
|
2015-12-31 09:19:13 -05:00
|
|
|
end
|
2015-12-31 10:25:49 -05:00
|
|
|
|
2015-12-31 16:30:07 -05:00
|
|
|
# Create a new variable in project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
2016-01-07 07:49:38 -05:00
|
|
|
# key (required) - The key of variable
|
|
|
|
# value (required) - The value of variable
|
2015-12-31 16:30:07 -05:00
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/variables
|
|
|
|
post ':id/variables' do
|
|
|
|
required_attributes! [:key, :value]
|
|
|
|
|
|
|
|
variable = user_project.variables.create(key: params[:key], value: params[:value])
|
|
|
|
|
2016-01-13 06:47:11 -05:00
|
|
|
if variable.valid?
|
|
|
|
present variable, with: Entities::Variable
|
|
|
|
else
|
|
|
|
render_validation_error!(variable)
|
|
|
|
end
|
2015-12-31 16:30:07 -05:00
|
|
|
end
|
|
|
|
|
2015-12-31 10:25:49 -05:00
|
|
|
# Update existing variable of a project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
2016-01-07 07:49:38 -05:00
|
|
|
# key (optional) - The `key` of variable
|
|
|
|
# value (optional) - New value for `value` field of variable
|
2015-12-31 10:25:49 -05:00
|
|
|
# Example Request:
|
2016-01-07 07:49:38 -05:00
|
|
|
# PUT /projects/:id/variables/:key
|
|
|
|
put ':id/variables/:key' do
|
2016-01-13 06:47:11 -05:00
|
|
|
variable = user_project.variables.find_by(key: params[:key].to_s)
|
2015-12-31 10:25:49 -05:00
|
|
|
|
2015-12-31 11:03:11 -05:00
|
|
|
return not_found!('Variable') unless variable
|
|
|
|
|
2016-01-13 06:47:11 -05:00
|
|
|
attrs = attributes_for_keys [:value]
|
|
|
|
if variable.update(attrs)
|
|
|
|
present variable, with: Entities::Variable
|
|
|
|
else
|
|
|
|
render_validation_error!(variable)
|
|
|
|
end
|
2015-12-31 10:25:49 -05:00
|
|
|
end
|
2015-12-31 10:56:03 -05:00
|
|
|
|
|
|
|
# Delete existing variable of a project
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
2016-01-07 07:49:38 -05:00
|
|
|
# key (required) - The ID of a variable
|
2016-01-13 12:47:39 -05:00
|
|
|
# Example Request:
|
2016-01-07 07:49:38 -05:00
|
|
|
# DELETE /projects/:id/variables/:key
|
|
|
|
delete ':id/variables/:key' do
|
2016-01-13 06:47:11 -05:00
|
|
|
variable = user_project.variables.find_by(key: params[:key].to_s)
|
2015-12-31 11:03:11 -05:00
|
|
|
|
|
|
|
return not_found!('Variable') unless variable
|
2015-12-31 10:56:03 -05:00
|
|
|
variable.destroy
|
2015-12-31 16:30:07 -05:00
|
|
|
|
|
|
|
present variable, with: Entities::Variable
|
2015-12-31 10:56:03 -05:00
|
|
|
end
|
2015-12-31 09:19:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|