class 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
- FIELDS_MAP
- FIELD_NAME_MAP
- FIELD_ORDER_LOOKUP
- KNOWN_FIELDS
- STRUCTURED_FIELDS
Attributes
Public Class Methods
Source
# File lib/mail/field.rb, line 195 def initialize(name, value = nil, charset = 'utf-8') case when name.index(Constants::COLON) raise ArgumentError, 'Passing an unparsed header field to Mail::Field.new is not supported in Mail 2.8.0+. Use Mail::Field.parse instead.' when Utilities.blank?(value) @name = name @unparsed_value = nil @charset = charset else @name = name @unparsed_value = value @charset = charset end klass = self.class.field_class_for(@name) @name = klass ? klass::NAME : @name end
Create a field by name and optional value:
Mail::Field.new("field-name", "value") # => #<Mail::Field …>
Values that aren’t strings or arrays are coerced to Strings with ‘#to_s`.
Mail::Field.new("field-name", 1234) # => #<Mail::Field …> Mail::Field.new('content-type', ['text', 'plain', {:charset => 'UTF-8'}]) # => #<Mail::Field …>
Source
# File lib/mail/field.rb, line 150 def parse(field, charset = 'utf-8') name, value = split(field) if name && value new name, value, charset end end
Parse a field from a raw header line:
Mail::Field.parse("field-name: field data") # => #<Mail::Field …>
Public Instance Methods
Source
# File lib/mail/field.rb, line 254 def <=>(other) field_order_id <=> other.field_order_id end
Source
# File lib/mail/field.rb, line 246 def ==(other) same(other) && Utilities.match_to_s(other.value, value) end
Source
# File lib/mail/field.rb, line 216 def field @field ||= create_field(@name, @unparsed_value, @charset) end
Source
# File lib/mail/field.rb, line 258 def field_order_id @field_order_id ||= FIELD_ORDER_LOOKUP.fetch(self.name.to_s.downcase, 100) end
Source
# File lib/mail/field.rb, line 236 def inspect "#<#{self.class.name} 0x#{(object_id * 2).to_s(16)} #{instance_variables.map do |ivar| "#{ivar}=#{instance_variable_get(ivar).inspect}" end.join(" ")}>" end
Source
# File lib/mail/field.rb, line 262 def method_missing(name, *args, &block) field.send(name, *args, &block) end
Source
# File lib/mail/field.rb, line 266 def respond_to_missing?(method_name, include_private) field.respond_to?(method_name, include_private) || super end
Calls superclass method
Source
# File lib/mail/field.rb, line 250 def responsible_for?(field_name) name.to_s.casecmp(field_name.to_s) == 0 end
Source
# File lib/mail/field.rb, line 242 def same(other) other.kind_of?(self.class) && Utilities.match_to_s(other.name, name) end
Source
# File lib/mail/field.rb, line 228 def value=(val) @field = create_field(name, val, @charset) end