Module: IRC::Parser

Defined in:
lib/on_irc/parser.rb

Class Method Summary collapse

Class Method Details

.parse(line) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/on_irc/parser.rb', line 3

def self.parse(line)      
  prefix = ''
  command = ''
  params = []
  msg = StringScanner.new(line)
  
  if msg.peek(1) == ':'
    msg.pos += 1
    prefix = msg.scan /\S+/
    msg.skip /\s+/
  end
  
  command = msg.scan /\S+/
  
  until msg.eos?
    msg.skip /\s+/
    
    if msg.peek(1) == ':'
      msg.pos += 1
      params << msg.rest
      msg.terminate
    else
      params << msg.scan(/\S+/)
    end
  end
  
  {:prefix => prefix, :command => command, :params => params}
end