Skip to content

Instantly share code, notes, and snippets.

@NuckChorris
Created January 21, 2013 09:37
Show Gist options
  • Select an option

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

Select an option

Save NuckChorris/4584874 to your computer and use it in GitHub Desktop.
class Packet
attr_reader :cmd, :param, :args, :body, :sub, :depth, :raw
def initialize (pkt)
@args = {}
@sub = []
if pkt.is_a? Hash
@cmd = pkt[:cmd]
@param = pkt[:param]
@args = pkt[:args]
@body = pkt[:body]
@sub = pkt[:sub]
@raw = pkt[:raw]
elsif pkt.is_a? String
@raw = pkt
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 || ""
@body.split("\n\n").each do |sub|
@sub.push Packet.new(sub)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment