Sha256: 7abdf2fca4e8144177707d740bd0d72a6e1b6ee2c9a72841462d0715f1389fc0

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'csv'

module EfoNelfo
  module Reader

    class CSV
      ENCODING = Encoding::ISO_8859_1

      CSV_OPTIONS = {
        col_sep: ';',
        headers: false,
        row_sep: "\r\n",
        quote_char: "\x00",
        force_quotes: false,
        skip_blanks: true
      }

      attr_reader :csv, :data

      def initialize(options)
        if options[:filename]
          @data = File.read(options[:filename], encoding: ENCODING)
        else
          @data = options[:data]
        end

        @csv = ::CSV.new @data, CSV_OPTIONS
      end

      def parse
        # Create the head object based on the first row
        head = parse_head first
        head.source = @data

        # Read rest of the file and add them to the head
        csv.each do |row|
          # Find the correct posttype module for given posttype and version
          klass = EfoNelfo::PostType.for row[0], head.version
          next if klass.nil?

          head.add klass.new(row)
        end

        head
      end

      # Returns the first row of the csv file
      def first
        csv.first
      end

      private

      def parse_head(row)
        klass = EfoNelfo::PostType.for row[0], row[2]
        raise EfoNelfo::UnsupportedPostType.new("Don't know how to handle v#{row[2]} of #{row[0]}") if klass.nil?
        klass.new row
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
efo_nelfo-1.3.2 lib/efo_nelfo/reader/csv.rb