class SimpleHL7Parser::HL7

Constants

SEGMENT_MAP

Public Class Methods

parse(hl7) click to toggle source
# File lib/simple_hl7_parser/hl7.rb, line 18
def self.parse(hl7)
  current_parent_obr = nil
  segments = hl7.split("\n").map do |line|
    segment_data = line.split('|')
    segment_type = segment_data.first
    current_parent_obr = nil unless %w[OBR OBX NTE].include?(segment_type)

    if klass = SEGMENT_MAP[segment_type.to_sym]
      if segment_type == 'OBR'
        current_parent_obr = klass.new(segment_data)
        next current_parent_obr
      end

      klass.new(segment_data, current_parent_obr)
    end
  end

  HL7Message.new(segments.compact)
end