Class: Rets::Parser::Multipart

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

Overview

Inspired by Mail.

Defined Under Namespace

Classes: Part

Constant Summary collapse

CRLF =
"\r\n"
WSP =
"\s"
HEADER_LINE =
/^([!-9;-~]+:\s*.+)$/

Class Method Summary collapse

Class Method Details

.parse(raw, boundary) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rets/parser/multipart.rb', line 13

def self.parse(raw, boundary)
  parts = []

  boundary_regexp = /--#{Regexp.quote(boundary)}(--)?#{CRLF}/

    raw.split(boundary_regexp).each do |chunk|

      header_part, body_part = chunk.split(/#{CRLF}#{WSP}*#{CRLF}/m, 2)

      if header_part =~ HEADER_LINE
        headers = header_part.split(/\r\n/).map { |kv| k,v = kv.split(/:\s?/); [k.downcase, v] }
        headers = Hash[*headers.flatten]

        parts << Part.new(headers, body_part)
      else
        next # not a valid chunk.
      end
    end

  parts
end