Class: Mail::Field

Inherits:
Object
  • Object
show all
Includes:
Comparable, Patterns
Defined in:
lib/mail/field.rb

Overview

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.

Defined Under Namespace

Classes: FieldError, ParseError, SyntaxError

Constant Summary collapse

STRUCTURED_FIELDS =
%w[ bcc cc content-description content-disposition
content-id content-location content-transfer-encoding
content-type date from in-reply-to keywords message-id
mime-version received references reply-to
resent-bcc resent-cc resent-date resent-from
resent-message-id resent-sender resent-to
return-path sender to ]
KNOWN_FIELDS =
STRUCTURED_FIELDS + ['comments', 'subject']
FIELD_ORDER =
%w[ return-path received
resent-date resent-from resent-sender resent-to
resent-cc resent-bcc resent-message-id
date from sender reply-to to cc bcc
message-id in-reply-to references
subject comments keywords
mime-version content-type content-transfer-encoding
content-location content-disposition content-description ]

Constants included from Patterns

Patterns::ATOM_UNSAFE, Patterns::CONTROL_CHAR, Patterns::CRLF, Patterns::FIELD_BODY, Patterns::FIELD_LINE, Patterns::FIELD_NAME, Patterns::FWS, Patterns::HEADER_LINE, Patterns::PHRASE_UNSAFE, Patterns::QP_SAFE, Patterns::QP_UNSAFE, Patterns::TEXT, Patterns::TOKEN_UNSAFE, Patterns::WSP

Instance Method Summary collapse

Constructor Details

#initialize(name, value = nil, charset = 'utf-8') ⇒ Field

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'}])


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mail/field.rb', line 70

def initialize(name, value = nil, charset = 'utf-8')
  case
  when name =~ /:/ && value.blank?   # Field.new("field-name: field data")
    name, value = split(name)
    create_field(name, value, charset)
  when name !~ /:/ && value.blank?  # Field.new("field-name")
    create_field(name, nil, charset)
  else                              # Field.new("field-name", "value")
    create_field(name, value, charset)
  end
  return self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



121
122
123
# File 'lib/mail/field.rb', line 121

def method_missing(name, *args, &block)
  field.send(name, *args, &block)
end

Instance Method Details

#<=>(other) ⇒ Object



115
116
117
118
119
# File 'lib/mail/field.rb', line 115

def <=>( other )
  self_order = FIELD_ORDER.rindex(self.name.to_s.downcase) || 100
  other_order = FIELD_ORDER.rindex(other.name.to_s.downcase) || 100
  self_order <=> other_order
end

#fieldObject



87
88
89
# File 'lib/mail/field.rb', line 87

def field
  @field
end

#field=(value) ⇒ Object



83
84
85
# File 'lib/mail/field.rb', line 83

def field=(value)
  @field = value
end

#nameObject



91
92
93
# File 'lib/mail/field.rb', line 91

def name
  field.name
end

#same(other) ⇒ Object



111
112
113
# File 'lib/mail/field.rb', line 111

def same( other )
  match_to_s(other.name, field.name)
end

#to_sObject



103
104
105
# File 'lib/mail/field.rb', line 103

def to_s
  field.to_s
end

#update(name, value) ⇒ Object



107
108
109
# File 'lib/mail/field.rb', line 107

def update(name, value)
  create_field(name, value, charset)
end

#valueObject



95
96
97
# File 'lib/mail/field.rb', line 95

def value
  field.value
end

#value=(val) ⇒ Object



99
100
101
# File 'lib/mail/field.rb', line 99

def value=(val)
  create_field(name, val, charset)
end