iron-import 0.8.3 → 0.8.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/History.txt +5 -0
- data/README.rdoc +15 -14
- data/Version.txt +1 -1
- data/lib/iron/import/csv_reader.rb +21 -8
- data/lib/iron/import/data_reader.rb +2 -2
- data/spec/importer/csv_reader_spec.rb +18 -0
- data/spec/importer/importer_spec.rb +12 -0
- data/spec/samples/2-sheets.xls +0 -0
- data/spec/samples/2-sheets.xlsx +0 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f53e828e0c419e7ad91abd7d4bc4879154941828
|
4
|
+
data.tar.gz: 239576c3acf292c759363da0e007acf82e4cbfc3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 858285de3786fb5e37e313fb48fc52e05fe39f81ef4ed972060bf87f2c906210e62a9c6e6a7200afb348250403162fcd89da66b7526def7b52aa5b0247a87c04
|
7
|
+
data.tar.gz: a9b29b5cdb47b3fc4885484b75b31e070667abf30955f77fda115d2878bbb2fa269f6402dd669aabbebec925edac4bc543a38ebf260f26dc5749e6669dce05f3
|
data/History.txt
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 0.8.4 / 2018-01-24
|
2
|
+
|
3
|
+
* Improve CSV reader to canonicalize newlines, converting \r + \r\n to \n before import, fixes Windows lameness
|
4
|
+
* Fix nokogiri and roo gem detection to properly handle installed versions much greater than required
|
5
|
+
|
1
6
|
== 0.8.3 / 2017-08-22
|
2
7
|
|
3
8
|
* Add :bool column type (supports 'yes'/'no', 'Y'/'N', 0/1, 'true'/'false', 'T'/'F')
|
data/README.rdoc
CHANGED
@@ -41,7 +41,7 @@ any errors encountered... well, this is the library for you!
|
|
41
41
|
- Import custom tabular data via passed block
|
42
42
|
- Automatic column order and start row detection
|
43
43
|
- Support for optional columns and dynamic column sets
|
44
|
-
- Basic data coercion supporting string, int, float, date and cents types
|
44
|
+
- Basic data coercion supporting string, int, float, date, bool and cents types
|
45
45
|
- Custom data coercion via passed block
|
46
46
|
- Custom data validation via passed block
|
47
47
|
- Row filtering using custom block
|
@@ -65,11 +65,12 @@ any errors encountered... well, this is the library for you!
|
|
65
65
|
end
|
66
66
|
# And custom validation
|
67
67
|
validate do |parsed_val|
|
68
|
-
add_error('
|
68
|
+
add_error('Description too short') unless parsed_val.length > 5
|
69
69
|
end
|
70
70
|
end
|
71
71
|
column :price do
|
72
|
-
# Built in type conversion handles common cases
|
72
|
+
# Built in type conversion handles common cases - in this case
|
73
|
+
# will correctly turn 2.5, "$2.50" or "2.5" into 250
|
73
74
|
type :cents
|
74
75
|
end
|
75
76
|
|
@@ -99,7 +100,7 @@ any errors encountered... well, this is the library for you!
|
|
99
100
|
end
|
100
101
|
|
101
102
|
else
|
102
|
-
# General errors, dump report
|
103
|
+
# General errors, dump summary report
|
103
104
|
puts "Error(s) on import: " + error_summary
|
104
105
|
end
|
105
106
|
end
|
@@ -118,17 +119,17 @@ any errors encountered... well, this is the library for you!
|
|
118
119
|
|
119
120
|
It can be tricky to keep track of what happens in Importer#import, so here's a quick cheat-sheet:
|
120
121
|
|
121
|
-
- Determine the
|
122
|
-
- Determine
|
123
|
-
-
|
124
|
-
- Validate presence of
|
125
|
-
-
|
122
|
+
- Determine the *format* of stream/file to import
|
123
|
+
- Determine *import scope* (sheet/table/whatever) using Importer#scope settings, if any
|
124
|
+
- *Find column headers + start row*
|
125
|
+
- Validate presence of *required columns*
|
126
|
+
- *Validate column set* using Importer#validate_columns
|
126
127
|
- Run each row:
|
127
|
-
-
|
128
|
-
-
|
129
|
-
-
|
130
|
-
-
|
131
|
-
-
|
128
|
+
- *Parse* each column's value using Column#parse or Column#type
|
129
|
+
- *Filter the row* using Importer#filter_rows on parsed values to reject unwanted rows
|
130
|
+
- *Calculate virtual columns* using Column#calculate
|
131
|
+
- *Validate each parsed value* using Column#validate
|
132
|
+
- *Validate entire row* using Importer#validate_rows
|
132
133
|
|
133
134
|
Generally, the import will stop when an error occurs, save on row processing, where each row will
|
134
135
|
be run until an error for that row is found. The goal is to accumulate actionable info for
|
data/Version.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.4
|
@@ -14,19 +14,32 @@ class Importer
|
|
14
14
|
if mode == :stream
|
15
15
|
# For streams, we just read 'em in and parse 'em
|
16
16
|
text = source.read
|
17
|
-
encoding = @importer.encoding || 'UTF-8'
|
18
|
-
@raw_rows = CSV.parse(text, :encoding => "#{encoding}:UTF-8")
|
19
|
-
true
|
20
17
|
|
21
18
|
elsif mode == :file
|
22
19
|
# Files have a different path
|
23
|
-
|
24
|
-
@raw_rows = CSV.read(source, :encoding => "#{encoding}:UTF-8")
|
25
|
-
true
|
20
|
+
text = File.read(source)
|
26
21
|
|
27
22
|
else
|
28
|
-
|
29
|
-
|
23
|
+
# WTF?
|
24
|
+
@importer.add_error("Unsupported CSV mode: #{mode.inspect}")
|
25
|
+
return false
|
26
|
+
end
|
27
|
+
|
28
|
+
# Fix shitty Windows line-feeds so things are standardized
|
29
|
+
text.gsub!(/\r\n/, "\n")
|
30
|
+
text.gsub!(/\r/, "\n")
|
31
|
+
|
32
|
+
# Parse it out
|
33
|
+
encoding = @importer.encoding || 'UTF-8'
|
34
|
+
options = {
|
35
|
+
:encoding => "#{encoding}:UTF-8",
|
36
|
+
:skip_blanks => true
|
37
|
+
}
|
38
|
+
begin
|
39
|
+
@raw_rows = CSV.parse(text, options)
|
40
|
+
rescue Exception => e
|
41
|
+
@importer.add_error('Error encountered while parsing CSV')
|
42
|
+
@importer.add_exception(e)
|
30
43
|
end
|
31
44
|
end
|
32
45
|
|
@@ -9,7 +9,7 @@ class Importer
|
|
9
9
|
attr_reader :format
|
10
10
|
|
11
11
|
def self.verify_roo!
|
12
|
-
if Gem::Specification.find_all_by_name('roo', '
|
12
|
+
if Gem::Specification.find_all_by_name('roo', '>= 1.13.0').empty?
|
13
13
|
raise "You are attempting to use the iron-import gem to import an Excel file. Doing so requires installing the roo gem, version 1.13.0 or later."
|
14
14
|
else
|
15
15
|
require 'roo'
|
@@ -17,7 +17,7 @@ class Importer
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.verify_nokogiri!
|
20
|
-
if Gem::Specification.find_all_by_name('nokogiri', '
|
20
|
+
if Gem::Specification.find_all_by_name('nokogiri', '>= 1.6.0').empty?
|
21
21
|
raise "You are attempting to use the iron-import gem to import an HTML file. Doing so requires installing the nokogiri gem, version 1.6.0 or later."
|
22
22
|
else
|
23
23
|
require 'nokogiri'
|
@@ -5,6 +5,24 @@ describe Importer::CsvReader do
|
|
5
5
|
@reader = Importer::CsvReader.new(@importer)
|
6
6
|
end
|
7
7
|
|
8
|
+
it 'should convert to standard newlines' do
|
9
|
+
importer = Importer.build do
|
10
|
+
headerless!
|
11
|
+
column :number do
|
12
|
+
type :integer
|
13
|
+
end
|
14
|
+
column :string do
|
15
|
+
type :string
|
16
|
+
end
|
17
|
+
end
|
18
|
+
importer.import_string("1,\"foo\nbar\"\r\n2,hi\r3,yo").should be_true
|
19
|
+
importer.to_a.should == [
|
20
|
+
{:number => 1, :string => "foo\nbar"},
|
21
|
+
{:number => 2, :string => "hi"},
|
22
|
+
{:number => 3, :string => "yo"}
|
23
|
+
]
|
24
|
+
end
|
25
|
+
|
8
26
|
it 'should load our simple CSV data' do
|
9
27
|
importer = Importer.build do
|
10
28
|
column :number do
|
@@ -286,4 +286,16 @@ describe Importer do
|
|
286
286
|
importer.found_columns.count.should == 2
|
287
287
|
end
|
288
288
|
|
289
|
+
it 'should search multiple sheets to find header' do
|
290
|
+
importer = Importer.build do
|
291
|
+
column :date do
|
292
|
+
type :date
|
293
|
+
end
|
294
|
+
column :order
|
295
|
+
end
|
296
|
+
importer.import(SpecHelper.sample_path('2-sheets.xlsx')).should be_true
|
297
|
+
importer.errors.count.should == 0
|
298
|
+
importer.to_a.should == [{:order => '223300', :date => '1973-01-02'.to_date}]
|
299
|
+
end
|
300
|
+
|
289
301
|
end
|
Binary file
|
Binary file
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iron-import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob Morris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: iron-extensions
|
@@ -123,6 +123,8 @@ files:
|
|
123
123
|
- spec/importer/row_spec.rb
|
124
124
|
- spec/importer/xls_reader_spec.rb
|
125
125
|
- spec/importer/xlsx_reader_spec.rb
|
126
|
+
- spec/samples/2-sheets.xls
|
127
|
+
- spec/samples/2-sheets.xlsx
|
126
128
|
- spec/samples/3-sheets.xls
|
127
129
|
- spec/samples/col-span.html
|
128
130
|
- spec/samples/html-th-td.html
|