Sha256: 8e6a6c229e04e51be238276533abde5466cc321f8ae709cab1e4e1c3396e9f23

Contents?: true

Size: 839 Bytes

Versions: 1

Compression:

Stored size: 839 Bytes

Contents

require 'csv'

module Valigator
  module CSV
    class Validator
      attr_reader :filename, :errors



      def initialize(filename)
        @filename = filename
        @errors = []
      end



      def validate(options = {})
        ::CSV.foreach(@filename, build_options(options)) { |_row|}
      rescue ::CSV::MalformedCSVError, ArgumentError => error
        raise if unrelated_error?(error)

        @errors << CSV::Error.new(error)
      end



      private

      def build_options(options = {})
        {
          col_sep: options[:col_sep] || ',',
          quote_char: options[:quote_char] || '"',
          encoding: options[:encoding] || 'UTF-8'
        }
      end



      def unrelated_error?(error)
        error.is_a?(ArgumentError) && error.message != 'invalid byte sequence in UTF-8'
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
valigator-csv-1.2.0 lib/valigator/csv/validator.rb