Class: Shades::StreamParser

Inherits:
Object
  • Object
show all
Defined in:
lib/shades/streamparser.rb

Overview

parse a stream of events with whitespace delimited fields preceeded by metadata headers

Instance Method Summary collapse

Constructor Details

#initialize(&receiver) ⇒ StreamParser

Returns a new instance of StreamParser.



5
6
7
8
9
# File 'lib/shades/streamparser.rb', line 5

def initialize(&receiver)
  @dimensions = nil
  @measures = nil
  @receiver = receiver
end

Instance Method Details

#<<(line) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shades/streamparser.rb', line 11

def <<(line)
  line.strip!
  if !@metadata.nil?
    event = @metadata.parse_event(line, /\t/)
    if event.nil?
      puts line
    else
      @receiver.call(event)
    end
  elsif line.start_with?("#")
    parts = line.scan(/[\w\.]+/)
    if parts[0].eql?("dimensions")
      @dimensions = parts.drop(1)
    elsif parts[0].eql?("measures")
      @measures = parts.drop(1)
    end
    if !@dimensions.nil? && !@measures.nil?
      @metadata = Shades::Metadata.new(@dimensions, @measures)
    end
  else
    $stderr.puts "discarding line received before metadata"
  end
end