Sha256: e9455e0249cb5c3ff8906c6e4ec5c0050cd7939d6aa7d13893d35ba588a0c15e

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

module Potato
  module IRC
    # A line from an IRC client.
    class Packet
      # The first word of the packet, usually all-caps.
      # @return [String]
      attr_reader :cmd
      
      # Any words in the packet that aren't part of the content.
      # @return [Array<String>]
      attr_reader :args
      
      # Content of the packet (after a colon).
      # @return [String, nil]
      attr_reader :content
      
      # The actual packet string.
      # @return [String]
      attr_reader :raw
      
      def initialize str
        str = str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
        @raw = str
        if str.include?(" :")
          pieces    = str.split(" ", str[0...str.index(" :")+1].count(" ") + 1)
          @cmd      = pieces.shift
          @content  = pieces.pop
          @args     = pieces
          @content.slice!(0)
        else
          @cmd, *@args = str.split(" ")
        end
        @cmd.upcase!
        @content = @args.pop(@args.size - 1).join(" ") if @cmd == "PRIVMSG" && @content.nil?
      end
      
      # The first argument whose first character is an octothorpe (#), or nil if none is found.
      # @return [String]
      def room
        @args.find{|a|a =~ /^#/}[1..-1]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
potato-0.0.16 lib/potato/irc/packet.rb
potato-0.0.15 lib/potato/irc/packet.rb
potato-0.0.14 lib/potato/irc/packet.rb
potato-0.0.13 lib/potato/irc/packet.rb
potato-0.0.12 lib/potato/irc/packet.rb
potato-0.0.11 lib/potato/irc/packet.rb