class Rubbis::Protocol
Public Class Methods
marshal(ruby)
click to toggle source
# File lib/rubbis/protocol.rb, line 6 def marshal(ruby) case ruby when Symbol then "+#{ruby.to_s.upcase}\r\n" when nil then "$-1\r\n" when Integer then ":#{ruby}\r\n" when String then "$#{ruby.length}\r\n#{ruby}\r\n" when Error then "-ERR #{ruby.message}\r\n" when Array then "*#{ruby.length}\r\n#{ruby.map {|x| marshal(x)}.join}" else raise "Dont know how to marshal #{ruby}" end end
safe_readline(io)
click to toggle source
# File lib/rubbis/protocol.rb, line 49 def safe_readline(io) io.readline("\r\n").tap do |line| raise EOFError unless line.end_with?("\r\n") end end
safe_readpartial(io, length)
click to toggle source
# File lib/rubbis/protocol.rb, line 55 def safe_readpartial(io, length) io.readpartial(length).tap do |data| raise EOFError unless data.length == length end end
unmarshal(data)
click to toggle source
# File lib/rubbis/protocol.rb, line 18 def unmarshal(data) io = StringIO.new(data) result = [] processed = 0 begin loop do header = safe_readline(io) raise ProtocolError unless header[0] == '*' n = header[1..-1].to_i result << n.times.map do raise ProtocolError unless io.readpartial(1) == '$' length = safe_readline(io).to_i safe_readpartial(io, length).tap do safe_readline(io) end end processed = io.pos end rescue ProtocolError processed = io.pos rescue EOFError end [result, processed] end