Created
January 21, 2013 09:37
-
-
Save NuckChorris/4584874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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