module EventMachine::IMAP::ResponseParser

Intercepts the receive_data event and generates receive_response events with parsed data.

Public Instance Methods

post_init() click to toggle source
Calls superclass method
# File lib/em-imap/response_parser.rb, line 6
def post_init
  super
  @parser = Net::IMAP::ResponseParser.new
  @buffer = ""
end
receive_data(data) click to toggle source

This is a translation of Net::IMAP#get_response

# File lib/em-imap/response_parser.rb, line 13
def receive_data(data)
  @buffer << data

  until @buffer.empty?

    eol = @buffer.index(CRLF)
    
    # Include IMAP literals on the same line.
    # The format for a literal is "{8}\r\n........"
    # so the size would be at the end of what we thought was the line.
    # We then skip over that much, and try looking for the next newline.
    # (The newline after a literal is the end of the actual line,
    # there's no termination marker for literals).
    while eol && @buffer[0, eol][/\{(\d+)\}\z/]
      eol = @buffer.index(CRLF, eol + CRLF.size + $1.to_i)
    end

    # The current line is not yet complete, wait for more data.
    return unless eol

    line = @buffer.slice!(0, eol + CRLF.size)

    receive_response parse(line)
  end
end
receive_response(response) click to toggle source

Callback used by receive data.

# File lib/em-imap/response_parser.rb, line 40
def receive_response(response); end

Private Instance Methods

parse(line) click to toggle source
# File lib/em-imap/response_parser.rb, line 44
def parse(line)
  @parser.parse(line)
rescue Net::IMAP::ResponseParseError => e
  fail e
end