valigator-csv 0.1.0 → 1.0.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: fcc5d33aa6bee7eb628ec0b993f3c791744b7307
4
- data.tar.gz: a3231352ad677d439f12a60230dc2367828d827e
3
+ metadata.gz: c6fbf0f123746a2514a558c87d27825c0134835f
4
+ data.tar.gz: 7c5bba057ab2eaac454956b82104cadea6813e84
5
5
  SHA512:
6
- metadata.gz: 1a68779f9248f75d227994a95403a6372155ca683960dd7a59c71d6901e8bde516feb56368f141eb400af744f0a980168fb239da16103cf3f725dc08f630ed32
7
- data.tar.gz: c80a9660dd3c75066ad508111e991407dba985260484a1c859da4359fb957e85820a31913c121d5d183e1ebab578f58b82bb52aaaaca17c9b41a557db469144d
6
+ metadata.gz: 50277157bc3a7f63d94bba35ccf4fdadb3cc3f4c4a6ec401f6aeb43a9e2e373d405417ac71d53b8953b6f58376e847b4a0bf4149f6686f16977a697626256f32
7
+ data.tar.gz: ec89294d043924afaee7eb7d1407d5ed92d9f1e193c3fec3da331c77967158b1a63143b9ddae51535d56fe639a3875b4aa188b007bd262da8d71e437e7307873
data/.gitattributes ADDED
@@ -0,0 +1 @@
1
+ *.csv text eol=crlf
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .idea/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # CsvValidator
1
+ # Valigator::CSV
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/csv_validator`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/valigator/csv`. To experiment with that code, run `bin/console` for an interactive prompt.
4
4
 
5
5
  TODO: Delete this and the text above, and describe your gem
6
6
 
@@ -9,7 +9,7 @@ TODO: Delete this and the text above, and describe your gem
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'csv_validator'
12
+ gem 'valigator/csv'
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -18,7 +18,7 @@ And then execute:
18
18
 
19
19
  Or install it yourself as:
20
20
 
21
- $ gem install csv_validator
21
+ $ gem install valigator-csv
22
22
 
23
23
  ## Usage
24
24
 
@@ -32,5 +32,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
32
 
33
33
  ## Contributing
34
34
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/csv_validator.
35
+ Bug reports and pull requests are welcome on GitHub at the project's [issue tracker](https://github.com/emarsys/valigator-csv).
36
36
 
data/lib/valigator/csv.rb CHANGED
@@ -1,7 +1,6 @@
1
- require "valigator/csv/version"
2
-
3
1
  module Valigator
4
2
  module CSV
5
- # Your code goes here...
3
+ autoload :Version, 'valigator/csv/version'
4
+ autoload :Validator, 'valigator/csv/validator'
6
5
  end
7
6
  end
@@ -0,0 +1,72 @@
1
+ require 'csvlint'
2
+ require 'active_support/core_ext/string/filters'
3
+ require 'active_support/hash_with_indifferent_access'
4
+
5
+ module Valigator
6
+ module CSV
7
+ class Validator
8
+ attr_reader :filename, :errors
9
+
10
+
11
+
12
+ def initialize(filename)
13
+ @filename = filename
14
+ end
15
+
16
+
17
+
18
+ def validate(options = {})
19
+ @errors = []
20
+
21
+ check_with_csvlint(options)
22
+ end
23
+
24
+
25
+
26
+ private
27
+
28
+ def check_with_csvlint(options = {})
29
+ File.open(filename, "r:#{options[:encoding] || 'UTF-8'}") do |file|
30
+ validator = ::Csvlint::Validator.new(file, {}, build_schema(options))
31
+
32
+ validator.errors.each { |error| add_to_errors error }
33
+ validator.warnings.each { |warning| add_to_errors warning }
34
+ end
35
+ end
36
+
37
+
38
+
39
+ def build_schema(options = {})
40
+ return unless options[:headers]
41
+
42
+ ::Csvlint::Schema.new("", build_header_fields(options))
43
+ end
44
+
45
+
46
+
47
+ def build_header_fields(options = {})
48
+ options[:headers].map do |header|
49
+ header_definition = JSON.parse JSON.dump(header)
50
+
51
+ ::Csvlint::Field.new(header_definition["name"], header_definition["constraints"])
52
+ end
53
+ end
54
+
55
+
56
+
57
+ def add_to_errors(original_error)
58
+ return if original_error.type == :encoding
59
+
60
+ error = {
61
+ row: original_error.row,
62
+ column: original_error.column,
63
+ type: original_error.type.to_s,
64
+ content: original_error.content.to_s.truncate(80)
65
+ }
66
+
67
+ @errors << error.reject { |_, v| v.blank? }
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -1,5 +1,5 @@
1
1
  module Valigator
2
2
  module CSV
3
- VERSION = "0.1.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "csvlint"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.10"
22
24
  spec.add_development_dependency "rake", "~> 10.0"
23
25
  spec.add_development_dependency "rspec"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valigator-csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Nagy
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2015-11-12 00:00:00.000000000 Z
12
+ date: 2015-11-16 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'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: bundler
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -61,6 +75,7 @@ executables: []
61
75
  extensions: []
62
76
  extra_rdoc_files: []
63
77
  files:
78
+ - ".gitattributes"
64
79
  - ".gitignore"
65
80
  - ".rspec"
66
81
  - ".travis.yml"
@@ -71,6 +86,7 @@ files:
71
86
  - bin/setup
72
87
  - lib/valigator.rb
73
88
  - lib/valigator/csv.rb
89
+ - lib/valigator/csv/validator.rb
74
90
  - lib/valigator/csv/version.rb
75
91
  - valigator-csv.gemspec
76
92
  homepage: https://github.com/emartech/valigator-csv