class Rets::Parser::Multipart

Inspired by Mail.

Constants

CRLF
HEADER_LINE
Part
WSP

Public Class Methods

check_for_invalids_parts!(parts) click to toggle source
# File lib/rets/parser/multipart.rb, line 33
def self.check_for_invalids_parts!(parts)
  return unless parts.length == 1 && parts.first.headers['content-type'] == 'text/xml'
  ErrorChecker.check(parts.first)
end
parse(raw, boundary) click to toggle source
# File lib/rets/parser/multipart.rb, line 11
def self.parse(raw, boundary)
  parts = []
  boundary_regexp = /--#{Regexp.quote(boundary)}(--)?#{CRLF}/

  # WTF some RETS servers declare response body including jpeg binary is encoded in utf8
  raw.force_encoding 'ascii-8bit' if raw.respond_to?(:force_encoding)

  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| p = kv.split(/:\s?/); [p[0].downcase, p[1..-1].join(':')] }
      headers = Hash[*headers.flatten]
      parts << Part.new(headers, body_part)
    else
      next # not a valid chunk.
    end
  end
  check_for_invalids_parts!(parts)
  parts
end