Class: SMail::Header

Inherits:
Object
  • Object
show all
Defined in:
lib/smail/header.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, smail) ⇒ Header

Returns a new instance of Header.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/smail/header.rb', line 5

def initialize(headers, smail)
  @crlf = smail.crlf || "\r\n"

  @order = Array.new
  @head = Hash.new
  headers.each do |header|
    @order << header.first
    if @head[header.first].nil?
      @head[header.first] = Array.new
    end
    @head[header.first] << header.last
  end

  @header_names = @head.collect {|header,value| [header.downcase, header] }
end

Instance Attribute Details

#crlfObject (readonly)

Returns the value of attribute crlf.



3
4
5
# File 'lib/smail/header.rb', line 3

def crlf
  @crlf
end

#headObject (readonly)

Returns the value of attribute head.



3
4
5
# File 'lib/smail/header.rb', line 3

def head
  @head
end

Instance Method Details

#header(field) ⇒ Object



40
41
42
43
# File 'lib/smail/header.rb', line 40

def header(field)
  return unless names = @header_names.assoc(field.downcase)
  headers(field).first
end

#header_namesObject



25
26
27
# File 'lib/smail/header.rb', line 25

def header_names
  @header_names.collect {|pair| pair.last }
end

#header_pairsObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/smail/header.rb', line 29

def header_pairs
  headers = []
  seen = {}
  seen.default = -1
  @order.each do |header|
    headers << [header, @head[header][seen[header] += 1]]
  end
    
  headers
end

#header_set(field, lines) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/smail/header.rb', line 51

def header_set(field, lines)
  unless field =~ /^[\x21-\x39\x3b-\x7e]+$/
    raise(ArgumentError, "Field name contains illegal characters")
  end
  unless field =~ /^[\w-]+$/
    raise(ArgumentError, "Field name is not limited to hyphens and alphanumerics")
  end

  old_length = 0
  if @header_names.assoc(field.downcase)
    old_length = @head[@header_names.assoc(field.downcase).last].length
  else
    @header_names << [field.downcase, field]
  #  @order << field # FIXME: add new fields onto the end
  end

  @head[@header_names.assoc(field.downcase).last] = lines
  if lines.length < old_length
    # Remove the last x instances of field from @order
    headers_to_remove = old_length - lines.length
    headers_seen = 0
    @order.collect! {|field_name|
      if field_name.downcase == field.downcase
        headers_seen += 1
        next if headers_seen > headers_to_remove
      end
      field_name
    }.compact!
  elsif lines.length > old_length
    # Add x instances of field to the end of @order
    @order = @order + ([field] * (lines.length - old_length))
  end

  headers(field)
end

#headers(field) ⇒ Object



45
46
47
48
49
# File 'lib/smail/header.rb', line 45

def headers(field)
  return unless names = @header_names.assoc(field.downcase)
  
  @head[names.last]
end

#to_sObject

:nodoc:



21
22
23
# File 'lib/smail/header.rb', line 21

def to_s #:nodoc:
  header_pairs.collect {|pair| fold(pair.join(": "))}.join(@crlf) + @crlf
end