@@ -10,12 +10,14 @@ def self.capitalize_header(header_name)
1010 header_name . gsub ( /^(\w )/ ) { |m | m . capitalize } . gsub ( /-(\w )/ ) { |n | '-' + n [ 1 ] . chr . capitalize }
1111 end
1212 class Entity
13- def initialize ( arg = nil )
14- if arg . is_a? ( String )
15- @raw = arg . gsub ( /\r / , '' ) . gsub ( /\n / , "\r \n " ) # normalizes end-of-line characters
13+ def initialize ( one = nil , two = nil )
14+ if one . is_a? ( Hash ) || two . is_a? ( Hash )
15+ @headers = one . is_a? ( Hash ) ? one : two
16+ set_content one if one . is_a? ( String )
17+ @encoding = 'quoted-printable' unless encoding
18+ elsif one . is_a? ( String )
19+ @raw = one . gsub ( /\r / , '' ) . gsub ( /\n / , "\r \n " ) # normalizes end-of-line characters
1620 from_parsed ( IETF ::RFC2045 . parse_rfc2045_from ( @raw ) )
17- elsif arg . is_a? ( Hash )
18- @headers = arg
1921 end
2022 end
2123
@@ -72,12 +74,12 @@ def headers
7274 # Macro Methods #
7375
7476 def multipart?
75- !!( headers [ 'content-type' ] =~ /multipart\/ / )
77+ !!( headers [ 'content-type' ] =~ /multipart\/ / ) if headers [ 'content-type' ]
7678 end
7779 def multipart_type
7880 if headers [ 'content-type' ] =~ /multipart\/ (\w +)/
7981 $1
80- end
82+ end if headers [ 'content-type' ]
8183 end
8284 # Auto-generates a boundary if one doesn't yet exist.
8385 def multipart_boundary
@@ -90,24 +92,28 @@ def multipart_boundary
9092 end
9193 end
9294 def attachment?
93- headers [ 'content-disposition' ] =~ /^attachment(?=;|$)/ || headers [ 'content-disposition' ] =~ /^form-data;.* filename=[\" \' ]?[^\" \' ]+[\" \' ]?/
95+ headers [ 'content-disposition' ] =~ /^attachment(?=;|$)/ || headers [ 'content-disposition' ] =~ /^form-data;.* filename=[\" \' ]?[^\" \' ]+[\" \' ]?/ if headers [ 'content-disposition' ]
9496 end
9597 alias :file? :attachment?
9698 def part_filename
9799 # Content-Disposition: attachment; filename="summary.txt"
98100 if headers [ 'content-disposition' ] =~ /; filename=[\" \' ]?([^\" \' ]+)/
99101 $1
100- end
102+ end if headers [ 'content-disposition' ]
101103 end
102- attr_writer :encoding
104+
103105 def encoding
104106 @encoding ||= headers [ 'content-transfer-encoding' ] || nil
105107 end
108+ attr_writer :encoding
109+ alias :set_encoding :encoding=
110+
106111 def find_part ( options )
107112 find_parts ( options ) . first
108113 end
109114 def find_parts ( options )
110115 parts = [ ]
116+ return nil unless ( options [ :content_type ] && headers [ 'content-type' ] ) || ( options [ :content_disposition ] && headers [ 'content-disposition' ] )
111117 # Do I match your search?
112118 iam = true
113119 iam = false if options [ :content_type ] && headers [ 'content-type' ] !~ /^#{ options [ :content_type ] } (?=;|$)/
@@ -167,7 +173,7 @@ def decoded_content
167173 end
168174
169175 # You can set new content, and it will be saved in encoded form.
170- def content = ( raw )
176+ def set_content ( raw )
171177 @content = raw . is_a? ( Array ) ? raw :
172178 case encoding . to_s . downcase
173179 when 'quoted-printable'
@@ -178,10 +184,11 @@ def content=(raw)
178184 raw
179185 end
180186 end
187+ alias :content= :set_content
181188
182189 private
183190 def transfer_to ( other )
184- other . instance_variable_set ( :@content , @content . dup )
191+ other . instance_variable_set ( :@content , @content . dup ) if @content
185192 other . headers . clear
186193 other . headers . merge! ( Hash [ *headers . dup . select { |k , v | k =~ /content/ } . flatten ] )
187194 end
0 commit comments