valigator-csv 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1efa995498cf14a8bf892006e140abb02e3aee2e
4
- data.tar.gz: 5b26ffad290e8334a508381d8709c15d07c31f19
3
+ metadata.gz: 325bd8b898cc324095f85b90956285b9fd49eb1c
4
+ data.tar.gz: dc833a514d767425f97077301ccb0addb3841335
5
5
  SHA512:
6
- metadata.gz: ec34eacd57451041c1e2121387becb196fd3e0f3201008ecfaec658801de839c1f0678a770d27a3aa3984a88485a0b4d7831caa1f4200894c6a39c2493c805b4
7
- data.tar.gz: 83a1ce0eafcad4816753b4c06929e949e655fd18b3a0f999d4d27f0b5c57a7c097976e28f13f6616a81fbb2493ee583380e02844e012d9c054318e508f50e6f8
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 'csvlint'
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 build_schema(options = {})
47
- return unless options[:headers]
48
-
49
- ::Csvlint::Schema.new("", build_header_fields(options))
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
@@ -1,5 +1,5 @@
1
1
  module Valigator
2
2
  module CSV
3
- VERSION = "1.0.1"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
data/lib/valigator.rb CHANGED
@@ -1 +1,2 @@
1
1
  require 'valigator/csv'
2
+ require 'valigator/csv/error'
@@ -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 = "exe"
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.1
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: exe
10
+ bindir: bin
11
11
  cert_chain: []
12
- date: 2015-11-17 00:00:00.000000000 Z
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
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here