Class: StreamWrapper::ObservationFilter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/logbox/stream_wrapper/observation_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, filter_options) ⇒ ObservationFilter

Support both strings and streams as input.



12
13
14
15
16
17
18
19
20
21
# File 'lib/logbox/stream_wrapper/observation_filter.rb', line 12

def initialize(input, filter_options)
  input = StringIO.new(input) if input.class == String
  @stream = input
  # Quick check of options. It would be easy to set :valid => false but it is not allowed.
  if filter_options.detect { |key, value| value == false }
    raise "A filter option can not have false as value. Use skip instead." 
  end
  @filter_options = filter_options
  build_filters @filter_options
end

Instance Attribute Details

#observation_countObject (readonly)

Returns the value of attribute observation_count.



9
10
11
# File 'lib/logbox/stream_wrapper/observation_filter.rb', line 9

def observation_count
  @observation_count
end

Instance Method Details

#eachObject

Enumerate over Observations



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logbox/stream_wrapper/observation_filter.rb', line 37

def each
  @observation_count = 0
  while(row = gets)
    begin
      attrs = LogParser.parse_line(row) || next
    rescue LogParser::ParseError => e
      $stderr.puts("#{e} for row:#{row}")
      next
    end
    observation = Observation.new(attrs)
    if (@filter_options[:valid] && observation.valid?) ||
       (@filter_options[:skip_valid] && !observation.valid?) ||
       (@filter_options[:valid].nil? && @filter_options[:skip_valid].nil?)
      yield observation
      @observation_count += 1
    end
  end
end

#eof?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/logbox/stream_wrapper/observation_filter.rb', line 32

def eof?
  @stream.eof?
end

#getsObject

Get next row and remove invalid utf-8 byte sequences.



24
25
26
27
28
29
30
# File 'lib/logbox/stream_wrapper/observation_filter.rb', line 24

def gets
  while row = @stream.gets
    row = Logbox::StringEncoder.iconv(row)
    break if keep? row
  end
  row
end