class Mhtml::RootDocument

Constants

BOUNDARY_PREFIX

Attributes

boundary[RW]
sub_docs[RW]

Public Class Methods

new(str = nil) click to toggle source
Calls superclass method Mhtml::Document::new
# File lib/mhtml/root_document.rb, line 7
def initialize(str = nil)
  @sub_docs = []
  super(str)
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method Mhtml::Document#==
# File lib/mhtml/root_document.rb, line 12
def ==(other)
  super(other) && @boundary == other.boundary && @sub_docs == other.sub_docs
end
boundary_str() click to toggle source
# File lib/mhtml/root_document.rb, line 32
def boundary_str
  "#{Mhtml::LINE_BREAK}#{BOUNDARY_PREFIX}#{@boundary}#{Mhtml::LINE_BREAK}"
end
last_boundary_str() click to toggle source
# File lib/mhtml/root_document.rb, line 36
def last_boundary_str
  "#{Mhtml::LINE_BREAK}#{BOUNDARY_PREFIX}#{@boundary}#{BOUNDARY_PREFIX}#{Mhtml::LINE_BREAK}"
end
on_subdoc_begin() click to toggle source
# File lib/mhtml/root_document.rb, line 16
def on_subdoc_begin
  @subdoc_begin_proc = Proc.new
end
on_subdoc_body() click to toggle source
# File lib/mhtml/root_document.rb, line 24
def on_subdoc_body
  @subdoc_body_proc = Proc.new
end
on_subdoc_complete() click to toggle source
# File lib/mhtml/root_document.rb, line 28
def on_subdoc_complete
  @subdoc_complete_proc = Proc.new
end
on_subdoc_header() click to toggle source
# File lib/mhtml/root_document.rb, line 20
def on_subdoc_header
  @subdoc_header_proc = Proc.new
end
to_s() click to toggle source

for testing only = no spec implemented

Calls superclass method Mhtml::Document#to_s
# File lib/mhtml/root_document.rb, line 41
def to_s
  doc_sep = Mhtml::DOUBLE_LINE_BREAK + BOUNDARY_PREFIX + @boundary + 
    Mhtml::LINE_BREAK
  super + doc_sep + @sub_docs.join(doc_sep)
end

Private Instance Methods

create_chunked_subdoc() click to toggle source
# File lib/mhtml/root_document.rb, line 113
def create_chunked_subdoc
  @chunked_sub_doc = Document.new
  @chunked_sub_doc.root_doc = self

  @chunked_sub_doc.on_header do |header| 
    @subdoc_header_proc.call(header) unless @subdoc_header_proc.nil?
  end

  @chunked_sub_doc.on_body do |body|
    @subdoc_body_proc.call(body) unless @subdoc_body_proc.nil?
  end
end
handle_body(inst, data) click to toggle source
Calls superclass method Mhtml::Document#handle_body
# File lib/mhtml/root_document.rb, line 49
def handle_body(inst, data)
  maybe_create_header
  boundary = boundary_str

  unless @split.nil?
    data = @split + data
    @split = nil
  end

  parts = data.split(boundary)
  
  unless @body_read
    @body_read = parts.length > 1
    super(inst, parts.shift)
  end

  parts.each_with_index do |part, i|
    end_boundary_pos = part.rindex(last_boundary_str)
    is_last_subdoc = !end_boundary_pos.nil?
    part = part[0..(end_boundary_pos - 1)] if is_last_subdoc

    if @chunked
      is_last_part = i + 1 == parts.length
      handle_chunked_body(part, is_last_part, is_last_subdoc)
    else
      sub_doc = Document.new(part)
      sub_doc.root_doc = self
      @sub_docs << sub_doc
    end
  end
end
handle_chunked_body(chunk, is_last_part, is_last_subdoc) click to toggle source
# File lib/mhtml/root_document.rb, line 81
def handle_chunked_body(chunk, is_last_part, is_last_subdoc)
  if @chunked_sub_doc.nil?
    create_chunked_subdoc
    @subdoc_begin_proc.call unless @subdoc_begin_proc.nil?
  end

  if is_last_part
    split_idx = chunk.rindex_of_split(boundary_str)

    if split_idx.nil?
      quoted_matches = chunk.match(/=[0-9A-F\r\n]{0,2}\Z/)

      unless quoted_matches.nil?
        split_idx = chunk.length - quoted_matches[0].length + 1
      end              
    end

    unless split_idx.nil?
      @split = chunk[split_idx..(chunk.length - 1)]
      chunk = chunk[0..(split_idx - 1)]
    end
  end

  @chunked_sub_doc << chunk

  unless is_last_part && !is_last_subdoc
    @sub_docs << @chunked_sub_doc
    @chunked_sub_doc = nil
    @subdoc_complete_proc.call unless @subdoc_complete_proc.nil?
  end
end