Class: Mbox::Mail::Headers
- Inherits:
-
Object
- Object
- Mbox::Mail::Headers
show all
- Includes:
- Enumerable
- Defined in:
- lib/mbox/mail/headers.rb,
lib/mbox/mail/headers/status.rb,
lib/mbox/mail/headers/content_type.rb
Defined Under Namespace
Classes: ContentType, Status
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(start = {}) ⇒ Headers
Returns a new instance of Headers.
52
53
54
55
56
|
# File 'lib/mbox/mail/headers.rb', line 52
def initialize (start = {})
@data = {}
merge! start
end
|
Class Method Details
.name_to_symbol(name) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/mbox/mail/headers.rb', line 28
def self.name_to_symbol (name)
return name if name.is_a? Symbol
name = name.to_s.downcase.gsub('-', '_').to_sym
if name.empty?
raise ArgumentError, 'cannot pass empty name'
end
name
end
|
.parse(input) ⇒ Object
46
47
48
|
# File 'lib/mbox/mail/headers.rb', line 46
def self.parse (input)
new.parse(input)
end
|
.symbol_to_name(name) ⇒ Object
40
41
42
43
44
|
# File 'lib/mbox/mail/headers.rb', line 40
def self.symbol_to_name (name)
name.to_s.downcase.gsub('_', '-').gsub(/(\A|-)(.)/) {|match|
match.upcase
}
end
|
Instance Method Details
#[](name) ⇒ Object
62
63
64
|
# File 'lib/mbox/mail/headers.rb', line 62
def [] (name)
@data[Headers.name_to_symbol(name)]
end
|
#[]=(name, value) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/mbox/mail/headers.rb', line 66
def []= (name, value)
name = Headers.name_to_symbol(name)
value = case name
when :status then Status.parse(value)
when :content_type then ContentType.parse(value)
else value
end
if tmp = @data[name] && !tmp.is_a?(Array)
@data[name] = [tmp]
end
if @data[name].is_a?(Array)
@data[name] << value
else
@data[name] = value
end
end
|
#delete(name) ⇒ Object
86
87
88
|
# File 'lib/mbox/mail/headers.rb', line 86
def delete (name)
@data.delete(Headers.name_to_symbol(name))
end
|
#each(&block) ⇒ Object
58
59
60
|
# File 'lib/mbox/mail/headers.rb', line 58
def each (&block)
@data.each(&block)
end
|
#merge(other) ⇒ Object
98
99
100
|
# File 'lib/mbox/mail/headers.rb', line 98
def merge (other)
clone.merge!(other)
end
|
#merge!(other) ⇒ Object
90
91
92
93
94
95
96
|
# File 'lib/mbox/mail/headers.rb', line 90
def merge! (other)
other.each {|name, value|
self[name] = value
}
self
end
|
#parse(input) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/mbox/mail/headers.rb', line 102
def parse (input)
input = if input.respond_to? :to_io
input.to_io
elsif input.is_a? String
StringIO.new(input)
else
raise ArgumentError, 'I do not know what to do.'
end
last = nil
until input.eof? || (line = input.readline).chomp.empty?
if !line.match(/^\s/)
next unless matches = line.match(/^([^:]+):\s*(.+)$/)
whole, name, value = matches.to_a
self[name] = value
last = name
elsif self[last]
if self[last].is_a?(String)
self[last] << " #{line}"
elsif self[last].is_a?(Array)
self[last].last << " #{line}"
end
end
end
self
end
|
#to_s ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/mbox/mail/headers.rb', line 133
def to_s
result = ''
each {|name, values|
[values].flatten.each {|value|
result << "#{Headers.symbol_to_name(name)}: #{value}\n"
}
}
result
end
|