Skip to content

Sample usage in a real world example

rob mathews edited this page Jun 22, 2021 · 2 revisions

Here we generate a CSV file from a 834 Eligibility file. This successfully parsed 300K lines in about 2.5 minutes.

  def convert
    count = 0
    parser = X12::Parser.new('834.xml');
    CSV.open(temp_file, "w", write_headers: true, headers: HEADERS) do |csv|
      parser.parse("834", File.read("/path/to-file.txt", "L2000") do |l2000|
        row={}
        row["first_name"]=l2000.L2100A.NM1.NameFirst
        row["last_name"] =l2000.L2100A.NM1.NameLastOrOrganizationName
        row[phone_number_key(l2000.L2100A.PER)]=l2000.L2100A.PER.CommunicationNumber1 unless l2000.L2100A.PER.CommunicationNumber1.blank?
        row["address"]   =l2000.L2100A.N3.AddressInformation1
        row["address2"]  =l2000.L2100A.N3.AddressInformation2
        row["city"]      =l2000.L2100A.N4.CityName
        row["state"]     =l2000.L2100A.N4.StateOrProvinceCode
        row["zip"]       =l2000.L2100A.N4.PostalCode
        row["dob"]       =l2000.L2100A.DMG.DateTimePeriod
        row["gender"]    =l2000.L2100A.DMG.GenderCode
        row["language"]  =l2000.L2100A.LUI.IdentificationCode
        l2000.L2300.DTP.each do |dtp|
          case dtp.DateTimeQualifier
          when '348' then row["effective_from"] = dtp.DateTimePeriod
          when '349' then row["effective_until"] = dtp.DateTimePeriod
          end
        end
        l2000.REF.each do |ref|
          id = ref.ReferenceIdentification
          case ref.ReferenceIdentificationQualifier
          when '0F' then row["subscriber_identifier"] = id
          when '1L' then row["member_number"] = id
          when '23' then row["member_supplemental_identifier"] = id
          end
        end
        csv << row
        count+=1
        putc "." if(count % 100) == 0
        puts "\n#{count}\n" if(count % 1000) == 0
      end
      puts "#{count} records processed"
    end
    self
  end
Clone this wiki locally