Object
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.
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.
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 70 70: def initialize(name, value = nil, charset = 'utf-8') 71: case 72: when name =~ /:/ && value.blank? # Field.new("field-name: field data") 73: name, value = split(name) 74: create_field(name, value, charset) 75: when name !~ /:/ && value.blank? # Field.new("field-name") 76: create_field(name, nil, charset) 77: else # Field.new("field-name", "value") 78: create_field(name, value, charset) 79: end 80: return self 81: end
# File lib/mail/field.rb, line 115 115: def <=>( other ) 116: self_order = FIELD_ORDER.rindex(self.name.to_s.downcase) || 100 117: other_order = FIELD_ORDER.rindex(other.name.to_s.downcase) || 100 118: self_order <=> other_order 119: end
# File lib/mail/field.rb, line 83 83: def field=(value) 84: @field = value 85: end
# File lib/mail/field.rb, line 121 121: def method_missing(name, *args, &block) 122: field.send(name, *args, &block) 123: end
# File lib/mail/field.rb, line 111 111: def same( other ) 112: match_to_s(other.name, field.name) 113: end
# File lib/mail/field.rb, line 103 103: def to_s 104: field.to_s 105: end
# File lib/mail/field.rb, line 107 107: def update(name, value) 108: create_field(name, value, charset) 109: end
# File lib/mail/field.rb, line 143 143: def create_field(name, value, charset) 144: begin 145: self.field = new_field(name, value, charset) 146: rescue Mail::Field::ParseError => e 147: self.field = Mail::UnstructuredField.new(name, value) 148: self.field.errors << [name, value, e] 149: self.field 150: end 151: end
# File lib/mail/field.rb, line 153 153: def new_field(name, value, charset) 154: # Could do this with constantize and make it "as DRY as", but a simple case 155: # statement is, well, simpler... 156: case name.to_s.downcase 157: when /^to$/ 158: ToField.new(value, charset) 159: when /^cc$/ 160: CcField.new(value, charset) 161: when /^bcc$/ 162: BccField.new(value, charset) 163: when /^message-id$/ 164: MessageIdField.new(value, charset) 165: when /^in-reply-to$/ 166: InReplyToField.new(value, charset) 167: when /^references$/ 168: ReferencesField.new(value, charset) 169: when /^subject$/ 170: SubjectField.new(value, charset) 171: when /^comments$/ 172: CommentsField.new(value, charset) 173: when /^keywords$/ 174: KeywordsField.new(value, charset) 175: when /^date$/ 176: DateField.new(value, charset) 177: when /^from$/ 178: FromField.new(value, charset) 179: when /^sender$/ 180: SenderField.new(value, charset) 181: when /^reply-to$/ 182: ReplyToField.new(value, charset) 183: when /^resent-date$/ 184: ResentDateField.new(value, charset) 185: when /^resent-from$/ 186: ResentFromField.new(value, charset) 187: when /^resent-sender$/ 188: ResentSenderField.new(value, charset) 189: when /^resent-to$/ 190: ResentToField.new(value, charset) 191: when /^resent-cc$/ 192: ResentCcField.new(value, charset) 193: when /^resent-bcc$/ 194: ResentBccField.new(value, charset) 195: when /^resent-message-id$/ 196: ResentMessageIdField.new(value, charset) 197: when /^return-path$/ 198: ReturnPathField.new(value, charset) 199: when /^received$/ 200: ReceivedField.new(value, charset) 201: when /^mime-version$/ 202: MimeVersionField.new(value, charset) 203: when /^content-transfer-encoding$/ 204: ContentTransferEncodingField.new(value, charset) 205: when /^content-description$/ 206: ContentDescriptionField.new(value, charset) 207: when /^content-disposition$/ 208: ContentDispositionField.new(value, charset) 209: when /^content-type$/ 210: ContentTypeField.new(value, charset) 211: when /^content-id$/ 212: ContentIdField.new(value, charset) 213: when /^content-location$/ 214: ContentLocationField.new(value, charset) 215: else 216: OptionalField.new(name, value, charset) 217: end 218: 219: end
# File lib/mail/field.rb, line 136 136: def split(raw_field) 137: match_data = raw_field.mb_chars.match(/^(#{FIELD_NAME})\s*:\s*(#{FIELD_BODY})?$/) 138: [match_data[1].to_s.mb_chars.strip, match_data[2].to_s.mb_chars.strip] 139: rescue 140: STDERR.puts "WARNING: Could not parse (and so ignorning) '#{raw_field}'" 141: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.