lib/lens_protocol/oma/parser.rb in lens_protocol-0.1.3 vs lib/lens_protocol/oma/parser.rb in lens_protocol-0.2.0
- old
+ new
@@ -1,24 +1,32 @@
module LensProtocol
module OMA
class Parser
def parse oma_str, types: {}
types = TYPES.merge(types)
- normalize_line_endings(oma_str)
- .split("\n")
- .reduce(Message.new) { |message, line| parse_line line, message, types }
+ lines = convert_to_structured_lines normalize_line_endings(oma_str).split("\n")
+ parse_lines lines, types, Message.new
end
private
- def parse_line line, message, types
- raise ParsingError.new('The label separator is missing', line) unless line.include?('=')
- label, = line.split('=')
- types[label].parse(line, message)
- end
-
def normalize_line_endings str
str.to_s.gsub /\r\n?/, "\n"
+ end
+
+ def convert_to_structured_lines lines
+ lines.map { |line| Line.parse line }
+ end
+
+ def parse_lines lines, types, message
+ line, *next_lines = lines
+ if line
+ value, next_lines = types[line.label].parse(line, next_lines)
+ message.add_record(line.label, value) if value
+ parse_lines next_lines, types, message
+ else
+ message
+ end
end
end
end
end