Parent

Included Modules

Class Index [+]

Quicksearch

Mail::Field

Provides a single class to call to create a new structured or unstructured field. Works out per RFC what field of field it is being given and returns the correct field of class back on new.

Per RFC 2822

 
 2.2. Header Fields
 
    Header fields are lines composed of a field name, followed by a colon
    (":"), followed by a field body, and terminated by CRLF.  A field
    name MUST be composed of printable US-ASCII characters (i.e.,
    characters that have values between 33 and 126, inclusive), except
    colon.  A field body may be composed of any US-ASCII characters,
    except for CR and LF.  However, a field body may contain CRLF when
    used in header "folding" and  "unfolding" as described in section
    2.2.3.  All field bodies MUST conform to the syntax described in
    sections 3 and 4 of this standard.
 

Constants

STRUCTURED_FIELDS
KNOWN_FIELDS
FIELD_ORDER

Public Class Methods

new(name, value = nil, charset = 'utf-8') click to toggle source

Accepts a string:

 Field.new("field-name: field data")

Or name, value pair:

 Field.new("field-name", "value")

Or a name by itself:

 Field.new("field-name")

Note, does not want a terminating carriage return. Returns self appropriately parsed. If value is not a string, then it will be passed through as is, for example, content-type field can accept an array with the type and a hash of parameters:

 Field.new('content-type', ['text', 'plain', {:charset => 'UTF-8'}])
    # File lib/mail/field.rb, line 78
78:     def initialize(name, value = nil, charset = 'utf-8')
79:       case
80:       when name =~ /:/                  # Field.new("field-name: field data")
81:         charset = value unless value.blank?
82:         name, value = split(name)
83:         create_field(name, value, charset)
84:       when name !~ /:/ && value.blank?  # Field.new("field-name")
85:         create_field(name, nil, charset)
86:       else                              # Field.new("field-name", "value")
87:         create_field(name, value, charset)
88:       end
89:       return self
90:     end

Public Instance Methods

<=>( other ) click to toggle source
     # File lib/mail/field.rb, line 126
126:     def <=>( other )
127:       self_order = FIELD_ORDER.rindex(self.name.to_s.downcase) || 100
128:       other_order = FIELD_ORDER.rindex(other.name.to_s.downcase) || 100
129:       self_order <=> other_order
130:     end
==( other ) click to toggle source
Alias for: same
field() click to toggle source
    # File lib/mail/field.rb, line 96
96:     def field
97:       @field
98:     end
field=(value) click to toggle source
    # File lib/mail/field.rb, line 92
92:     def field=(value)
93:       @field = value
94:     end
method_missing(name, *args, &block) click to toggle source
     # File lib/mail/field.rb, line 132
132:     def method_missing(name, *args, &block)
133:       field.send(name, *args, &block)
134:     end
name() click to toggle source
     # File lib/mail/field.rb, line 100
100:     def name
101:       field.name
102:     end
same( other ) click to toggle source
     # File lib/mail/field.rb, line 120
120:     def same( other )
121:       match_to_s(other.name, field.name)
122:     end
Also aliased as: ==
to_s() click to toggle source
     # File lib/mail/field.rb, line 112
112:     def to_s
113:       field.to_s
114:     end
update(name, value) click to toggle source
     # File lib/mail/field.rb, line 116
116:     def update(name, value)
117:       create_field(name, value, charset)
118:     end
value() click to toggle source
     # File lib/mail/field.rb, line 104
104:     def value
105:       field.value
106:     end
value=(val) click to toggle source
     # File lib/mail/field.rb, line 108
108:     def value=(val)
109:       create_field(name, val, charset)
110:     end

Private Instance Methods

create_field(name, value, charset) click to toggle source
     # File lib/mail/field.rb, line 154
154:     def create_field(name, value, charset)
155:       begin
156:         self.field = new_field(name, value, charset)
157:       rescue Mail::Field::ParseError => e
158:         self.field = Mail::UnstructuredField.new(name, value)
159:         self.field.errors << [name, value, e]
160:         self.field
161:       end
162:     end
new_field(name, value, charset) click to toggle source
     # File lib/mail/field.rb, line 164
164:     def new_field(name, value, charset)
165:       # Could do this with constantize and make it "as DRY as", but a simple case 
166:       # statement is, well, simpler... 
167:       case name.to_s.downcase
168:       when /^to$/
169:         ToField.new(value, charset)
170:       when /^cc$/
171:         CcField.new(value, charset)
172:       when /^bcc$/
173:         BccField.new(value, charset)
174:       when /^message-id$/
175:         MessageIdField.new(value, charset)
176:       when /^in-reply-to$/
177:         InReplyToField.new(value, charset)
178:       when /^references$/
179:         ReferencesField.new(value, charset)
180:       when /^subject$/
181:         SubjectField.new(value, charset)
182:       when /^comments$/
183:         CommentsField.new(value, charset)
184:       when /^keywords$/
185:         KeywordsField.new(value, charset)
186:       when /^date$/
187:         DateField.new(value, charset)
188:       when /^from$/
189:         FromField.new(value, charset)
190:       when /^sender$/
191:         SenderField.new(value, charset)
192:       when /^reply-to$/
193:         ReplyToField.new(value, charset)
194:       when /^resent-date$/
195:         ResentDateField.new(value, charset)
196:       when /^resent-from$/
197:         ResentFromField.new(value, charset)
198:       when /^resent-sender$/ 
199:         ResentSenderField.new(value, charset)
200:       when /^resent-to$/
201:         ResentToField.new(value, charset)
202:       when /^resent-cc$/
203:         ResentCcField.new(value, charset)
204:       when /^resent-bcc$/
205:         ResentBccField.new(value, charset)
206:       when /^resent-message-id$/
207:         ResentMessageIdField.new(value, charset)
208:       when /^return-path$/
209:         ReturnPathField.new(value, charset)
210:       when /^received$/
211:         ReceivedField.new(value, charset)
212:       when /^mime-version$/
213:         MimeVersionField.new(value, charset)
214:       when /^content-transfer-encoding$/
215:         ContentTransferEncodingField.new(value, charset)
216:       when /^content-description$/
217:         ContentDescriptionField.new(value, charset)
218:       when /^content-disposition$/
219:         ContentDispositionField.new(value, charset)
220:       when /^content-type$/
221:         ContentTypeField.new(value, charset)
222:       when /^content-id$/
223:         ContentIdField.new(value, charset)
224:       when /^content-location$/
225:         ContentLocationField.new(value, charset)
226:       else 
227:         OptionalField.new(name, value, charset)
228:       end
229:       
230:     end
split(raw_field) click to toggle source
     # File lib/mail/field.rb, line 147
147:     def split(raw_field)
148:       match_data = raw_field.mb_chars.match(/^(#{FIELD_NAME})\s*:\s*(#{FIELD_BODY})?$/)
149:       [match_data[1].to_s.mb_chars.strip, match_data[2].to_s.mb_chars.strip]
150:     rescue
151:       STDERR.puts "WARNING: Could not parse (and so ignorning) '#{raw_field}'"
152:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.