Last active
December 11, 2015 14:58
-
-
Save NuckChorris/4617240 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 :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" | |
| unless head[0].contains? "=" | |
| cmd = head.shift.split " " | |
| @cmd = cmd.shift | |
| @param = cmd.shift | |
| end | |
| head.each do |line| | |
| if line.contains? "=" | |
| splat = line.split "=", 2 | |
| @args[splat[0]] = splat[1] | |
| end | |
| 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