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
FIELD_ORDER_LOOKUP
KNOWN_FIELDS
STRUCTURED_FIELDS

Attributes

unparsed_value[R]

Public Class Methods

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

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 …>
# File lib/mail/field.rb, line 164
def initialize(name, value = nil, charset = 'utf-8')
  case
  when name.index(COLON)
    Kernel.warn 'Passing an unparsed header field to Mail::Field.new is deprecated and will be removed in Mail 2.8.0. Use Mail::Field.parse instead.'
    @name, @unparsed_value = self.class.split(name)
    @charset = Utilities.blank?(value) ? charset : value
  when Utilities.blank?(value)
    @name = name
    @unparsed_value = nil
    @charset = charset
  else
    @name = name
    @unparsed_value = value
    @charset = charset
  end
  @name = FIELD_NAME_MAP[@name.to_s.downcase] || @name
end
parse(field, charset = nil) click to toggle source

Parse a field from a raw header line:

Mail::Field.parse("field-name: field data")
# => #<Mail::Field …>
# File lib/mail/field.rb, line 124
def parse(field, charset = nil)
  name, value = split(field)
  if name && value
    new name, value, charset
  end
end

Public Instance Methods

<=>( other ) click to toggle source
# File lib/mail/field.rb, line 230
def <=>( other )
  self.field_order_id <=> other.field_order_id
end
==( other ) click to toggle source
# File lib/mail/field.rb, line 221
def ==( other )
  return false unless other.kind_of?(self.class)
  match_to_s(other.name, self.name) && match_to_s(other.value, self.value)
end
field() click to toggle source
# File lib/mail/field.rb, line 186
def field
  @field ||= create_field(@name, @unparsed_value, @charset)
end
field=(value) click to toggle source
# File lib/mail/field.rb, line 182
def field=(value)
  @field = value
end
field_order_id() click to toggle source
# File lib/mail/field.rb, line 234
def field_order_id
  @field_order_id ||= (FIELD_ORDER_LOOKUP[self.name.to_s.downcase] || 100)
end
inspect() click to toggle source
# File lib/mail/field.rb, line 206
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
method_missing(name, *args, &block) click to toggle source
# File lib/mail/field.rb, line 238
def method_missing(name, *args, &block)
  field.send(name, *args, &block)
end
name() click to toggle source
# File lib/mail/field.rb, line 190
def name
  @name
end
respond_to?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/mail/field.rb, line 247
def respond_to?(method_name, include_private = false)
  field.respond_to?(method_name, include_private) || super
end
respond_to_missing?(method_name, include_private) click to toggle source
Calls superclass method
# File lib/mail/field.rb, line 243
def respond_to_missing?(method_name, include_private)
  field.respond_to?(method_name, include_private) || super
end
responsible_for?( val ) click to toggle source
# File lib/mail/field.rb, line 226
def responsible_for?( val )
  name.to_s.casecmp(val.to_s) == 0
end
same( other ) click to toggle source
# File lib/mail/field.rb, line 216
def same( other )
  return false unless other.kind_of?(self.class)
  match_to_s(other.name, self.name)
end
to_s() click to toggle source
# File lib/mail/field.rb, line 202
def to_s
  field.to_s
end
update(name, value) click to toggle source
# File lib/mail/field.rb, line 212
def update(name, value)
  @field = create_field(name, value, @charset)
end
value() click to toggle source
# File lib/mail/field.rb, line 194
def value
  field.value
end
value=(val) click to toggle source
# File lib/mail/field.rb, line 198
def value=(val)
  @field = create_field(name, val, @charset)
end

Private Instance Methods

create_field(name, value, charset) click to toggle source
# File lib/mail/field.rb, line 265
def create_field(name, value, charset)
  new_field(name, value, charset)
rescue Mail::Field::ParseError => e
  field = Mail::UnstructuredField.new(name, value)
  field.errors << [name, value, e]
  field
end
field_class_for(name) click to toggle source
# File lib/mail/field.rb, line 283
def field_class_for(name)
  FIELDS_MAP[name.to_s.downcase]
end
new_field(name, value, charset) click to toggle source
# File lib/mail/field.rb, line 273
def new_field(name, value, charset)
  value = unfold(value) if value.is_a?(String)

  if klass = field_class_for(name)
    klass.new(value, charset)
  else
    OptionalField.new(name, value, charset)
  end
end
unfold(string) click to toggle source

2.2.3. Long Header Fields

The process of moving from this folded multiple-line representation
of a header field to its single line representation is called
"unfolding". Unfolding is accomplished by simply removing any CRLF
that is immediately followed by WSP.  Each header field should be
treated in its unfolded form for further syntactic and semantic
evaluation.
# File lib/mail/field.rb, line 295
def unfold(string)
  string.gsub(/#{Constants::CRLF}(#{Constants::WSP})/m, '\1')
end