Class: Telemetry::StreamParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ StreamParser

Returns a new instance of StreamParser.



56
57
58
59
60
61
62
63
64
65
# File 'lib/StreamParser.rb', line 56

def initialize( payload )
    @r = JSON.parse( payload )
    
    raise VersionNotSpecifiedError.new if @r['v'].nil?
    @version = @r['v']
    
    self.send "parse#{version}"
    
    return self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



26
27
28
29
30
# File 'lib/StreamParser.rb', line 26

def method_missing( meth, *args, &block )
    raise VersionNotSupportedError.new if meth[0,5] == "parse"
    
    raise NoMethodError.new( "method: #{meth}" )
end

Instance Attribute Details

#all_fieldsObject (readonly)

Returns the value of attribute all_fields.



24
25
26
# File 'lib/StreamParser.rb', line 24

def all_fields
  @all_fields
end

#listObject (readonly)

Returns the value of attribute list.



24
25
26
# File 'lib/StreamParser.rb', line 24

def list
  @list
end

#versionObject (readonly)

Returns the value of attribute version.



24
25
26
# File 'lib/StreamParser.rb', line 24

def version
  @version
end

Instance Method Details

#parse1Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/StreamParser.rb', line 32

def parse1
    raise MissingFormatError.new if @r['f'].nil?
    raise InvalidFormatError.new if @r['f'].length == 0
    raise MissingListError.new if @r['l'].nil?

    @defaults = @r['d'] || {}

    @list = Array.new
    format = @r['f']

    @all_fields = @defaults.keys + @r['f']

    #Break list up into chunks, each chunk being the size of the format record
    @r['l'].each_slice( format.length ).with_index do |el,idx|

        obj = @defaults.clone
        format.each_with_index do |name,idx|
            obj[name] = el[idx]
        end
        @list << obj
    end

end