class CPEE::ModelManagement::OpenItem

Public Instance Methods

response() click to toggle source
# File lib/cpee-model-management/implementation.rb, line 437
def response
  where = @a[0] == :main ? '' : Riddl::Protocols::Utils::unescape(@r[-3])
  name   = File.basename(@r[-2],'.xml')
  insta  = @a[1]
  cock   = @a[2]
  views  = @a[3]
  force  = @a[4]
  models = @a[5]
  stage  = @p[0]&.value || 'draft'

  fname  = File.join(models,where,name + '.xml')

  inst = nil
  begin
    inst = if File.exists?(fname + '.active') && File.exists?(fname + '.active-uuid') && !force
      t = {
        'CPEE-INSTANCE-URL'  => File.read(fname + '.active'),
        'CPEE-INSTANCE-UUID' => File.read(fname + '.active-uuid')
      }
      status, result, headers = Riddl::Client.new(File.join(t['CPEE-INSTANCE-URL'],'properties','state')).get

      if status && status >= 200 && status < 300 && (result[0].value == 'finished' || result[0].value == 'abandoned')
        force = true
        raise
      end
      status, result, headers = Riddl::Client.new(File.join(t['CPEE-INSTANCE-URL'],'properties','attributes','uuid')).get
      if status && status >= 200 && status < 300
        t['CPEE-INSTANCE-UUID'] == result[0].value ? t : nil
      else
        nil
      end
    end || begin
      status, result, headers = Riddl::Client.new(File.join(insta,'xml')).post [
        Riddl::Parameter::Simple.new('behavior','fork_ready'),
        Riddl::Parameter::Complex.new('xml','application/xml',File.read(fname))
      ] rescue nil
      if status && status >= 200 && status < 300
        JSON::parse(result[0].value.read).tap do |t|
          File.write(File.join(fname + '.active'),t['CPEE-INSTANCE-URL'])
          File.write(File.join(fname + '.active-uuid'),t['CPEE-INSTANCE-UUID'])
        end
      else
        nil
      end
    end
  rescue
    retry
  end

  if inst.nil?
    @status = 400
    return Riddl::Parameter::Complex.new('nope','text/plain','No longer exists.')
  else
    insturl = inst['CPEE-INSTANCE-URL']
    @status = 302
    @headers << Riddl::Header.new('Location',cock[stage] + insturl)
  end
  nil
end