Class: Hermeneutics::Multipart
Defined Under Namespace
Classes: IllegalBoundary, ParseError
Constant Summary collapse
- MIME =
/^multipart\//- BOUNDARY_CHARS_STD =
[ [*"0".."9"], [*"A".."Z"], [*"a".."z"]].join
- BOUNDARY_CHARS =
“‘()+_,-./:=?”
BOUNDARY_CHARS_STD + "+_./:=-"
Instance Attribute Summary collapse
-
#boundary ⇒ Object
readonly
Returns the value of attribute boundary.
-
#epilog ⇒ Object
readonly
Returns the value of attribute epilog.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#prolog ⇒ Object
readonly
Returns the value of attribute prolog.
Class Method Summary collapse
Instance Method Summary collapse
- #[](num) ⇒ Object
- #boundary! ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(boundary, prolog, list, epilog) ⇒ Multipart
constructor
A new instance of Multipart.
- #inspect ⇒ Object
- #length ⇒ Object
- #to_s ⇒ Object
Methods inherited from Mime
Constructor Details
#initialize(boundary, prolog, list, epilog) ⇒ Multipart
Returns a new instance of Multipart.
45 46 47 48 |
# File 'lib/hermeneutics/message.rb', line 45 def initialize boundary, prolog, list, epilog @boundary = boundary.notempty? @prolog, @list, @epilog = prolog, list, epilog end |
Instance Attribute Details
#boundary ⇒ Object (readonly)
Returns the value of attribute boundary.
43 44 45 |
# File 'lib/hermeneutics/message.rb', line 43 def boundary @boundary end |
#epilog ⇒ Object (readonly)
Returns the value of attribute epilog.
43 44 45 |
# File 'lib/hermeneutics/message.rb', line 43 def epilog @epilog end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
43 44 45 |
# File 'lib/hermeneutics/message.rb', line 43 def list @list end |
#prolog ⇒ Object (readonly)
Returns the value of attribute prolog.
43 44 45 |
# File 'lib/hermeneutics/message.rb', line 43 def prolog @prolog end |
Class Method Details
.parse(input, **parameters) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hermeneutics/message.rb', line 23 def parse input, **parameters b = parameters[ :boundary] b or raise ParseError, "Missing boundary parameter." input.encode! input.encoding, universal_newline: true list = input.split /^--#{Regexp.quote b}/ prolog = list.shift epilog = list.pop epilog and epilog.slice! /\A--\n/ or raise "Missing last separator." list.each { |p| p.slice! /\A\n/ or raise "Malformed separator: #{b + p[/.*/]}." } list.map! { |t| Message.parse t } new b, prolog, list, epilog end |
Instance Method Details
#[](num) ⇒ Object
85 86 87 |
# File 'lib/hermeneutics/message.rb', line 85 def [] num @list[ num] end |
#boundary! ⇒ Object
50 51 52 53 54 55 |
# File 'lib/hermeneutics/message.rb', line 50 def boundary! b = BOUNDARY_CHARS_STD.length r = Time.now.strftime "%Y%m%d%H%M%S." 16.times { r << BOUNDARY_CHARS_STD[ (rand b)].chr } @boundary = r end |
#each(&block) ⇒ Object
89 90 91 |
# File 'lib/hermeneutics/message.rb', line 89 def each &block @list.each &block end |
#inspect ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/hermeneutics/message.rb', line 57 def inspect r = "" r << "#<#{self.class}:" r << "0x%x" % (object_id<<1) r << " n=#{@list.length}" r << ">" end |
#length ⇒ Object
93 |
# File 'lib/hermeneutics/message.rb', line 93 def length ; @list.length ; end |
#to_s ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/hermeneutics/message.rb', line 65 def to_s @boundary or raise IllegalBoundary r = "" splitter = "--#@boundary" re = /#{Regexp.quote @boundary}/ @prolog =~ re and raise IllegalBoundary r << @prolog @list.each { |p| s = p.to_s s =~ re rescue nil $& and raise IllegalBoundary r << splitter << "\n" << s } @epilog =~ re and raise IllegalBoundary r << splitter << "--\n" << @epilog rescue IllegalBoundary boundary! retry end |