Class: Teleinfo::Parser
- Inherits:
-
Object
- Object
- Teleinfo::Parser
- Defined in:
- lib/teleinfo.rb
Instance Method Summary collapse
- #each ⇒ Object
- #frames ⇒ Object
-
#initialize(file) ⇒ Parser
constructor
A new instance of Parser.
- #next(only_valid: true, store_frame: false) ⇒ Object
-
#read_all ⇒ Object
Warning: for STDIN, will wait for EOF.
- #stop! ⇒ Object
Constructor Details
#initialize(file) ⇒ Parser
Returns a new instance of Parser.
8 9 10 11 12 13 |
# File 'lib/teleinfo.rb', line 8 def initialize(file) fail ArgumentError.new('Must respond to #readline') unless file.respond_to?('readline') @file = file @frames = [] @fully_parsed = false # indicate EOF readched end |
Instance Method Details
#each ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/teleinfo.rb', line 58 def each count = 0 until (frame = self.next).nil? || @fully_parsed yield frame if block_given? count += 1 end count end |
#frames ⇒ Object
67 68 69 |
# File 'lib/teleinfo.rb', line 67 def frames @fully_parsed ? @frames : @frames.dup end |
#next(only_valid: true, store_frame: false) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/teleinfo.rb', line 15 def next(only_valid: true, store_frame: false) begin frame = [] until @line =~ /ADCO/ @line = @file.readline end frame << @line.chomp if @line.length > 6 @line = @file.readline until @line =~ /ADCO/ frame << @line.chomp if @line.length > 6 @line = @file.readline end teleinfo_frame = Teleinfo::Frame.new(frame) if teleinfo_frame.errors.empty? @frames << teleinfo_frame if store_frame elsif only_valid && !@fully_parsed raise teleinfo_frame.errors.inspect teleinfo_frame = self.next(true) end teleinfo_frame rescue EOFError => error @fully_parsed = true @frames.freeze if store_frame nil end end |
#read_all ⇒ Object
Warning: for STDIN, will wait for EOF
43 44 45 46 47 48 49 50 51 |
# File 'lib/teleinfo.rb', line 43 def read_all unless @fully_parsed last_frame = self.next(store_frame: true) until last_frame.nil? || @fully_parsed self.next(store_frame: true) end end @frames.length end |
#stop! ⇒ Object
53 54 55 56 |
# File 'lib/teleinfo.rb', line 53 def stop! @fully_parsed = true self end |