valigator-csv 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/valigator/csv/error.rb +77 -0
- data/lib/valigator/csv/validator.rb +5 -65
- data/lib/valigator/csv/version.rb +1 -1
- data/lib/valigator.rb +1 -0
- data/valigator-csv.gemspec +2 -4
- metadata +4 -19
- data/bin/console +0 -14
- data/bin/setup +0 -7
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 325bd8b898cc324095f85b90956285b9fd49eb1c
         | 
| 4 | 
            +
              data.tar.gz: dc833a514d767425f97077301ccb0addb3841335
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 5f30e36ff1d0158203df6907dd192915dc07e65ec193a267ac17176727a8dca6d1aaa6b8e0b5cc52f2d99e32d892a3969c1798454297789b03f6f0f9970d9e25
         | 
| 7 | 
            +
              data.tar.gz: 5ed312f29eba227c354f9bcd6e3897227330aca19f6fc4d3b9197f514143269aceaaa53dd0cef59fe9163a697dcc0babf3f0867335b54a0f19074d23bb2d2e37
         | 
| @@ -0,0 +1,77 @@ | |
| 1 | 
            +
            module Valigator
         | 
| 2 | 
            +
              module CSV
         | 
| 3 | 
            +
                class Error
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                  attr_reader :row, :type, :message
         | 
| 6 | 
            +
             | 
| 7 | 
            +
             | 
| 8 | 
            +
             | 
| 9 | 
            +
                  def initialize(error)
         | 
| 10 | 
            +
                    case error
         | 
| 11 | 
            +
                      when Hash
         | 
| 12 | 
            +
                        build_from_hash error
         | 
| 13 | 
            +
                      when StandardError
         | 
| 14 | 
            +
                        build_from_error error
         | 
| 15 | 
            +
                    end
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
             | 
| 19 | 
            +
             | 
| 20 | 
            +
                  def ==(other)
         | 
| 21 | 
            +
                    row == other.row && message == other.message && type == other.type
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
             | 
| 25 | 
            +
             | 
| 26 | 
            +
                  private
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                  def build_from_hash(error)
         | 
| 29 | 
            +
                    build error[:type], error[:message], error[:row]
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
             | 
| 33 | 
            +
             | 
| 34 | 
            +
                  def build_from_error(error)
         | 
| 35 | 
            +
                    build map_to_type(error.message), error.message, determine_row(error.message)
         | 
| 36 | 
            +
                  end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
             | 
| 39 | 
            +
             | 
| 40 | 
            +
                  def determine_row(message)
         | 
| 41 | 
            +
                    matches = /line (?<lineno>\d+)/.match(message)
         | 
| 42 | 
            +
                    matches[:lineno].to_i if matches
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
             | 
| 46 | 
            +
             | 
| 47 | 
            +
                  def build(type, message, row)
         | 
| 48 | 
            +
                    @type = type
         | 
| 49 | 
            +
                    @row = row
         | 
| 50 | 
            +
                    @message = message
         | 
| 51 | 
            +
                  end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
             | 
| 54 | 
            +
             | 
| 55 | 
            +
                  def map_to_type(message)
         | 
| 56 | 
            +
                    case message
         | 
| 57 | 
            +
                      when /Missing or stray quote/
         | 
| 58 | 
            +
                        'stray_quote'
         | 
| 59 | 
            +
                      when /Unquoted fields do not allow/
         | 
| 60 | 
            +
                        'line_breaks'
         | 
| 61 | 
            +
                      when /Illegal quoting/
         | 
| 62 | 
            +
                        'illegal_quoting'
         | 
| 63 | 
            +
                      when /Field size exceeded/
         | 
| 64 | 
            +
                        'field_size'
         | 
| 65 | 
            +
                      when /Unclosed quoted field/
         | 
| 66 | 
            +
                        'unclosed_quote'
         | 
| 67 | 
            +
                      when /invalid byte sequence/
         | 
| 68 | 
            +
                        'invalid_encoding'
         | 
| 69 | 
            +
                      else
         | 
| 70 | 
            +
                        raise ArgumentError, 'unknown type'
         | 
