Class: Revactor::Filter::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/revactor/filters/line.rb

Overview

A filter for line based protocols which are framed using LF or CRLF encoding, such as IRC. Both LF and CRLF are supported and no validation is done on bare LFs for CRLF encoding. The output is chomped and delivered without any newline.

Constant Summary collapse

MAX_LENGTH =

Maximum length of a single line

1048576

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Line

Create a new Line filter. Accepts the following options:

delimiter: A character to use as a delimiter.  Defaults to "\n"
           Character sequences are not supported.

maxlength: Maximum length of a line


23
24
25
26
27
# File 'lib/revactor/filters/line.rb', line 23

def initialize(options = {})
  @input = ''
  @delimiter = options[:delimiter] || "\n"
  @size_limit = options[:maxlength] || MAX_LENGTH
end

Instance Method Details

#decode(data) ⇒ Object

Callback for processing incoming lines



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/revactor/filters/line.rb', line 30

def decode(data)
  lines = data.split @delimiter, -1
  
  if @size_limit and @input.size + lines.first.size > @size_limit
    raise 'input buffer full' 
  end
  
  @input << lines.shift
  return [] if lines.empty?
  
  lines.unshift @input
  @input = lines.pop
  
  lines.map(&:chomp)
end

#encode(*data) ⇒ Object

Encode lines using the current delimiter



47
48
49
# File 'lib/revactor/filters/line.rb', line 47

def encode(*data)
  data.inject("") { |str, d| str << d << @delimiter }
end