# class Mail::UnstructuredField

Provides access to an unstructured header field

### Per RFC 2822:¶ ↑

    2.2.1. Unstructured Header Field Bodies

       Some field bodies in this standard are defined simply as
       "unstructured" (which is specified below as any US-ASCII characters,
       except for CR and LF) with no further restrictions.  These are
       referred to as unstructured field bodies.  Semantically, unstructured
       field bodies are simply to be treated as a single line of characters
       with no further processing (except for header "folding" and
       "unfolding" as described in section 2.2.3).

### Attributes

charset[RW]
errors[R]

### Public Class Methods

new(name, value, charset = nil) click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 24
    def initialize(name, value, charset = nil)
      @errors = []

      if value.is_a?(Array)
        # Probably has arrived here from a failed parse of an AddressList Field
        value = value.join(', ')
      else
        # Ensure we are dealing with a string
        value = value.to_s
      end

      if charset
        self.charset = charset
      else
        if value.respond_to?(:encoding)
          self.charset = value.encoding
        else
          self.charset = $KCODE
        end
      end
      self.name = name
      self.value = value
      self
    end

### Public Instance Methods

decoded() click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 53
    def decoded
      do_decode
    end

default() click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 57
    def default
      decoded
    end

encoded() click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 49
    def encoded
      do_encode
    end

parse() click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 61
    def parse # An unstructured field does not parse
      self
    end

### Private Instance Methods

do_decode() click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 71
    def do_decode
      Utilities.blank?(value) ? nil : Encodings.decode_encode(value, :decode)
    end

do_encode() click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 67
    def do_encode
      value.nil? ? '' : "#{wrapped_value}\r\n"
    end

encode(value) click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 179
    def encode(value)
      value = [value].pack(CAPITAL_M).gsub(EQUAL_LF, EMPTY)
      value.gsub!(/"/,  '=22')
      value.gsub!(/\(/, '=28')
      value.gsub!(/\)/, '=29')
      value.gsub!(/\?/, '=3F')
      value.gsub!(/_/,  '=5F')
      value.gsub!(/ /,  '_')
      value
    end

encode_crlf(value) click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 190
    def encode_crlf(value)
      value.gsub!(CR, CR_ENCODED)
      value.gsub!(LF, LF_ENCODED)
      value
    end

normalized_encoding() click to toggle source

    # File lib/mail/fields/unstructured_field.rb, line 196
    def normalized_encoding
      encoding = charset.to_s.upcase.gsub('_', '-')
      encoding = 'UTF-8' if encoding == 'UTF8' # Ruby 1.8.x and $KCODE == 'u'
      encoding
    end

wrap_lines(name, folded_lines) click to toggle source

6.2. Display of 'encoded-word's

    When displaying a particular header field that contains multiple
    'encoded-word's, any 'linear-white-space' that separates a pair of
    adjacent 'encoded-word's is ignored.  (This is to allow the use of
    multiple 'encoded-word's to represent long strings of unencoded text,
    without having to separate 'encoded-word's where spaces occur in the
    unencoded text.)

    # File lib/mail/fields/unstructured_field.rb, line 113
    def wrap_lines(name, folded_lines)
      result = ["#{name}: #{folded_lines.shift}"]
      result.concat(folded_lines)
      result.join("\r\n\s")
    end