Skip to content

Instantly share code, notes, and snippets.

@NuckChorris
Created January 24, 2013 02:40
Show Gist options
  • Select an option

  • Save NuckChorris/4617231 to your computer and use it in GitHub Desktop.

Select an option

Save NuckChorris/4617231 to your computer and use it in GitHub Desktop.
class Packet
attr :cmd, :param, :args, :body
def sub
unless @body.nil?
body.split("\n\n").map { |sub| Packet.new sub }
else
[]
end
end
def raw
pkt = "#{@cmd} #{@param}"
@args.each do |key, val|
pkt << "\n"
pkt << "#{key}=#{val}"
end
unless @body.nil? || @body.empty?
pkt << "\n"
pkt << @body
end
pkt << "\n"
end
def initialize (pkt)
@args = {}
@sub = []
if pkt.is_a? Hash
@cmd = pkt[:cmd]
@param = pkt[:param]
@args = pkt[:args]
@sub = pkt[:sub]
elsif pkt.is_a? String
parts = pkt.split "\n\n", 2
head = parts.shift.split "\n"
if head[0]['='] == nil
cmd = head.shift.split " "
@cmd = cmd.shift
@param = cmd.shift
end
head.each do |line|
splat = line.split "=", 2
@args[splat[0]] = splat[1]
end
@body = parts.shift || ""
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment