Class: SOAP::MIMEMessage

Inherits:
Object show all
Defined in:
lib/soap/mimemessage.rb

Defined Under Namespace

Classes: Header, Headers, MIMEMessageError, Part

Constant Summary collapse

MultipartContentType =
'multipart/\w+'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMIMEMessage

Returns a new instance of MIMEMessage.



150
151
152
153
154
155
# File 'lib/soap/mimemessage.rb', line 150

def initialize
  @parts = []
  @headers = Headers.new
  @root = nil
  @boundary = nil
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



161
162
163
# File 'lib/soap/mimemessage.rb', line 161

def headers
  @headers
end

#partsObject (readonly)

Returns the value of attribute parts.



161
162
163
# File 'lib/soap/mimemessage.rb', line 161

def parts
  @parts
end

Class Method Details

.parse(head, str) ⇒ Object



157
158
159
# File 'lib/soap/mimemessage.rb', line 157

def self.parse(head, str)
  new.parse(head, str)
end

Instance Method Details

#add_attachment(attach) ⇒ Object



211
212
213
214
215
216
217
# File 'lib/soap/mimemessage.rb', line 211

def add_attachment(attach)
  part = Part.new
  part.headers.add("Content-Type", attach.contenttype)
  part.headers.add("Content-ID", attach.mime_contentid)
  part.body = attach.content
  @parts.unshift(part)
end

#add_part(content) ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/soap/mimemessage.rb', line 202

def add_part(content)
  part = Part.new
  part.headers.add("Content-Type",
    "text/xml; charset=" + XSD::Charset.xml_encoding_label)
  part.headers.add("Content-ID", Attachment.contentid(part))
  part.body = content
  @parts.unshift(part)
end

#boundaryObject



195
196
197
198
199
200
# File 'lib/soap/mimemessage.rb', line 195

def boundary
  if @boundary == nil
    @boundary = "----=Part_" + __id__.to_s + rand.to_s
  end
  @boundary
end

#closeObject



163
164
165
166
167
168
# File 'lib/soap/mimemessage.rb', line 163

def close
  @headers.add(
    "Content-Type",
    "multipart/related; type=\"text/xml\"; boundary=\"#{boundary}\"; start=\"#{@parts[0].contentid}\""
  )
end

#content_strObject



227
228
229
230
231
232
233
234
235
# File 'lib/soap/mimemessage.rb', line 227

def content_str
  str = ''
  @parts.each do |prt|
    str << "--" + boundary + "\r\n"
    str << prt.to_s + "\r\n"
  end
  str << '--' + boundary + "--\r\n"
  str
end

#has_parts?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/soap/mimemessage.rb', line 219

def has_parts?
  (@parts.length > 0)
end

#headers_strObject



223
224
225
# File 'lib/soap/mimemessage.rb', line 223

def headers_str
  @headers.to_s
end

#parse(head, str) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/soap/mimemessage.rb', line 170

def parse(head, str)
  @headers = Headers.parse(head + "\r\n" + "From: jfh\r\n")
  boundary = @headers['content-type']['boundary']
  if boundary != nil
    parts = str.split(/--#{Regexp.quote(boundary)}\s*(?:\r\n|--\r\n)/)
    part = parts.shift	# preamble must be ignored.
    @parts = parts.collect { |part| Part.parse(part) }
  else
    @parts = [Part.parse(str)]
  end
  if @parts.length < 1
    raise MIMEMessageError.new("This message contains no valid parts!")
  end
  self
end

#rootObject



186
187
188
189
190
191
192
193
# File 'lib/soap/mimemessage.rb', line 186

def root
  if @root == nil
    start = @headers['content-type']['start']
    @root = (start && @parts.find { |prt| prt.contentid == start }) ||
	@parts[0]
  end
  @root
end

#to_sObject



237
238
239
# File 'lib/soap/mimemessage.rb', line 237

def to_s
  str = headers_str + "\r\n\r\n" + conent_str
end