smarter_csv 1.2.3 → 1.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +10 -4
- data/Gemfile +0 -1
- data/README.md +16 -5
- data/lib/smarter_csv/smarter_csv.rb +17 -10
- data/lib/smarter_csv/version.rb +1 -1
- data/spec/fixtures/ignore_comments.csv +11 -0
- data/spec/fixtures/ignore_comments2.csv +3 -0
- data/spec/fixtures/problematic.csv +8 -0
- data/spec/smarter_csv/ignore_comments_spec.rb +30 -0
- data/spec/smarter_csv/problematic.rb +34 -0
- metadata +18 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 90df8a442690ff9202ee2762a199e652ebca4dc6e5899d24ee0a72d192cf539f
|
4
|
+
data.tar.gz: 16d4826bc7840a112ef5d5ea3b7acf0997d5fbeb46f8f5289ffa31a7356aa71c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85cf20915d26794ba37e508379bc8460d65f054771803779df3b67cd907a61da9ea195d8b51da5a9942bb7f3a5866325114a7a107cbbfcc269f47f6bf6dbfff5
|
7
|
+
data.tar.gz: e2508804722db4bd76e63a9e459b180fb75f7215e3e6d53970e5a83c120d0dab3cdd639c562abfe91d5b9458c3a990aa3959e85000491aef8529442ade5a3f90
|
data/.travis.yml
CHANGED
@@ -6,10 +6,14 @@ before_install:
|
|
6
6
|
|
7
7
|
matrix:
|
8
8
|
include:
|
9
|
-
- rvm: 2.2.
|
10
|
-
- rvm: 2.3.
|
11
|
-
- rvm: 2.4.
|
12
|
-
- rvm:
|
9
|
+
- rvm: 2.2.10
|
10
|
+
- rvm: 2.3.8
|
11
|
+
- rvm: 2.4.6
|
12
|
+
- rvm: 2.5.7
|
13
|
+
- rvm: 2.6.3
|
14
|
+
- rvm: 2.7.2
|
15
|
+
- rvm: 3.0.0
|
16
|
+
- rvm: jruby-9.2.7.0
|
13
17
|
env:
|
14
18
|
- JRUBY_OPTS="--server -Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -J-Xms512m -J-Xmx1024m"
|
15
19
|
- rvm: ruby-head
|
@@ -17,3 +21,5 @@ matrix:
|
|
17
21
|
branches:
|
18
22
|
only:
|
19
23
|
- master
|
24
|
+
- 1.2-stable
|
25
|
+
- 2.0-develop
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -324,11 +324,24 @@ Planned in the next releases:
|
|
324
324
|
|
325
325
|
## Changes
|
326
326
|
|
327
|
+
#### 1.2.8 (2020-02-04)
|
328
|
+
* fix deprecation warnings on Ruby 2.7 (thank to Diego Salido)
|
329
|
+
|
330
|
+
#### 1.2.7 (2020-02-03)
|
331
|
+
|
332
|
+
#### 1.2.6 (2018-11-13)
|
333
|
+
* fixing error caused by calling f.close when we do not hand in a file
|
334
|
+
|
335
|
+
#### 1.2.5 (2018-09-16)
|
336
|
+
* fixing issue #136 with comments in CSV files
|
337
|
+
* fixing error class hierarchy
|
338
|
+
|
339
|
+
#### 1.2.4 (2018-08-06)
|
340
|
+
* using Rails blank? if it's available
|
341
|
+
|
327
342
|
#### 1.2.3 (2018-01-27)
|
328
343
|
* fixed regression / test
|
329
344
|
* fuxed quote_char interpolation for headers, but not data (thanks to Colin Petruno)
|
330
|
-
|
331
|
-
#### 1.2.1 (2018-01-25) ### YANKED!
|
332
345
|
* bugfix (thanks to Joshua Smith for reporting)
|
333
346
|
|
334
347
|
#### 1.2.0 (2018-01-20)
|
@@ -385,9 +398,6 @@ Planned in the next releases:
|
|
385
398
|
#### 1.0.14 (2013-11-01)
|
386
399
|
* added GPL-2 and MIT license to GEM spec file; if you need another license contact me
|
387
400
|
|
388
|
-
#### 1.0.13 (2013-11-01) ### YANKED!
|
389
|
-
* added GPL-2 license to GEM spec file; if you need another license contact me
|
390
|
-
|
391
401
|
#### 1.0.12 (2013-10-15)
|
392
402
|
* added RSpec tests
|
393
403
|
|
@@ -503,6 +513,7 @@ And a special thanks to those who contributed pull requests:
|
|
503
513
|
* [Rohit Amarnath](https://github.com/ramarnat)
|
504
514
|
* [Joshua Smith](https://github.com/enviable)
|
505
515
|
* [Colin Petruno](https://github.com/colinpetruno)
|
516
|
+
* [Diego Salido](https://github.com/salidux)
|
506
517
|
|
507
518
|
|
508
519
|
## Contributing
|
@@ -1,16 +1,16 @@
|
|
1
1
|
module SmarterCSV
|
2
|
-
|
3
|
-
class HeaderSizeMismatch <
|
4
|
-
class IncorrectOption <
|
5
|
-
class DuplicateHeaders <
|
6
|
-
class MissingHeaders <
|
2
|
+
class SmarterCSVException < StandardError; end
|
3
|
+
class HeaderSizeMismatch < SmarterCSVException; end
|
4
|
+
class IncorrectOption < SmarterCSVException; end
|
5
|
+
class DuplicateHeaders < SmarterCSVException; end
|
6
|
+
class MissingHeaders < SmarterCSVException; end
|
7
7
|
|
8
8
|
|
9
9
|
def SmarterCSV.process(input, options={}, &block) # first parameter: filename or input object with readline method
|
10
10
|
default_options = {:col_sep => ',' , :row_sep => $/ , :quote_char => '"', :force_simple_split => false , :verbose => false ,
|
11
11
|
:remove_empty_values => true, :remove_zero_values => false , :remove_values_matching => nil , :remove_empty_hashes => true , :strip_whitespace => true,
|
12
12
|
:convert_values_to_numeric => true, :strip_chars_from_headers => nil , :user_provided_headers => nil , :headers_in_file => true,
|
13
|
-
:comment_regexp =>
|
13
|
+
:comment_regexp => /\A#/, :chunk_size => nil , :key_mapping_hash => nil , :downcase_header => true, :strings_as_keys => false, :file_encoding => 'utf-8',
|
14
14
|
:remove_unmapped_keys => false, :keep_original_headers => false, :value_converters => nil, :skip_lines => nil, :force_utf8 => false, :invalid_byte_sequence => '',
|
15
15
|
:auto_row_sep_chars => 500, :required_headers => nil
|
16
16
|
}
|
@@ -22,6 +22,7 @@ module SmarterCSV
|
|
22
22
|
old_row_sep = $/
|
23
23
|
file_line_count = 0
|
24
24
|
csv_line_count = 0
|
25
|
+
has_rails = !! defined?(Rails)
|
25
26
|
begin
|
26
27
|
f = input.respond_to?(:readline) ? input : File.open(input, "r:#{options[:file_encoding]}")
|
27
28
|
|
@@ -52,7 +53,7 @@ module SmarterCSV
|
|
52
53
|
|
53
54
|
if (header =~ %r{#{options[:quote_char]}}) and (! options[:force_simple_split])
|
54
55
|
file_headerA = begin
|
55
|
-
CSV.parse( header, csv_options ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
|
56
|
+
CSV.parse( header, **csv_options ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
|
56
57
|
rescue CSV::MalformedCSVError => e
|
57
58
|
raise $!, "#{$!} [SmarterCSV: csv line #{csv_line_count}]", $!.backtrace
|
58
59
|
end
|
@@ -148,7 +149,7 @@ module SmarterCSV
|
|
148
149
|
|
149
150
|
if (line =~ %r{#{options[:quote_char]}}) and (! options[:force_simple_split])
|
150
151
|
dataA = begin
|
151
|
-
CSV.parse( line, csv_options ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
|
152
|
+
CSV.parse( line, **csv_options ).flatten.collect!{|x| x.nil? ? '' : x} # to deal with nil values from CSV.parse
|
152
153
|
rescue CSV::MalformedCSVError => e
|
153
154
|
raise $!, "#{$!} [SmarterCSV: csv line #{csv_line_count}]", $!.backtrace
|
154
155
|
end
|
@@ -167,7 +168,13 @@ module SmarterCSV
|
|
167
168
|
|
168
169
|
# remove empty values using the same regexp as used by the rails blank? method
|
169
170
|
# which caters for double \n and \r\n characters such as "1\r\n\r\n2" whereas the original check (v =~ /^\s*$/) does not
|
170
|
-
|
171
|
+
if options[:remove_empty_values]
|
172
|
+
if has_rails
|
173
|
+
hash.delete_if{|k,v| v.blank?}
|
174
|
+
else
|
175
|
+
hash.delete_if{|k,v| v.nil? || v !~ /[^[:space:]]/}
|
176
|
+
end
|
177
|
+
end
|
171
178
|
|
172
179
|
hash.delete_if{|k,v| ! v.nil? && v =~ /^(\d+|\d+\.\d+)$/ && v.to_f == 0} if options[:remove_zero_values] # values are typically Strings!
|
173
180
|
hash.delete_if{|k,v| v =~ options[:remove_values_matching]} if options[:remove_values_matching]
|
@@ -241,7 +248,7 @@ module SmarterCSV
|
|
241
248
|
end
|
242
249
|
ensure
|
243
250
|
$/ = old_row_sep # make sure this stupid global variable is always reset to it's previous value after we're done!
|
244
|
-
f.close
|
251
|
+
f.close if f.respond_to?(:close)
|
245
252
|
end
|
246
253
|
if block_given?
|
247
254
|
return chunk_count # when we do processing through a block we only care how many chunks we processed
|
data/lib/smarter_csv/version.rb
CHANGED
@@ -0,0 +1,8 @@
|
|
1
|
+
Compte;Date de comptabilisation;Date op�ration;Libell�;R�f�rence;Date valeur;Montant
|
2
|
+
22215449203;02/06/2018;01/06/2018;ECHEANCE PRET DONT CAP 410,33 ASS. 8,00E INT. 21,87 COM. 0,00E;8711552;01/06/2018;-440,20;
|
3
|
+
22215449203;04/06/2018;04/06/2018;EVI Gaultier Laperche remboursement compte courant;1038326;04/06/2018;-144,07;
|
4
|
+
22215449203;04/06/2018;04/06/2018;EVI Guillemain Nicolas remboursement CC pret d'honneur;1038328;04/06/2018;-144,07;
|
5
|
+
22215449203;01/06/2018;01/06/2018;310518 SC****5448 INTERMARCHE 95ERMONT;701JQ1K;01/06/2018;-16,00;
|
6
|
+
22215449203;01/06/2018;01/06/2018;EVI Stripe Payments UK L STRIPE E7U0R1;706AO1Q;01/06/2018;45,89;
|
7
|
+
22215449203;01/06/2018;01/06/2018;EVI Compte N26 Heroku;1100653;01/06/2018;-700,00;
|
8
|
+
22215449203;31/05/2018;31/05/2018;EVI Stripe Payments UK L STRIPE L2J1N7;6YISBWF;31/05/2018;465,89;
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
fixture_path = 'spec/fixtures'
|
4
|
+
|
5
|
+
describe 'be_able_to' do
|
6
|
+
it 'ignore comments in CSV files' do
|
7
|
+
options = {}
|
8
|
+
data = SmarterCSV.process("#{fixture_path}/ignore_comments.csv", options)
|
9
|
+
|
10
|
+
data.size.should eq 5
|
11
|
+
|
12
|
+
# all the keys should be symbols
|
13
|
+
data.each{|item| item.keys.each{|x| x.is_a?(Symbol).should be_truthy}}
|
14
|
+
data.each do |h|
|
15
|
+
h.keys.each do |key|
|
16
|
+
[:"not_a_comment#first_name", :last_name, :dogs, :cats, :birds, :fish].should include( key )
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'ignore comments in CSV files with CRLF' do
|
22
|
+
options = {row_sep: "\r\n"}
|
23
|
+
data = SmarterCSV.process("#{fixture_path}/ignore_comments2.csv", options)
|
24
|
+
|
25
|
+
# all the keys should be symbols
|
26
|
+
data.size.should eq 1
|
27
|
+
data.first[:h1].should eq 'a'
|
28
|
+
data.first[:h2].should eq "b\r\n#c"
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
fixture_path = 'spec/fixtures'
|
4
|
+
|
5
|
+
describe 'loading file with UTF-8 characters in the header' do
|
6
|
+
|
7
|
+
# file which caused issues because of UTF-8 characters in the header
|
8
|
+
it 'loads the file with force_utf8 flag set' do
|
9
|
+
options = {col_sep: ";", force_utf8: true}
|
10
|
+
data = SmarterCSV.process("#{fixture_path}/problematic.csv", options)
|
11
|
+
|
12
|
+
data.length.should eq 7
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'loads the file with strings as keys' do
|
16
|
+
options = {
|
17
|
+
file_encoding: 'iso-8859-1:UTF-8', # important!
|
18
|
+
col_sep: ";", strings_as_keys: true,
|
19
|
+
}
|
20
|
+
data = SmarterCSV.process("#{fixture_path}/problematic.csv", options)
|
21
|
+
|
22
|
+
data.length.should eq 7
|
23
|
+
data.first.keys.sort.should eq [
|
24
|
+
"compte",
|
25
|
+
"date_de_comptabilisation",
|
26
|
+
"date_opération",
|
27
|
+
"date_valeur",
|
28
|
+
"libellé",
|
29
|
+
"montant",
|
30
|
+
"référence"
|
31
|
+
]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smarter_csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 'Tilo Sloboda
|
8
8
|
|
9
|
-
'
|
10
|
-
autorequire:
|
9
|
+
'
|
10
|
+
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-02-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -32,7 +32,7 @@ description: Ruby Gem for smarter importing of CSV Files as Array(s) of Hashes,
|
|
32
32
|
email:
|
33
33
|
- 'tilo.sloboda@gmail.com
|
34
34
|
|
35
|
-
'
|
35
|
+
'
|
36
36
|
executables: []
|
37
37
|
extensions: []
|
38
38
|
extra_rdoc_files: []
|
@@ -58,6 +58,8 @@ files:
|
|
58
58
|
- spec/fixtures/chunk_cornercase.csv
|
59
59
|
- spec/fixtures/duplicate_headers.csv
|
60
60
|
- spec/fixtures/empty.csv
|
61
|
+
- spec/fixtures/ignore_comments.csv
|
62
|
+
- spec/fixtures/ignore_comments2.csv
|
61
63
|
- spec/fixtures/line_endings_n.csv
|
62
64
|
- spec/fixtures/line_endings_r.csv
|
63
65
|
- spec/fixtures/line_endings_rn.csv
|
@@ -68,6 +70,7 @@ files:
|
|
68
70
|
- spec/fixtures/no_header.csv
|
69
71
|
- spec/fixtures/numeric.csv
|
70
72
|
- spec/fixtures/pets.csv
|
73
|
+
- spec/fixtures/problematic.csv
|
71
74
|
- spec/fixtures/quote_char.csv
|
72
75
|
- spec/fixtures/quoted.csv
|
73
76
|
- spec/fixtures/quoted2.csv
|
@@ -87,6 +90,7 @@ files:
|
|
87
90
|
- spec/smarter_csv/convert_values_to_numeric_spec.rb
|
88
91
|
- spec/smarter_csv/extenstions_spec.rb
|
89
92
|
- spec/smarter_csv/header_transformation_spec.rb
|
93
|
+
- spec/smarter_csv/ignore_comments_spec.rb
|
90
94
|
- spec/smarter_csv/invalid_headers_spec.rb
|
91
95
|
- spec/smarter_csv/keep_headers_spec.rb
|
92
96
|
- spec/smarter_csv/key_mapping_spec.rb
|
@@ -95,6 +99,7 @@ files:
|
|
95
99
|
- spec/smarter_csv/malformed_spec.rb
|
96
100
|
- spec/smarter_csv/no_header_spec.rb
|
97
101
|
- spec/smarter_csv/not_downcase_header_spec.rb
|
102
|
+
- spec/smarter_csv/problematic.rb
|
98
103
|
- spec/smarter_csv/quoted_spec.rb
|
99
104
|
- spec/smarter_csv/remove_empty_values_spec.rb
|
100
105
|
- spec/smarter_csv/remove_keys_from_hashes_spec.rb
|
@@ -115,7 +120,7 @@ licenses:
|
|
115
120
|
- MIT
|
116
121
|
- GPL-2
|
117
122
|
metadata: {}
|
118
|
-
post_install_message:
|
123
|
+
post_install_message:
|
119
124
|
rdoc_options: []
|
120
125
|
require_paths:
|
121
126
|
- lib
|
@@ -131,9 +136,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
136
|
version: '0'
|
132
137
|
requirements:
|
133
138
|
- csv
|
134
|
-
|
135
|
-
|
136
|
-
signing_key:
|
139
|
+
rubygems_version: 3.0.6
|
140
|
+
signing_key:
|
137
141
|
specification_version: 4
|
138
142
|
summary: Ruby Gem for smarter importing of CSV Files (and CSV-like files), with lots
|
139
143
|
of optional features, e.g. chunked processing for huge CSV files
|
@@ -147,6 +151,8 @@ test_files:
|
|
147
151
|
- spec/fixtures/chunk_cornercase.csv
|
148
152
|
- spec/fixtures/duplicate_headers.csv
|
149
153
|
- spec/fixtures/empty.csv
|
154
|
+
- spec/fixtures/ignore_comments.csv
|
155
|
+
- spec/fixtures/ignore_comments2.csv
|
150
156
|
- spec/fixtures/line_endings_n.csv
|
151
157
|
- spec/fixtures/line_endings_r.csv
|
152
158
|
- spec/fixtures/line_endings_rn.csv
|
@@ -157,6 +163,7 @@ test_files:
|
|
157
163
|
- spec/fixtures/no_header.csv
|
158
164
|
- spec/fixtures/numeric.csv
|
159
165
|
- spec/fixtures/pets.csv
|
166
|
+
- spec/fixtures/problematic.csv
|
160
167
|
- spec/fixtures/quote_char.csv
|
161
168
|
- spec/fixtures/quoted.csv
|
162
169
|
- spec/fixtures/quoted2.csv
|
@@ -176,6 +183,7 @@ test_files:
|
|
176
183
|
- spec/smarter_csv/convert_values_to_numeric_spec.rb
|
177
184
|
- spec/smarter_csv/extenstions_spec.rb
|
178
185
|
- spec/smarter_csv/header_transformation_spec.rb
|
186
|
+
- spec/smarter_csv/ignore_comments_spec.rb
|
179
187
|
- spec/smarter_csv/invalid_headers_spec.rb
|
180
188
|
- spec/smarter_csv/keep_headers_spec.rb
|
181
189
|
- spec/smarter_csv/key_mapping_spec.rb
|
@@ -184,6 +192,7 @@ test_files:
|
|
184
192
|
- spec/smarter_csv/malformed_spec.rb
|
185
193
|
- spec/smarter_csv/no_header_spec.rb
|
186
194
|
- spec/smarter_csv/not_downcase_header_spec.rb
|
195
|
+
- spec/smarter_csv/problematic.rb
|
187
196
|
- spec/smarter_csv/quoted_spec.rb
|
188
197
|
- spec/smarter_csv/remove_empty_values_spec.rb
|
189
198
|
- spec/smarter_csv/remove_keys_from_hashes_spec.rb
|