Class: NMEAPlus::SourceDecoder
- Inherits:
-
Object
- Object
- NMEAPlus::SourceDecoder
- Defined in:
- lib/nmea_plus.rb
Overview
The SourceDecoder is meant as the primary entry point into the NMEAPlus module. It wraps a Decoder object and an IO object, converting IO’s #each_line functionality to #each_message and/or #each_complete_message, which yield Message objects representing the parsed data.
Instance Attribute Summary collapse
-
#throw_on_parse_fail ⇒ bool
False by default.
-
#throw_on_unrecognized_type ⇒ bool
False by default.
Instance Method Summary collapse
-
#each_complete_message {|NMEAPlus::Message| ... } ⇒ void
Attempts to group multipart NMEA messages into chains, and executes the block once for every complete chain.
-
#each_message {|NMEAPlus::Message| ... } ⇒ void
Executes the block for every valid NMEA message in the source stream.
-
#initialize(line_reader) ⇒ SourceDecoder
constructor
A new instance of SourceDecoder.
Constructor Details
#initialize(line_reader) ⇒ SourceDecoder
Returns a new instance of SourceDecoder.
26 27 28 29 30 31 32 33 |
# File 'lib/nmea_plus.rb', line 26 def initialize(line_reader) unless line_reader.respond_to? :each_line fail ArgumentError, "line_reader must inherit from type IO (or implement each_line)" end @throw_on_parse_fail = false @source = line_reader @decoder = NMEAPlus::Decoder.new end |
Instance Attribute Details
#throw_on_parse_fail ⇒ bool
False by default.
19 20 21 |
# File 'lib/nmea_plus.rb', line 19 def throw_on_parse_fail @throw_on_parse_fail end |
#throw_on_unrecognized_type ⇒ bool
False by default. Typically for development.
23 24 25 |
# File 'lib/nmea_plus.rb', line 23 def throw_on_unrecognized_type @throw_on_unrecognized_type end |
Instance Method Details
#each_complete_message {|NMEAPlus::Message| ... } ⇒ void
This method returns an undefined value.
Attempts to group multipart NMEA messages into chains, and executes the block once for every complete chain.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/nmea_plus.rb', line 81 def partials = {} # hash of message type to message-chain-in-progress do |msg| slot = msg.data_type # the slot in the hash if partials[slot].nil? # no message in there partials[slot] = msg elsif 1 != (msg. - partials[slot].) # broken sequence # error! just overwrite what was there partials[slot] = msg else # chain on to what's there partials[slot].(msg) end # take action if we've completed the chain maybe_full = partials[slot] if maybe_full. partials[slot] = nil yield maybe_full end end end |
#each_message {|NMEAPlus::Message| ... } ⇒ void
This method returns an undefined value.
Executes the block for every valid NMEA message in the source stream. In practice, you should use #each_complete_message unless you have a very compelling reason not to.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/nmea_plus.rb', line 49 def @source.each_line do |line| if @throw_on_parse_fail yield @decoder.parse(line) else got_error = false begin y = @decoder.parse(line) rescue got_error = true end yield y unless got_error end end end |