Class: Hermeneutics::Multipart

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

Defined Under Namespace

Classes: IllegalBoundary, ParseError, PartFile

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.



153
154
155
156
# File 'lib/hermeneutics/message.rb', line 153

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.



151
152
153
# File 'lib/hermeneutics/message.rb', line 151

def boundary
  @boundary
end

#epilogObject (readonly)

Returns the value of attribute epilog.



151
152
153
# File 'lib/hermeneutics/message.rb', line 151

def epilog
  @epilog
end

#listObject (readonly)

Returns the value of attribute list.



151
152
153
# File 'lib/hermeneutics/message.rb', line 151

def list
  @list
end

#prologObject (readonly)

Returns the value of attribute prolog.



151
152
153
# File 'lib/hermeneutics/message.rb', line 151

def prolog
  @prolog
end

Class Method Details

.parse(input, parameters) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hermeneutics/message.rb', line 133

def parse input, parameters
  b = parameters[ :boundary]
  b or raise ParseError, "Missing boundary parameter."
  PartFile.open input, b do |partfile|
    list = []
    while partfile.next_part do
      m = Message.parse partfile
      list.push m
    end
    new b, partfile.prolog, list, partfile.epilog
  end
end

Instance Method Details

#[](num) ⇒ Object



193
194
195
# File 'lib/hermeneutics/message.rb', line 193

def [] num
  @list[ num]
end

#boundary!Object



158
159
160
161
162
163
# File 'lib/hermeneutics/message.rb', line 158

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



197
198
199
# File 'lib/hermeneutics/message.rb', line 197

def each &block
  @list.each &block
end

#inspectObject



165
166
167
168
169
170
171
# File 'lib/hermeneutics/message.rb', line 165

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

#lengthObject



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

def length ; @list.length ; end

#to_sObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/hermeneutics/message.rb', line 173

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 << $/ << s << $/
  }
  @epilog =~ re and raise IllegalBoundary
  r << splitter << "--" << @epilog
rescue IllegalBoundary
  boundary!
  retry
end