Class: Sponge::IRC::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sponge/irc/parser.rb

Overview

Author

Description

Synopsis

Defined Under Namespace

Classes: BadMessageFormat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Parser

Returns a new instance of Parser.



18
19
20
# File 'lib/sponge/irc/parser.rb', line 18

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Our IRC::Client instance



16
17
18
# File 'lib/sponge/irc/parser.rb', line 16

def client
  @client
end

Instance Method Details

#parse(data) ⇒ Object

Parses a raw IRC server message and returns a nicely encapsulated IRC::Message for you to play with

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sponge/irc/parser.rb', line 24

def parse(data)
  return unless data
  raise(BadMessageFormat, "Unknown Message") unless matches = data.match(/\A(?:\:(\S+)\s)?([A-Z0-9]+?)\s(.+?)\Z/)
  prefix, command, params = matches.captures
  
  message = IRC::Message.new(client, data, prefix, command, params)
  message.for = message.params.first
  message.text = message.params.last
  
  if prefix && usermatch = prefix.match(/^(\S+?)!(\S+?)@(\S+?)$/)
    message.from = IRC::User.new(client, *usermatch.captures)
    message.nick = message.from.nick
  end
  
  message
end