Module: Cyclid::API::Organizations::Stages
- Defined in:
- app/cyclid/controllers/organizations/stages.rb
Overview
API endpoints for Organization Stages
Defined Under Namespace
Modules: Helpers
Organizations collapse
-
#GET(/organizations/: organization/stages) ⇒ Object
Get all of the stages.
-
#GET(/organizations/: organization/stages/:stage) ⇒ Object
Get the details of the specified stage within the organization.
-
#GET(/organizations/: organization/stages/:stage/:version) ⇒ Object
Get the details of the specified stage within the organization.
-
#POST(/organizations/: organization/stages) ⇒ 400, ...
Create a new stage.
Class Method Summary collapse
-
.registered(app) ⇒ Object
Sinatra callback.
Class Method Details
.registered(app) ⇒ Object
Sinatra callback
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'app/cyclid/controllers/organizations/stages.rb', line 81 def self.registered(app) include Errors::HTTPErrors # Get all of the stages. app.get do (params[:name], Operations::READ) org = Organization.find_by(name: params[:name]) halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \ if org.nil? # Convert each Stage to a hash & sanitize it stages = org.stages.all.map do |stage| stage_hash = sanitize_stage(stage.serializable_hash) # Santize each step in this stage steps = stage.steps.map do |step| sanitize_step(step.serializable_hash) end stage_hash['steps'] = steps stage_hash end return stages.to_json end # Create a new stage. app.post do (params[:name], Operations::ADMIN) payload = parse_request_body Cyclid.logger.debug payload org = Organization.find_by(name: params[:name]) halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \ if org.nil? halt_with_json_response(400, INVALID_JSON, 'stage does not define any steps') \ unless payload.key? 'steps' # Ensure that a stage with the same name & version doesn't already exist version = payload['version'] || '0.0.1' halt_with_json_response(409, \ DUPLICATE, \ 'A stage with that name and version already exists') \ if org.stages.exists?(name: payload['name'], version: version) begin stage = Stage.new stage.name = payload['name'] stage.version = payload['version'] if payload.key? 'version' stage.organization = org # Create the steps & store their serialized form stage.steps << create_steps(payload['steps']) stage.save! rescue ActiveRecord::ActiveRecordError, \ ActiveRecord::UnknownAttributeError => ex Cyclid.logger.debug ex. halt_with_json_response(400, INVALID_JSON, ex.) end end # Returns every defined version for the given stage. app.get '/:stage' do (params[:name], Operations::READ) org = Organization.find_by(name: params[:name]) halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \ if org.nil? # There may be multiple versions of the same stage, so we need to # find every instance of the given stage, convert each Stage to a # hash & sanitize it stages = org.stages.where(name: params[:stage]).map do |stage| stage_hash = sanitize_stage(stage.serializable_hash) # Santize each step in this stage steps = stage.steps.map do |step| sanitize_step(step.serializable_hash) end stage_hash['steps'] = steps stage_hash end halt_with_json_response(404, INVALID_STAGE, 'stage does not exist') \ if stages.empty? return stages.to_json end # Get the details of the specified stage within the organization. # Returns the specified version of the given stage. app.get '/:stage/:version' do (params[:name], Operations::READ) org = Organization.find_by(name: params[:name]) halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \ if org.nil? stage = org.stages.find_by(name: params[:stage], version: params[:version]) halt_with_json_response(404, INVALID_STAGE, 'stage does not exist') \ if stage.nil? # Sanitize the stage stage_hash = sanitize_stage(stage.serializable_hash) # Santize each step in this stage steps = stage.steps.map do |step| sanitize_step(step.serializable_hash) end stage_hash['steps'] = steps return stage_hash.to_json end app.helpers do include Helpers end end |
Instance Method Details
#GET(/organizations/: organization/stages) ⇒ Object
Get all of the stages.
|
# File 'app/cyclid/controllers/organizations/stages.rb', line 27
|
#GET(/organizations/: organization/stages/:stage) ⇒ Object
Get the details of the specified stage within the organization. Returns every defined version for the given stage.
|
# File 'app/cyclid/controllers/organizations/stages.rb', line 56
|
#GET(/organizations/: organization/stages/:stage/:version) ⇒ Object
Get the details of the specified stage within the organization. Returns the specified version of the given stage.
|
# File 'app/cyclid/controllers/organizations/stages.rb', line 66
|
#POST(/organizations/: organization/stages) ⇒ 400, ...
Create a new stage.
|
# File 'app/cyclid/controllers/organizations/stages.rb', line 35
|