Class: MIME::Content::Text::Plain::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/safrano/multipart.rb

Overview

Parser for Text::Plain

Constant Summary collapse

HMD_RGX =
/^([\w-]+)\s*:\s*(.*)/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Parser

Returns a new instance of Parser.



201
202
203
204
205
# File 'lib/safrano/multipart.rb', line 201

def initialize(target)
  @state = :h
  @lines = []
  @target = target
end

Instance Method Details

#addline(line) ⇒ Object



207
208
209
# File 'lib/safrano/multipart.rb', line 207

def addline(line)
  @lines << line
end

#parse(level: 0) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/safrano/multipart.rb', line 222

def parse(level: 0)
  return unless @lines

  @level = level
  @lines.each do |line|
    case @state
    when :h
      parse_head(line)
    when :b
      @target.content << line
    end
  end
  @lines = nil
  @target.level = level
  @target
end

#parse_head(line) ⇒ Object



211
212
213
214
215
216
217
218
219
220
# File 'lib/safrano/multipart.rb', line 211

def parse_head(line)
  if (hmd = HMD_RGX.match(line))
    @target.hd[hmd[1].downcase] = hmd[2].strip
  elsif CRLF == line
    @state = :b
  else
    @target.content << line
    @state = :b
  end
end