Class: MIME::Content::Multipart::Base

Inherits:
MIME::Content::Media show all
Defined in:
lib/safrano/multipart.rb

Overview

base class for multipart mixed, related, digest etc

Defined Under Namespace

Classes: Parser

Instance Attribute Summary collapse

Attributes inherited from MIME::Content::Media

#parser

Attributes inherited from Media

#content, #ct, #hd, #level, #response

Instance Method Summary collapse

Constructor Details

#initialize(boundary) ⇒ Base

Returns a new instance of Base.



325
326
327
328
329
330
# File 'lib/safrano/multipart.rb', line 325

def initialize(boundary)
  @boundary = boundary
  @hd = {}
  @content_id_references = nil
  @parser = Parser.new(self)
end

Instance Attribute Details

#boundaryObject (readonly)

Parser



323
324
325
# File 'lib/safrano/multipart.rb', line 323

def boundary
  @boundary
end

Instance Method Details

#==(other) ⇒ Object



332
333
334
# File 'lib/safrano/multipart.rb', line 332

def ==(other)
  (@boundary == other.boundary) && (@content == other.content)
end

#each_changesetObject



336
337
338
339
340
341
342
# File 'lib/safrano/multipart.rb', line 336

def each_changeset
  return unless @level.zero?

  @content.each do |part|
    yield part if part.is_a? MIME::Content::Multipart::Base
  end
end

#get_failed_changeset_response(_xeption) ⇒ Object



402
403
404
405
406
407
408
409
410
# File 'lib/safrano/multipart.rb', line 402

def get_failed_changeset_response(_xeption)
  @response = MIME::Content::Application::HttpResp.new
  @response.status = '400'
  @response.content = [{ 'odata.error' =>
                       { 'message' =>
                         'Bad Request: Failed changeset ' } }.to_json]
  @response.hd = Safrano::CT_JSON
  @response
end

#get_mult_resp(full_req) ⇒ Object



371
372
373
374
# File 'lib/safrano/multipart.rb', line 371

def get_mult_resp(full_req)
  get_response(full_req)
  [202, @response.hd, @response.unparse_bodyonly]
end

#get_response(full_req) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/safrano/multipart.rb', line 376

def get_response(full_req)
  @response = self.class.new(::SecureRandom.uuid)
  @response.set_multipart_header
  if @level == 1 # changeset need their own global transaction
    # the change requests that are part of it have @level==2
    # and will be flagged with in_changeset=true
    # and this will finally be used to skip the transaction
    # of the changes

    full_req.db.transaction do
      begin
        @response.content = @content.map { |part| part.get_response(full_req) }
      rescue Sequel::Rollback => e
        # one of the changes of the changeset has failed
        # --> provide a dummy empty response for the change-parts
        # then transmit the Rollback to Sequel
        get_failed_changeset_response(e)
        raise
      end
    end
  else
    @response.content = @content.map { |part| part.get_response(full_req) }
  end
  @response
end

#prepare_content_id_refsObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/safrano/multipart.rb', line 344

def prepare_content_id_refs
  if @level.zero?
    each_changeset(&:prepare_content_id_refs)
  elsif @level == 1

    @content_id_references = {}

    @content.each do |part|
      next unless part.is_a? MIME::Content::Application::Http

      case part.content.http_method
      when 'POST', 'PUT'
        if (ctid = part.hd['content-id'])
          part.content.content_id = ctid
          @content_id_references[ctid] = nil
        end
      end
      part.content.content_id_references = @content_id_references
    end

  end
end

#set_multipart_headerObject



367
368
369
# File 'lib/safrano/multipart.rb', line 367

def set_multipart_header
  @hd[CTT_TYPE_LC] = "#{Safrano::MP_MIXED}; boundary=#{@boundary}"
end

#unparseObject



412
413
414
415
416
417
418
419
420
421
# File 'lib/safrano/multipart.rb', line 412

def unparse
  b = +String.new
  b << "#{Safrano::CONTENT_TYPE}: #{@hd[CTT_TYPE_LC]}#{CRLF}"

  # warning: duplicated code with below
  b << crbdcr = "#{CRLF}--#{@boundary}#{CRLF}"
  b << @content.map(&:unparse).join(crbdcr)
  b << "#{CRLF}--#{@boundary}--#{CRLF}"
  b
end

#unparse_bodyonlyObject



423
424
425
426
427
428
429
430
# File 'lib/safrano/multipart.rb', line 423

def unparse_bodyonly
  b = +String.new
  # warning: duplicated code with above
  b << crbdcr = "#{CRLF}--#{@boundary}#{CRLF}"
  b << @content.map(&:unparse).join(crbdcr)
  b << "#{CRLF}--#{@boundary}--#{CRLF}"
  b
end