Class: Mhtml::Document
- Inherits:
-
Object
- Object
- Mhtml::Document
- Defined in:
- lib/mhtml/document.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#chunked ⇒ Object
readonly
Returns the value of attribute chunked.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#is_base_64 ⇒ Object
Returns the value of attribute is_base_64.
-
#is_quoted_printable ⇒ Object
Returns the value of attribute is_quoted_printable.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#root_doc ⇒ Object
Returns the value of attribute root_doc.
Instance Method Summary collapse
- #<<(chunk) ⇒ Object
- #==(other) ⇒ Object
- #header(key) ⇒ Object
-
#initialize(str = nil) ⇒ Document
constructor
A new instance of Document.
- #on_body ⇒ Object
- #on_header ⇒ Object
- #relative_file_path ⇒ Object
-
#to_s ⇒ Object
for testing only = no spec implemented.
Constructor Details
#initialize(str = nil) ⇒ Document
Returns a new instance of Document.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mhtml/document.rb', line 16 def initialize(str = nil) @chunked = !str.is_a?(String) @header_key = nil @header_value_lines = nil @is_quoted_printable = false @is_base_64 = 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. { |inst| (inst) } parser. { |inst| (inst) } end @parser.parse(@request, Mhtml::STATUS_LINE) unless @chunked @headers = [] @body = '' @parser.parse(@request, str) end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
8 9 10 |
# File 'lib/mhtml/document.rb', line 8 def body @body end |
#chunked ⇒ Object (readonly)
Returns the value of attribute chunked.
6 7 8 |
# File 'lib/mhtml/document.rb', line 6 def chunked @chunked end |
#encoding ⇒ Object
Returns the value of attribute encoding.
8 9 10 |
# File 'lib/mhtml/document.rb', line 8 def encoding @encoding end |
#file_path ⇒ Object
Returns the value of attribute file_path.
8 9 10 |
# File 'lib/mhtml/document.rb', line 8 def file_path @file_path end |
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/mhtml/document.rb', line 8 def headers @headers end |
#is_base_64 ⇒ Object
Returns the value of attribute is_base_64.
8 9 10 |
# File 'lib/mhtml/document.rb', line 8 def is_base_64 @is_base_64 end |
#is_quoted_printable ⇒ Object
Returns the value of attribute is_quoted_printable.
8 9 10 |
# File 'lib/mhtml/document.rb', line 8 def is_quoted_printable @is_quoted_printable end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
6 7 8 |
# File 'lib/mhtml/document.rb', line 6 def parser @parser end |
#root_doc ⇒ Object
Returns the value of attribute root_doc.
8 9 10 |
# File 'lib/mhtml/document.rb', line 8 def root_doc @root_doc end |
Instance Method Details
#<<(chunk) ⇒ Object
43 44 45 |
# File 'lib/mhtml/document.rb', line 43 def <<(chunk) @parser.parse(@request, chunk) end |
#==(other) ⇒ Object
47 48 49 50 |
# File 'lib/mhtml/document.rb', line 47 def ==(other) @headers == other.headers && @body.gsub(/\r\n/, "\n").strip == other.body.gsub(/\r\n/, "\n").strip end |
#header(key) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mhtml/document.rb', line 60 def header(key) header = nil @headers.each do |h| if h.key == key header = h break end end header end |
#on_body ⇒ Object
56 57 58 |
# File 'lib/mhtml/document.rb', line 56 def on_body @body_proc = Proc.new end |
#on_header ⇒ Object
52 53 54 |
# File 'lib/mhtml/document.rb', line 52 def on_header @headers_proc = Proc.new end |
#relative_file_path ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mhtml/document.rb', line 73 def relative_file_path return nil if @file_path.nil? return '.' if @file_path == @root_doc.file_path str = nil if !@root_doc.file_path.nil? && @file_path.start_with?(@root_doc.file_path) start = @root_doc.file_path.length str = @file_path[start..(@file_path.length - 1)] elsif @file_path.include?(':') start = @file_path.rindex(':') + 1 str = @file_path[start..(@file_path.length - 1)] else str = @file_path end str = str[1..(str.length - 1)] if str[0] == '/' str end |
#to_s ⇒ Object
for testing only = no spec implemented
97 98 99 |
# File 'lib/mhtml/document.rb', line 97 def to_s @headers.join(LINE_BREAK) + Mhtml::DOUBLE_LINE_BREAK + @body end |