Class: As2::MimeGenerator::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/as2/mime_generator.rb

Instance Method Summary collapse

Constructor Details

#initializePart

Returns a new instance of Part.



4
5
6
7
8
9
# File 'lib/as2/mime_generator.rb', line 4

def initialize
  @parts = []
  @body = ""
  @headers = {}
  @id = nil
end

Instance Method Details

#[](name) ⇒ Object



11
12
13
# File 'lib/as2/mime_generator.rb', line 11

def [](name)
  @headers[name]
end

#[]=(name, value) ⇒ Object



15
16
17
# File 'lib/as2/mime_generator.rb', line 15

def []=(name, value)
  @headers[name] = value
end

#add_part(part) ⇒ Object



30
31
32
33
34
# File 'lib/as2/mime_generator.rb', line 30

def add_part(part)
  gen_id unless @id
  @parts << part
  @body = nil
end

#bodyObject



19
20
21
# File 'lib/as2/mime_generator.rb', line 19

def body
  @body
end

#body=(body) ⇒ Object



23
24
25
26
27
28
# File 'lib/as2/mime_generator.rb', line 23

def body=(body)
  unless @parts.empty?
    raise "Cannot add plain budy to multipart"
  end
  @body = body
end

#multipart?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/as2/mime_generator.rb', line 36

def multipart?
  ! @parts.empty?
end

#write(io) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/as2/mime_generator.rb', line 40

def write(io)
  @headers.each do |name, value|
    if multipart? && name =~ /content-type/i
      io.print "#{name}: #{value}; \r\n"
      io.print "\tboundary=\"----=_Part_#{@id}\"\r\n"
    else
      io.print "#{name}: #{value}\r\n"
    end
  end
  io.print "\r\n"
  if @parts.empty?
    io.print @body, "\r\n"
  else
    @parts.each do|p|
      io.print "------=_Part_#{@id}\r\n"
      p.write(io)
    end
    io.print "------=_Part_#{@id}--\r\n"
  end
  io.print "\r\n"
end