Class: MultipartParser::Reader
- Inherits:
-
Object
- Object
- MultipartParser::Reader
- Defined in:
- lib/multipart_parser/reader.rb
Overview
A more high level interface to MultipartParser.
Defined Under Namespace
Classes: Part
Class Method Summary collapse
-
.extract_boundary_value(content_type) ⇒ Object
Extracts a boundary value from a Content-Type header.
Instance Method Summary collapse
-
#ended? ⇒ Boolean
Returns true if the parser has finished parsing.
-
#initialize(boundary) ⇒ Reader
constructor
Initializes a MultipartReader, that will read a request with the given boundary value.
-
#on_error(&callback) ⇒ Object
Sets a code block to call when a parser error occurs.
-
#on_part(&callback) ⇒ Object
Sets to a code block to call when part headers have been parsed.
-
#write(buffer) ⇒ Object
Write data from the given buffer (String) into the reader.
Constructor Details
#initialize(boundary) ⇒ Reader
Initializes a MultipartReader, that will read a request with the given boundary value.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/multipart_parser/reader.rb', line 11 def initialize(boundary) @parser = Parser.new @parser.init_with_boundary(boundary) @header_field = '' @header_value = '' @part = nil @ended = false @on_error = nil @on_part = nil init_parser_callbacks end |
Class Method Details
.extract_boundary_value(content_type) ⇒ Object
Extracts a boundary value from a Content-Type header. Note that it is the header value you provide here. Raises NotMultipartError if content_type is invalid.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/multipart_parser/reader.rb', line 54 def self.extract_boundary_value(content_type) if content_type =~ /multipart/i if match = (content_type =~ /boundary=(?:"([^"]+)"|([^;]+))/i) $1 || $2 else raise NotMultipartError.new("No multipart boundary") end else raise NotMultipartError.new("Not a multipart content type!") end end |
Instance Method Details
#ended? ⇒ Boolean
Returns true if the parser has finished parsing
25 26 27 |
# File 'lib/multipart_parser/reader.rb', line 25 def ended? @ended end |
#on_error(&callback) ⇒ Object
Sets a code block to call when a parser error occurs.
37 38 39 |
# File 'lib/multipart_parser/reader.rb', line 37 def on_error(&callback) @on_error = callback end |
#on_part(&callback) ⇒ Object
Sets to a code block to call when part headers have been parsed.
31 32 33 |
# File 'lib/multipart_parser/reader.rb', line 31 def on_part(&callback) @on_part = callback end |
#write(buffer) ⇒ Object
Write data from the given buffer (String) into the reader.
43 44 45 46 47 48 49 |
# File 'lib/multipart_parser/reader.rb', line 43 def write(buffer) bytes_parsed = @parser.write(buffer) if bytes_parsed != buffer.size msg = "Parser error, #{bytes_parsed} of #{buffer.length} bytes parsed" @on_error.call(msg) unless @on_error.nil? end end |