Class: Mhtml::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/mhtml/document.rb

Direct Known Subclasses

RootDocument

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str = nil) ⇒ Document

Returns a new instance of Document.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mhtml/document.rb', line 8

def initialize(str = nil)
  @chunked = !str.is_a?(String)
  @header_key = nil
  @header_value_lines = nil
  @is_quoted_printable = false
  @encoding = nil

  @request = HttpParser::Parser.new_instance { |inst| inst.type = :response }

  @parser = HttpParser::Parser.new do |parser|
    parser.on_header_field { |inst, data| handle_header_field(inst, data) }
    parser.on_header_value { |inst, data| handle_header_value(inst, data) }
    parser.on_body { |inst, data| handle_body(inst, data) }
    parser.on_message_begin { |inst| handle_message_begin(inst) }
    parser.on_message_complete { |inst| handle_message_complete(inst) }
  end

  @parser.parse(@request, Mhtml::STATUS_LINE)

  unless @chunked
    @headers = []
    @body = ''
    @parser.parse(@request, str)
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



6
7
8
# File 'lib/mhtml/document.rb', line 6

def body
  @body
end

#chunkedObject (readonly)

Returns the value of attribute chunked.



5
6
7
# File 'lib/mhtml/document.rb', line 5

def chunked
  @chunked
end

#encodingObject

Returns the value of attribute encoding.



6
7
8
# File 'lib/mhtml/document.rb', line 6

def encoding
  @encoding
end

#headersObject

Returns the value of attribute headers.



6
7
8
# File 'lib/mhtml/document.rb', line 6

def headers
  @headers
end

#is_quoted_printableObject

Returns the value of attribute is_quoted_printable.



6
7
8
# File 'lib/mhtml/document.rb', line 6

def is_quoted_printable
  @is_quoted_printable
end

#parserObject (readonly)

Returns the value of attribute parser.



5
6
7
# File 'lib/mhtml/document.rb', line 5

def parser
  @parser
end

Instance Method Details

#<<(chunk) ⇒ Object



34
35
36
# File 'lib/mhtml/document.rb', line 34

def <<(chunk)
  @parser.parse(@request, chunk)
end

#==(other) ⇒ Object



38
39
40
41
# File 'lib/mhtml/document.rb', line 38

def ==(other)
  @headers == other.headers &&
    @body.gsub(/\r\n/, "\n").strip == other.body.gsub(/\r\n/, "\n").strip
end

#header(key) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mhtml/document.rb', line 51

def header(key)
  header = nil

  @headers.each do |h|
    if h.key == key
      header = h
      break
    end
  end

  header
end

#on_bodyObject



47
48
49
# File 'lib/mhtml/document.rb', line 47

def on_body
  @body_proc = Proc.new
end

#on_headerObject



43
44
45
# File 'lib/mhtml/document.rb', line 43

def on_header
  @headers_proc = Proc.new
end

#to_sObject

for testing only = no spec implemented



65
66
67
# File 'lib/mhtml/document.rb', line 65

def to_s
  @headers.join(LINE_BREAK) + Mhtml::DOUBLE_LINE_BREAK + @body
end