Class Index [+]

Quicksearch

Mail::ContentTypeField

Constants

FIELD_NAME
CAPITALIZED_FIELD

Public Class Methods

generate_boundary() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 81
81:     def ContentTypeField.generate_boundary
82:       "--==_mimepart_#{Mail.random_tag}"
83:     end
new(value = nil, charset = 'utf-8') click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 10
10:     def initialize(value = nil, charset = 'utf-8')
11:       self.charset = charset
12:       if value.class == Array
13:         @main_type = value[0]
14:         @sub_type = value[1]
15:         @parameters = ParameterHash.new.merge!(value.last)
16:       else
17:         @main_type = nil
18:         @sub_type = nil
19:         @parameters = nil
20:         value = strip_field(FIELD_NAME, value)
21:       end
22:       super(CAPITALIZED_FIELD, value, charset)
23:       self.parse
24:       self
25:     end
with_boundary(type) click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 77
77:     def ContentTypeField.with_boundary(type)
78:       new("#{type}; boundary=#{generate_boundary}")
79:     end

Public Instance Methods

attempt_to_clean() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 43
43:     def attempt_to_clean
44:       # Sanitize the value, handle special cases
45:       @element ||= Mail::ContentTypeElement.new(sanatize(value))
46:     rescue
47:       # All else fails, just get the MIME media type
48:       @element ||= Mail::ContentTypeElement.new(get_mime_type(value))
49:     end
content_type() click to toggle source
Alias for: string
decoded() click to toggle source
     # File lib/mail/fields/content_type_field.rb, line 119
119:     def decoded
120:       if parameters.length > 0
121:         p = "; #{parameters.decoded}"
122:       else
123:         p = ""
124:       end
125:       "#{content_type}" + p
126:     end
default() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 63
63:     def default
64:       decoded
65:     end
element() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 35
35:     def element
36:       begin
37:         @element ||= Mail::ContentTypeElement.new(value)
38:       rescue
39:         attempt_to_clean
40:       end
41:     end
encoded() click to toggle source

TODO: Fix this up

     # File lib/mail/fields/content_type_field.rb, line 110
110:     def encoded
111:       if parameters.length > 0
112:         p = ";\r\n\s#{parameters.encoded}"
113:       else
114:         p = ""
115:       end
116:       "#{CAPITALIZED_FIELD}: #{content_type}#{p}\r\n"
117:     end
filename() click to toggle source
     # File lib/mail/fields/content_type_field.rb, line 97
 97:     def filename
 98:       case
 99:       when parameters['filename']
100:         @filename = parameters['filename']
101:       when parameters['name']
102:         @filename = parameters['name']
103:       else
104:         @filename = nil
105:       end
106:       @filename
107:     end
main_type() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 51
51:     def main_type
52:       @main_type ||= element.main_type
53:     end
parameters() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 69
69:     def parameters
70:       unless @parameters
71:         @parameters = ParameterHash.new
72:         element.parameters.each { |p| @parameters.merge!(p) }
73:       end
74:       @parameters
75:     end
parse(val = value) click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 27
27:     def parse(val = value)
28:       unless val.blank?
29:         self.value = val
30:         @element = nil
31:         element
32:       end
33:     end
string() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 59
59:     def string
60:       "#{main_type}/#{sub_type}"
61:     end
Also aliased as: content_type
stringify(params) click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 93
93:     def stringify(params)
94:       params.map { |k,v| "#{k}=#{Encodings.param_encode(v)}" }.join("; ")
95:     end
sub_type() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 55
55:     def sub_type
56:       @sub_type ||= element.sub_type
57:     end
value() click to toggle source
    # File lib/mail/fields/content_type_field.rb, line 85
85:     def value
86:       if @value.class == Array
87:         "#{@main_type}/#{@sub_type}; #{stringify(parameters)}"
88:       else
89:         @value
90:       end
91:     end

Private Instance Methods

get_mime_type( val ) click to toggle source
     # File lib/mail/fields/content_type_field.rb, line 189
189:     def get_mime_type( val )
190:       case
191:       when val =~ /^([\w\d\-_]+)\/([\w\d\-_]+);.+$/
192:         "#{$1}/#{$2}"
193:       else
194:         'text/plain'
195:       end
196:     end
method_missing(name, *args, &block) click to toggle source
     # File lib/mail/fields/content_type_field.rb, line 130
130:     def method_missing(name, *args, &block)
131:       if name.to_s =~ /([\w_]+)=/
132:         self.parameters[$1] = args.first
133:         @value = "#{content_type}; #{stringify(parameters)}"
134:       else
135:         super
136:       end
137:     end
sanatize( val ) click to toggle source

Various special cases from random emails found that I am not going to change the parser for

     # File lib/mail/fields/content_type_field.rb, line 141
141:     def sanatize( val )
142: 
143:       # TODO: check if there are cases where whitespace is not a separator
144:       val = val.tr(' ',';').
145:         squeeze(';').
146:         gsub(';', '; '). #use '; ' as a separator (or EOL)
147:         gsub(/;\s*$/,'') #remove trailing to keep examples below
148: 
149:       if val =~ /(boundary=(\S*))/
150:         val = "#{$`.downcase}boundary=#{$2}#{$'.downcase}"
151:       else
152:         val.downcase!
153:       end
154: 
155:       case
156:       when val.chomp =~ /^\s*([\w\d\-_]+)\/([\w\d\-_]+)\s*;;+(.*)$/
157:         # Handles 'text/plain;; format="flowed"' (double semi colon)
158:         "#{$1}/#{$2}; #{$3}"
159:       when val.chomp =~ /^\s*([\w\d\-_]+)\/([\w\d\-_]+)\s*;\s?(ISO[\w\d\-_]+)$/
160:         # Microsoft helper:
161:         # Handles 'type/subtype;ISO-8559-1'
162:         "#{$1}/#{$2}; charset=#{quote_atom($3)}"
163:       when val.chomp =~ /^text;?$/
164:         # Handles 'text;' and 'text'
165:         "text/plain;"
166:       when val.chomp =~ /^(\w+);\s(.*)$/
167:         # Handles 'text; <parameters>'
168:         "text/plain; #{$2}"
169:       when val =~ /([\w\d\-_]+\/[\w\d\-_]+);\scharset="charset="(\w+)""/
170:         # Handles text/html; charset="charset="GB2312""
171:         "#{$1}; charset=#{quote_atom($2)}"
172:       when val =~ /([\w\d\-_]+\/[\w\d\-_]+);\s+(.*)/
173:         type = $1
174:         # Handles misquoted param values
175:         # e.g: application/octet-stream; name=archiveshelp1[1].htm
176:         # and: audio/x-midi;\r\n\sname=Part .exe
177:         params = $2.to_s.split(/\s+/)
178:         params = params.map { |i| i.to_s.chomp.strip }
179:         params = params.map { |i| i.split(/\s*\=\s*/) }
180:         params = params.map { |i| "#{i[0]}=#{dquote(i[1].to_s)}" }.join('; ')
181:         "#{type}; #{params}"
182:       when val =~ /^\s*$/
183:         'text/plain'
184:       else
185:         ''
186:       end
187:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.