module API # Projects variables API class Variables < Grape::API before { authenticate! } before { authorize_admin_project } 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 # Get specifica variable of a project # # Parameters: # id (required) - The ID of a project # key (required) - The `key` of variable # Example Request: # GET /projects/:id/variables/:key get ':id/variables/:key' do key = params[:key] variables = user_project.variables.where(key: key) return not_found!('Variable') if variables.empty? present variables.first, with: Entities::Variable end # Create a new variable in project # # Parameters: # id (required) - The ID of a project # key (required) - The key of variable # value (required) - The value of variable # 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]) return render_validation_error!(variable) unless variable.valid? variable.save! present variable, with: Entities::Variable end # Update existing variable of a project # # Parameters: # id (required) - The ID of a project # key (optional) - The `key` of variable # value (optional) - New value for `value` field of variable # Example Request: # PUT /projects/:id/variables/:key put ':id/variables/:key' do variable = user_project.variables.where(key: params[:key]).first return not_found!('Variable') unless variable variable.value = params[:value] variable.save! present variable, with: Entities::Variable end # Delete existing variable of a project # # Parameters: # id (required) - The ID of a project # key (required) - The ID of a variable # Exanoke Reqyest: # DELETE /projects/:id/variables/:key delete ':id/variables/:key' do variable = user_project.variables.where(key: params[:key]).first return not_found!('Variable') unless variable variable.destroy present variable, with: Entities::Variable end end end end