Class: Flexo::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/flexo/data.rb

Overview

This is a handy objecty representation of an IRC message. Each line is broken into tiny little pieces, made available to the rest of Flexo through accessors (only readers).

Defined Under Namespace

Classes: Hostmask

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Data

Calls parse() and fills up all the variables with sensible data



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/flexo/data.rb', line 17

def initialize(raw)
  @manager = Flexo::Manager.instance
  data = parse(raw)

  if data == nil
    @manager.logger.error("Unable to parse\n#{raw}")
    return
  end

  @raw      = raw
  @message  = data[:message]
  @origin   = data[:origin]
  @params   = data[:params]
  @string   = data[:string]
  @hostmask = parse_hostmask(@origin) if @origin
  @numeric  = @message =~ /^\d+$/ ? true : false
end

Instance Attribute Details

#hostmaskObject (readonly)

Returns the value of attribute hostmask.



12
13
14
# File 'lib/flexo/data.rb', line 12

def hostmask
  @hostmask
end

#messageObject (readonly)

Returns the value of attribute message.



8
9
10
# File 'lib/flexo/data.rb', line 8

def message
  @message
end

#numericObject (readonly)

Returns the value of attribute numeric.



13
14
15
# File 'lib/flexo/data.rb', line 13

def numeric
  @numeric
end

#originObject (readonly)

Returns the value of attribute origin.



9
10
11
# File 'lib/flexo/data.rb', line 9

def origin
  @origin
end

#paramsObject (readonly)

Returns the value of attribute params.



10
11
12
# File 'lib/flexo/data.rb', line 10

def params
  @params
end

#rawObject (readonly)

Returns the value of attribute raw.



7
8
9
# File 'lib/flexo/data.rb', line 7

def raw
  @raw
end

#stringObject (readonly)

Returns the value of attribute string.



11
12
13
# File 'lib/flexo/data.rb', line 11

def string
  @string
end

Instance Method Details

#inspectObject

:nodoc:



43
44
45
# File 'lib/flexo/data.rb', line 43

def inspect # :nodoc:
  "#<#{self.class}:#{(@raw.size > 40 ? @raw[0..40] + '...' : @raw).inspect}>"
end

#numeric?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/flexo/data.rb', line 35

def numeric?
  @numeric
end

#parse(line) ⇒ Object

Does the actual parsing and splitting and trickery with the received line



49
50
51
52
# File 'lib/flexo/data.rb', line 49

def parse(line)
  m = line.lstrip.chomp.match(/^(?::(\S+) )?(\S+)(?: ([^:].*?[^:]))?(?: :(.*?))?$/)
  {:origin  => m[1], :message => m[2], :params  => m[3] ? m[3].split(' ') : [], :string  => m[4] || ''} if m
end

#parse_hostmask(mask) ⇒ Object

This parses the hostmask, creating an instance of Flexo::Data::Hostmask.



56
57
58
59
60
61
# File 'lib/flexo/data.rb', line 56

def parse_hostmask(mask)
  m = mask.match(/^([^!\s]+)(?:(?:!([^@\s]+))?@(\S+))?$/)
  
  return Hostmask.new(m[1], m[2], m[3]) if m      
  return nil
end

#to_sObject



39
40
41
# File 'lib/flexo/data.rb', line 39

def to_s
  @raw
end