| 71 | 
            +
                    end
         | 
| 72 | 
            +
                  end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
             | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
            end
         | 
| @@ -1,5 +1,4 @@ | |
| 1 | 
            -
            require ' | 
| 2 | 
            -
            require 'active_support/core_ext/string/filters'
         | 
| 1 | 
            +
            require 'csv'
         | 
| 3 2 |  | 
| 4 3 | 
             
            module Valigator
         | 
| 5 4 | 
             
              module CSV
         | 
| @@ -10,74 +9,15 @@ module Valigator | |
| 10 9 |  | 
| 11 10 | 
             
                  def initialize(filename)
         | 
| 12 11 | 
             
                    @filename = filename
         | 
| 13 | 
            -
                  end
         | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
                  def validate(options = {})
         | 
| 18 12 | 
             
                    @errors = []
         | 
| 19 | 
            -
             | 
| 20 | 
            -
                    check_with_csvlint(options)
         | 
| 21 | 
            -
                  end
         | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
                  private
         | 
| 26 | 
            -
             | 
| 27 | 
            -
                  def check_with_csvlint(options = {})
         | 
| 28 | 
            -
                    File.open(filename, "r:#{options[:encoding] || 'UTF-8'}") do |file|
         | 
| 29 | 
            -
                      validator = ::Csvlint::Validator.new(file, build_dialect(options), build_schema(options))
         | 
| 30 | 
            -
             | 
| 31 | 
            -
                      validator.errors.each { |error| add_to_errors error }
         | 
| 32 | 
            -
                      validator.warnings.each { |warning| add_to_errors warning }
         | 
| 33 | 
            -
                    end
         | 
| 34 | 
            -
                  end
         | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
                  def build_dialect(options = {})
         | 
| 39 | 
            -
                    return {} unless options[:dialect]
         | 
| 40 | 
            -
             | 
| 41 | 
            -
                    stringify_keys options[:dialect]
         | 
| 42 13 | 
             
                  end
         | 
| 43 14 |  | 
| 44 15 |  | 
| 45 16 |  | 
| 46 | 
            -
                  def  | 
| 47 | 
            -
                     | 
| 48 | 
            -
             | 
| 49 | 
            -
                    :: | 
| 50 | 
            -
                  end
         | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
                  def build_header_fields(options = {})
         | 
| 55 | 
            -
                    options[:headers].map do |header|
         | 
| 56 | 
            -
                      header_definition = stringify_keys(header)
         | 
| 57 | 
            -
             | 
| 58 | 
            -
                      ::Csvlint::Field.new(header_definition["name"], header_definition["constraints"])
         | 
| 59 | 
            -
                    end
         | 
| 60 | 
            -
                  end
         | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
                  def stringify_keys(hash)
         | 
| 65 | 
            -
                    JSON.parse JSON.dump(hash)
         | 
| 66 | 
            -
                  end
         | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
                  def add_to_errors(original_error)
         | 
| 71 | 
            -
                    return if original_error.type == :encoding
         | 
| 72 | 
            -
             | 
| 73 | 
            -
                    error = {
         | 
| 74 | 
            -
                      row: original_error.row,
         | 
| 75 | 
            -
                      column: original_error.column,
         | 
| 76 | 
            -
                      type: original_error.type.to_s,
         | 
| 77 | 
            -
                      content: original_error.content.to_s.truncate(80)
         | 
| 78 | 
            -
                    }
         | 
| 79 | 
            -
             | 
| 80 | 
            -
                    @errors << error.reject { |_, v| v.blank? }
         | 
| 17 | 
            +
                  def validate(options = {})
         | 
| 18 | 
            +
                    ::CSV.foreach(@filename, "r:#{options[:encoding] || 'UTF-8'}") { |_row| }
         | 
| 19 | 
            +
                  rescue ::CSV::MalformedCSVError, ArgumentError => error
         | 
| 20 | 
            +
                    @errors << CSV::Error.new(error)
         | 
| 81 21 | 
             
                  end
         | 
