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_end(&callback) ⇒ Object
-
#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.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/multipart_parser/reader.rb', line 9 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 @on_end = 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.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/multipart_parser/reader.rb', line 57 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
24 25 26 |
# File 'lib/multipart_parser/reader.rb', line 24 def ended? @ended end |
#on_end(&callback) ⇒ Object
34 35 36 |
# File 'lib/multipart_parser/reader.rb', line 34 def on_end(&callback) @on_end = callback end |
#on_error(&callback) ⇒ Object
Sets a code block to call when a parser error occurs.
40 41 42 |
# File 'lib/multipart_parser/reader.rb', line 40 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.
30 31 32 |
# File 'lib/multipart_parser/reader.rb', line 30 def on_part(&callback) @on_part = callback end |
#write(buffer) ⇒ Object
Write data from the given buffer (String) into the reader.
46 47 48 49 50 51 52 |
# File 'lib/multipart_parser/reader.rb', line 46 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 |