mbox 0.0.4.2 → 0.0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,66 +29,39 @@ class Mail
29
29
  headers = Mbox::Mail::Headers.new
30
30
  content = Mbox::Mail::Content.new(headers)
31
31
 
32
- inside = {
33
- metadata: true,
34
- headers: false,
35
- content: false
36
- }
37
-
38
- last = {
39
- line: '',
40
- stuff: ''
41
- }
42
-
43
32
  next until input.eof? || (line = input.readline).match(options[:separator])
44
33
 
45
34
  return if !line || line.empty?
46
35
 
47
- metadata.parse_from line
48
-
49
- until input.eof? || ((line = input.readline).match(options[:separator]) && last[:line].empty?)
50
- if inside[:metadata]
51
- if line.match(/^>+/)
52
- metadata.parse_from line
53
- else
54
- inside[:metadata] = false
55
- inside[:headers] = true
56
-
57
- last[:line] = line.chomp
58
-
59
- next
60
- end
61
- elsif inside[:headers]
62
- if line.strip.empty?
63
- inside[:headers] = false
64
- inside[:content] = true
65
-
66
- headers.parse(last[:stuff])
67
-
68
- last[:line] = line.chomp
69
- last[:stuff] = ''
36
+ # metadata parsing
37
+ begin
38
+ break unless line.match(/^>+/)
70
39
 
71
- next
72
- end
40
+ metadata.parse_from line
41
+ end until input.eof? || (line = input.readline).match(options[:separator])
73
42
 
74
- last[:stuff] << line
75
- elsif inside[:content]
76
- if options[:headers_only]
77
- last[:line] = line.chomp
43
+ # headers parsing
44
+ current = ''
45
+ begin
46
+ break if line.strip.empty?
78
47
 
79
- next
80
- end
48
+ current << line
49
+ end until input.eof? || (line = input.readline).match(options[:separator])
50
+ headers.parse(current)
81
51
 
82
- last[:stuff] << line
83
- end
52
+ # content parsing
53
+ current = ''
54
+ until input.eof? || (line = input.readline).match(options[:separator])
55
+ next if options[:headers_only]
84
56
 
85
- last[:line] = line.chomp
57
+ current << line
86
58
  end
87
59
 
88
- unless last[:stuff].empty?
89
- content.parse(last[:stuff])
60
+ unless options[:headers_only]
61
+ content.parse(current.chomp)
90
62
  end
91
63
 
64
+ # put the last separator back in its place
92
65
  if !input.eof? && line
93
66
  input.seek(-line.length, IO::SEEK_CUR)
94
67
  end
@@ -60,15 +60,7 @@ class Content < Array
60
60
  end
61
61
  }
62
62
  else
63
- stream = StringIO.new(text)
64
-
65
- content = (!stream.eof?) ? stream.readline : ''
66
- until stream.eof? || line = stream.readline
67
- content << line
68
- end
69
- content.chomp!
70
-
71
- self << File.new(Headers.new, content)
63
+ self << File.new(headers, text)
72
64
  end
73
65
 
74
66
  self
@@ -18,6 +18,7 @@
18
18
  #++
19
19
 
20
20
  require 'stringio'
21
+ require 'forwardable'
21
22
  require 'call-me/memoize'
22
23
 
23
24
  require 'mbox/mail/headers/status'
@@ -71,18 +72,17 @@ class Headers
71
72
  new.parse(input)
72
73
  end
73
74
 
75
+ extend Forwardable
74
76
  include Enumerable
75
77
 
78
+ def_delegators :@data, :each, :length, :size
79
+
76
80
  def initialize (start = {})
77
81
  @data = {}
78
82
 
79
83
  merge! start
80
84
  end
81
85
 
82
- def each (&block)
83
- @data.each(&block)
84
- end
85
-
86
86
  def [] (name)
87
87
  @data[Name.parse(name)]
88
88
  end
@@ -96,7 +96,7 @@ class Headers
96
96
  value = ContentType.parse(value)
97
97
  end
98
98
 
99
- if tmp = @data[name] && !tmp.is_a?(Array)
99
+ if (tmp = @data[name]) && !tmp.is_a?(Array)
100
100
  @data[name] = [tmp]
101
101
  end
102
102
 
@@ -140,13 +140,13 @@ class Headers
140
140
 
141
141
  whole, name, value = matches.to_a
142
142
 
143
- self[name] = value
143
+ self[name] = value.strip
144
144
  last = name
145
145
  elsif self[last]
146
146
  if self[last].is_a?(String)
147
- self[last] << " #{line}"
147
+ self[last] << " #{line.strip}"
148
148
  elsif self[last].is_a?(Array) && self[last].last.is_a?(String)
149
- self[last].last << " #{line}"
149
+ self[last].last << " #{line.strip}"
150
150
  end
151
151
  end
152
152
  end
@@ -68,6 +68,14 @@ class Mbox
68
68
  if @input.respond_to? :flock
69
69
  @input.flock File::LOCK_SH
70
70
  end
71
+
72
+ if block_given?
73
+ begin
74
+ yield self
75
+ ensure
76
+ unlock
77
+ end
78
+ end
71
79
  end
72
80
 
73
81
  def unlock
@@ -79,29 +87,23 @@ class Mbox
79
87
  def each (opts = {})
80
88
  @input.seek 0
81
89
 
82
- lock
83
-
84
- while mail = Mail.parse(@input, options.merge(opts))
85
- yield mail
86
- end
87
-
88
- unlock
90
+ lock {
91
+ while mail = Mail.parse(@input, options.merge(opts))
92
+ yield mail
93
+ end
94
+ }
89
95
  end
90
96
 
91
97
  def [] (index, opts = {})
92
- lock
93
-
94
- seek index
95
-
96
- if @input.eof?
97
- raise IndexError, "#{index} is out of range"
98
- end
98
+ lock {
99
+ seek index
99
100
 
100
- mail = Mail.parse(@input, options.merge(opts))
101
-
102
- unlock
101
+ if @input.eof?
102
+ raise IndexError, "#{index} is out of range"
103
+ end
103
104
 
104
- mail
105
+ Mail.parse(@input, options.merge(opts))
106
+ }
105
107
  end
106
108
 
107
109
  def seek (to, whence = IO::SEEK_SET)
@@ -135,17 +137,15 @@ class Mbox
135
137
  last = ''
136
138
  length = 0
137
139
 
138
- lock
140
+ lock {
141
+ while line = @input.readline rescue nil
142
+ if line.match(options[:separator]) && last.chomp.empty?
143
+ length += 1
144
+ end
139
145
 
140
- while line = @input.readline rescue nil
141
- if line.match(options[:separator]) && last.chomp.empty?
142
- length += 1
146
+ last = line
143
147
  end
144
-
145
- last = line
146
- end
147
-
148
- unlock
148
+ }
149
149
 
150
150
  length
151
151
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4.2
4
+ version: 0.0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-09 00:00:00.000000000 Z
12
+ date: 2012-05-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: call-me