| 82 22 |  | 
| 83 23 | 
             
                end
         | 
    
        data/lib/valigator.rb
    CHANGED
    
    
    
        data/valigator-csv.gemspec
    CHANGED
    
    | @@ -13,13 +13,11 @@ Gem::Specification.new do |spec| | |
| 13 13 | 
             
              spec.description   = %q{Yet another CSV validator library. Just better.}
         | 
| 14 14 | 
             
              spec.homepage      = 'https://github.com/emartech/valigator-csv'
         | 
| 15 15 |  | 
| 16 | 
            -
              spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
         | 
| 17 | 
            -
              spec.bindir        = " | 
| 16 | 
            +
              spec.files         = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|bin)/}) }
         | 
| 17 | 
            +
              spec.bindir        = "bin"
         | 
| 18 18 | 
             
              spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
         | 
| 19 19 | 
             
              spec.require_paths = ["lib"]
         | 
| 20 20 |  | 
| 21 | 
            -
              spec.add_runtime_dependency "csvlint"
         | 
| 22 | 
            -
             | 
| 23 21 | 
             
              spec.add_development_dependency "bundler", "~> 1.10"
         | 
| 24 22 | 
             
              spec.add_development_dependency "rake", "~> 10.0"
         | 
| 25 23 | 
             
              spec.add_development_dependency "rspec"
         | 
    
        metadata
    CHANGED
    
    | @@ -1,30 +1,16 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: valigator-csv
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.0 | 
| 4 | 
            +
              version: 1.1.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Daniel Nagy
         | 
| 8 8 | 
             
            - Istvan Demeter
         | 
| 9 9 | 
             
            autorequire: 
         | 
| 10 | 
            -
            bindir:  | 
| 10 | 
            +
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2015- | 
| 12 | 
            +
            date: 2015-12-02 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            -
              name: csvlint
         | 
| 16 | 
            -
              requirement: !ruby/object:Gem::Requirement
         | 
| 17 | 
            -
                requirements:
         | 
| 18 | 
            -
                - - ">="
         | 
| 19 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 20 | 
            -
                    version: '0'
         | 
| 21 | 
            -
              type: :runtime
         | 
| 22 | 
            -
              prerelease: false
         | 
| 23 | 
            -
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 24 | 
            -
                requirements:
         | 
| 25 | 
            -
                - - ">="
         | 
| 26 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 27 | 
            -
                    version: '0'
         | 
| 28 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 29 15 | 
             
              name: bundler
         | 
| 30 16 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| @@ -82,10 +68,9 @@ files: | |
| 82 68 | 
             
            - Gemfile
         | 
| 83 69 | 
             
            - README.md
         | 
| 84 70 | 
             
            - Rakefile
         | 
| 85 | 
            -
            - bin/console
         | 
| 86 | 
            -
            - bin/setup
         | 
| 87 71 | 
             
            - lib/valigator.rb
         | 
| 88 72 | 
             
            - lib/valigator/csv.rb
         | 
| 73 | 
            +
            - lib/valigator/csv/error.rb
         | 
| 89 74 | 
             
            - lib/valigator/csv/validator.rb
         | 
| 90 75 | 
             
            - lib/valigator/csv/version.rb
         | 
| 91 76 | 
             
            - valigator-csv.gemspec
         | 
    
        data/bin/console
    DELETED
    
    | @@ -1,14 +0,0 @@ | |
| 1 | 
            -
            #!/usr/bin/env ruby
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            require "bundler/setup"
         | 
| 4 | 
            -
            require "valigator/csv"
         | 
| 5 | 
            -
             | 
| 6 | 
            -
            # You can add fixtures and/or initialization code here to make experimenting
         | 
| 7 | 
            -
            # with your gem easier. You can also use a different console, if you like.
         | 
| 8 | 
            -
             | 
| 9 | 
            -
            # (If you use this, don't forget to add pry to your Gemfile!)
         | 
| 10 | 
            -
            # require "pry"
         | 
| 11 | 
            -
            # Pry.start
         | 
| 12 | 
            -
             | 
| 13 | 
            -
            require "irb"
         | 
| 14 | 
            -
            IRB.start
         |