Sha256: d6c0b4e31265be4fa90e1e96e25fadeaeece25e7ea87a8b142b7b25a89bae7c4

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 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?(" :")
          @cmd, *@args, @content = str.split(" ", str[0...str.index(" :")+1].count(" ") + 1)
          @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

3 entries across 3 versions & 1 rubygems

Version Path
potato-0.0.10 lib/potato/irc/packet.rb
potato-0.0.7 lib/potato/irc/packet.rb
potato-0.0.6 lib/potato/irc/packet.rb