Class: Hermeneutics::Multipart

Inherits:
Mime
  • Object
show all
Defined in:
lib/hermeneutics/message.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mime

find, inherited

Constructor Details

#initialize(boundary, prolog, list, epilog) ⇒ Multipart

Returns a new instance of Multipart.



44
45
46
47
# File 'lib/hermeneutics/message.rb', line 44

def initialize boundary, prolog, list, epilog
  @boundary = boundary.notempty?
  @prolog, @list, @epilog = prolog, list, epilog
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



42
43
44
# File 'lib/hermeneutics/message.rb', line 42

def boundary
  @boundary
end

#epilogObject (readonly)

Returns the value of attribute epilog.



42
43
44
# File 'lib/hermeneutics/message.rb', line 42

def epilog
  @epilog
end

#listObject (readonly)

Returns the value of attribute list.



42
43
44
# File 'lib/hermeneutics/message.rb', line 42

def list
  @list
end

#prologObject (readonly)

Returns the value of attribute prolog.



42
43
44
# File 'lib/hermeneutics/message.rb', line 42

def prolog
  @prolog
end

Class Method Details

.parse(input, **parameters) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hermeneutics/message.rb', line 23

def parse input, **parameters
  b = parameters[ :boundary]
  b or raise ParseError, "Missing boundary parameter."
  list = input.split /^--#{Regexp.quote b}/
  prolog = list.shift
  epilog = list.pop
  epilog and epilog.slice! /\A--\r?\n/ or raise "Missing last separator."
  list.each { |p|
    p.slice! /\A\r?\n/ or raise "Malformed separator: #{b + p[/.*/]}."
  }
  list.map! { |t| Message.parse t }
  new b, prolog, list, epilog
end

Instance Method Details

#[](num) ⇒ Object



84
85
86
# File 'lib/hermeneutics/message.rb', line 84

def [] num
  @list[ num]
end

#boundary!Object



49
50
51
52
53
54
# File 'lib/hermeneutics/message.rb', line 49

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



88
89
90
# File 'lib/hermeneutics/message.rb', line 88

def each &block
  @list.each &block
end

#inspectObject



56
57
58
59
60
61
62
# File 'lib/hermeneutics/message.rb', line 56

def inspect
  r = ""
  r << "#<#{self.class}:"
  r << "0x%x" % (object_id<<1)
  r << " n=#{@list.length}"
  r << ">"
end

#lengthObject



92
# File 'lib/hermeneutics/message.rb', line 92

def length ; @list.length ; end

#to_sObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hermeneutics/message.rb', line 64

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