csv_content_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 57fdd22b0982fc3f98f2dc04cf1b71bf28c3328c
4
+ data.tar.gz: f7b6435615313585e1abdb366e1f20e944c766a9
5
+ SHA512:
6
+ metadata.gz: 2ae4b7f9fbc88d87f5120189e621a732c6a6edd40c07951a8cea3ea870335435034e854b0b6a44a5cbd544be4bbe3d037da3c83209924c5aac2056d0c1e528c4
7
+ data.tar.gz: 2ce7d91344b19c02ef3fc981d85d9794654a9e1f545af64a6c46c4ca9a9fd4b23254a3480fd70619990f0088854eacaf12275d18d04076ec05324acd915c2d8e
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in csv_content_validator.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Brian Lam
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,46 @@
1
+ # CsvContentValidator
2
+
3
+ Ruby gem which will allow authors to validate the content of well-formed CSV files.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'csv_content_validator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install csv_content_validator
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'json'
23
+ require 'validators/recipients_validator.rb'
24
+
25
+ class RecipientsController < ApplicationController
26
+ def create
27
+ render validator.render_errors(params[:csv_file].tempfile)
28
+ end
29
+
30
+ def update
31
+ render validator.render_errors(params[:csv_file].tempfile, JSON.parse(params[:corrections]))
32
+ end
33
+
34
+ private
35
+ def validator
36
+ @validator ||= RecipientsValidator.new
37
+ end
38
+ end
39
+ ```
40
+ ## Contributing
41
+
42
+ 1. Fork it ( https://github.com/[my-github-username]/csv_content_validator/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csv_content_validator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "csv_content_validator"
8
+ spec.version = CsvContentValidator::VERSION
9
+ spec.authors = ["Brian Lam"]
10
+ spec.email = ["brianlam93@gmail.com"]
11
+ spec.summary = 'Validate the content of CSV files'
12
+ spec.description = 'Validate the content of well-formed CSV files on server and client side'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,117 @@
1
+ require 'csv'
2
+ require 'json'
3
+
4
+ # Example VALIDATIONS object
5
+ # VALIDATIONS =
6
+ # {
7
+ # validation_tests: {
8
+ # "column_name": {
9
+ # functions: [:validation_method_1] }
10
+ # options: [{label: 'English', value: 'en'}, {label: 'German', 'gem'}]
11
+ # }
12
+ # }
13
+
14
+ class Validator
15
+ VALIDATIONS =
16
+ {
17
+ validation_tests: { }
18
+ }
19
+
20
+ def validate_fatal_errors(csv_file)
21
+ fatal_errors = []
22
+ validations[:validation_tests].keys.each do |required_column|
23
+ unless csv_file.headers.include?(required_column.to_s) then
24
+ fatal_errors.push("Uploaded CSV missing required column '#{required_column}'")
25
+ end
26
+ end
27
+ return fatal_errors
28
+ end
29
+
30
+ def render_errors (csv_file, corrections=[])
31
+ begin
32
+ rows = validate_file(csv_file, corrections)
33
+ if rows[:errors] then
34
+ return {json: {errors:rows[:errors]}, :status => 420}
35
+ else
36
+ return {json: {validator: to_json, rows: rows[:rows]}}
37
+ end
38
+ rescue
39
+ puts "rescued!"
40
+ return {json: {errors: ["CSV file was malformed"]}, :status => 420 }
41
+ end
42
+ end
43
+
44
+ def validate_file! (csv_file, corrections)
45
+ csv_rows = CSV.read(csv_file, headers:true)
46
+
47
+ # check for fatal errors
48
+ fatal_errors = validate_fatal_errors(csv_rows)
49
+ return {errors:fatal_errors} unless fatal_errors.empty?
50
+ rows = []
51
+
52
+ # no fatal errors - validate cells accordingly
53
+ curr_correction_index = 0
54
+ CSV.foreach(csv_file, headers:true) do |row|
55
+ o = {}
56
+ if corrections[curr_correction_index] && $. === corrections[curr_correction_index]["number"] then
57
+ o = corrections[curr_correction_index]["row"]
58
+ curr_correction_index += 1
59
+ else
60
+ o = row.to_h
61
+ end
62
+ is_valid = true
63
+ o.keys.each do |column|
64
+ is_valid = is_valid && is_valid?(column, o[column])
65
+ end
66
+ unless is_valid then
67
+ rows << {number:$., row:o}
68
+ end
69
+ end
70
+
71
+ return {rows:rows}
72
+ end
73
+
74
+ def validate_file(csv_file, corrections)
75
+ return validate_file!(csv_file, corrections)
76
+ end
77
+
78
+ def is_valid? (column_name, value)
79
+ return true unless validations[:validation_tests].has_key?(column_name.to_sym)
80
+
81
+ validation_tests = validations[:validation_tests][column_name.to_sym]
82
+ # call all methods
83
+ if validation_tests[:functions] then
84
+ validation_tests[:functions].each do |function|
85
+ return false unless send(function, value)
86
+ end
87
+ elsif validation_tests[:options] then
88
+ return true if value === "" && !validation_tests[:required]
89
+ return validation_tests[:options].any?{|option| option[:value] === value}
90
+ end
91
+
92
+ return true
93
+ end
94
+
95
+ def inspect
96
+ validations.inspect
97
+ end
98
+
99
+ def to_json
100
+ validations.to_json;
101
+ end
102
+
103
+ # General Validation Methods
104
+ def always_true value
105
+ true
106
+ end
107
+
108
+ def always_false value
109
+ false
110
+ end
111
+
112
+ private
113
+
114
+ def validations
115
+ @validations ||= self.class::VALIDATIONS
116
+ end
117
+ end
@@ -0,0 +1,3 @@
1
+ module CsvContentValidator
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csv_content_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Lam
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Validate the content of well-formed CSV files on server and client side
42
+ email:
43
+ - brianlam93@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - csv_content_validator.gemspec
54
+ - lib/csv_content_validator.rb
55
+ - lib/csv_content_validator/version.rb
56
+ homepage: ''
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Validate the content of CSV files
80
+ test_files: []
81
+ has_rdoc: