iostreams 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +202 -0
- data/README.md +155 -47
- data/lib/io_streams/file/reader.rb +7 -8
- data/lib/io_streams/file/writer.rb +7 -8
- data/lib/io_streams/io_streams.rb +313 -129
- data/lib/io_streams/{delimited → line}/reader.rb +20 -30
- data/lib/io_streams/line/writer.rb +81 -0
- data/lib/io_streams/pgp.rb +4 -14
- data/lib/io_streams/record/reader.rb +55 -0
- data/lib/io_streams/record/writer.rb +63 -0
- data/lib/io_streams/row/reader.rb +60 -0
- data/lib/io_streams/row/writer.rb +62 -0
- data/lib/io_streams/s3.rb +25 -0
- data/lib/io_streams/s3/reader.rb +64 -0
- data/lib/io_streams/s3/writer.rb +13 -0
- data/lib/io_streams/streams.rb +1 -1
- data/lib/io_streams/tabular.rb +163 -0
- data/lib/io_streams/tabular/errors.rb +14 -0
- data/lib/io_streams/tabular/header.rb +146 -0
- data/lib/io_streams/tabular/parser/array.rb +26 -0
- data/lib/io_streams/tabular/parser/base.rb +12 -0
- data/lib/io_streams/tabular/parser/csv.rb +35 -0
- data/lib/io_streams/tabular/parser/fixed.rb +88 -0
- data/lib/io_streams/tabular/parser/hash.rb +21 -0
- data/lib/io_streams/tabular/parser/json.rb +25 -0
- data/lib/io_streams/tabular/parser/psv.rb +34 -0
- data/lib/io_streams/tabular/utility/csv_row.rb +115 -0
- data/lib/io_streams/version.rb +2 -2
- data/lib/io_streams/xlsx/reader.rb +1 -1
- data/lib/io_streams/zip/reader.rb +1 -1
- data/lib/io_streams/zip/writer.rb +1 -1
- data/lib/iostreams.rb +21 -10
- data/test/bzip2_reader_test.rb +21 -22
- data/test/bzip2_writer_test.rb +38 -32
- data/test/file_reader_test.rb +19 -18
- data/test/file_writer_test.rb +23 -22
- data/test/files/test.json +3 -0
- data/test/gzip_reader_test.rb +21 -22
- data/test/gzip_writer_test.rb +35 -29
- data/test/io_streams_test.rb +137 -61
- data/test/line_reader_test.rb +105 -0
- data/test/line_writer_test.rb +50 -0
- data/test/pgp_reader_test.rb +29 -29
- data/test/pgp_test.rb +149 -195
- data/test/pgp_writer_test.rb +63 -62
- data/test/record_reader_test.rb +61 -0
- data/test/record_writer_test.rb +73 -0
- data/test/row_reader_test.rb +34 -0
- data/test/row_writer_test.rb +51 -0
- data/test/tabular_test.rb +184 -0
- data/test/xlsx_reader_test.rb +13 -17
- data/test/zip_reader_test.rb +21 -22
- data/test/zip_writer_test.rb +40 -36
- metadata +41 -17
- data/lib/io_streams/csv/reader.rb +0 -21
- data/lib/io_streams/csv/writer.rb +0 -20
- data/lib/io_streams/delimited/writer.rb +0 -67
- data/test/csv_reader_test.rb +0 -34
- data/test/csv_writer_test.rb +0 -35
- data/test/delimited_reader_test.rb +0 -115
- data/test/delimited_writer_test.rb +0 -44
data/test/xlsx_reader_test.rb
CHANGED
@@ -1,45 +1,41 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
|
3
|
-
|
3
|
+
class XlsxReaderTest
|
4
4
|
describe IOStreams::Xlsx::Reader do
|
5
|
-
|
6
|
-
|
5
|
+
let :file_name do
|
6
|
+
File.join(File.dirname(__FILE__), 'files', 'spreadsheet.xlsx')
|
7
|
+
end
|
8
|
+
|
9
|
+
let :xlsx_contents do
|
10
|
+
[
|
7
11
|
['first column', 'second column', 'third column'],
|
8
12
|
['data 1', 'data 2', 'more data']
|
9
13
|
]
|
10
14
|
end
|
11
15
|
|
12
16
|
describe '.open' do
|
13
|
-
let(:file_name) { File.join(File.dirname(__FILE__), 'files', 'spreadsheet.xlsx') }
|
14
|
-
|
15
17
|
describe 'with a file path' do
|
16
|
-
before do
|
17
|
-
@file = File.open(file_name)
|
18
|
-
end
|
19
|
-
|
20
18
|
it 'returns the contents of the file' do
|
21
19
|
rows = []
|
22
|
-
IOStreams::Xlsx::Reader.open(
|
23
|
-
|
20
|
+
IOStreams::Xlsx::Reader.open(file_name) do |stream|
|
21
|
+
stream.each { |row| rows << row }
|
24
22
|
end
|
25
|
-
assert_equal
|
23
|
+
assert_equal xlsx_contents, rows
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
27
|
describe 'with a file stream' do
|
30
|
-
|
31
28
|
it 'returns the contents of the file' do
|
32
29
|
rows = []
|
33
30
|
File.open(file_name) do |file|
|
34
|
-
IOStreams::Xlsx::Reader.open(file) do |
|
35
|
-
|
31
|
+
IOStreams::Xlsx::Reader.open(file) do |stream|
|
32
|
+
stream.each { |row| rows << row }
|
36
33
|
end
|
37
34
|
end
|
38
35
|
|
39
|
-
assert_equal
|
36
|
+
assert_equal xlsx_contents, rows
|
40
37
|
end
|
41
38
|
end
|
42
39
|
end
|
43
|
-
|
44
40
|
end
|
45
41
|
end
|
data/test/zip_reader_test.rb
CHANGED
@@ -1,34 +1,33 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
require 'zip'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@file_name = File.join(File.dirname(__FILE__), 'files', 'text.zip')
|
10
|
-
@zip_data = File.open(@file_name, 'rb') { |f| f.read }
|
11
|
-
@data = Zip::File.open(@file_name) { |zip_file| zip_file.first.get_input_stream.read }
|
12
|
-
end
|
4
|
+
class ZipReaderTest < Minitest::Test
|
5
|
+
describe IOStreams::Zip::Reader do
|
6
|
+
let :file_name do
|
7
|
+
File.join(File.dirname(__FILE__), 'files', 'text.zip')
|
8
|
+
end
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
10
|
+
let :decompressed do
|
11
|
+
Zip::File.open(file_name) { |zip_file| zip_file.first.get_input_stream.read }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '.open' do
|
15
|
+
it 'file' do
|
16
|
+
result = IOStreams::Zip::Reader.open(file_name) do |io|
|
17
|
+
io.read
|
20
18
|
end
|
19
|
+
assert_equal decompressed, result
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
22
|
+
it 'stream' do
|
23
|
+
result = File.open(file_name) do |file|
|
24
|
+
IOStreams::Zip::Reader.open(file) do |io|
|
25
|
+
io.read
|
27
26
|
end
|
28
|
-
assert_equal @data, result
|
29
27
|
end
|
28
|
+
assert_equal decompressed, result
|
30
29
|
end
|
31
|
-
|
32
30
|
end
|
31
|
+
|
33
32
|
end
|
34
33
|
end
|
data/test/zip_writer_test.rb
CHANGED
@@ -1,47 +1,51 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
require 'zip'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
file_name = File.join(File.dirname(__FILE__), 'files', 'text.txt')
|
10
|
-
@data = File.read(file_name)
|
11
|
-
end
|
4
|
+
class ZipWriterTest < Minitest::Test
|
5
|
+
describe IOStreams::Zip::Writer do
|
6
|
+
let :temp_file do
|
7
|
+
Tempfile.new('iostreams')
|
8
|
+
end
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
result = Zip::File.open(file_name) do |zip_file|
|
21
|
-
zip_file.first.get_input_stream.read
|
22
|
-
end
|
23
|
-
temp_file.delete
|
24
|
-
assert_equal @data, result
|
25
|
-
end
|
10
|
+
let :file_name do
|
11
|
+
temp_file.path
|
12
|
+
end
|
13
|
+
|
14
|
+
let :decompressed do
|
15
|
+
File.read(File.join(File.dirname(__FILE__), 'files', 'text.txt'))
|
16
|
+
end
|
26
17
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
ensure
|
39
|
-
zin.close if zin
|
40
|
-
end
|
41
|
-
assert_equal @data, result
|
18
|
+
after do
|
19
|
+
temp_file.delete
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '.open' do
|
23
|
+
it 'file' do
|
24
|
+
IOStreams::Zip::Writer.open(file_name, zip_file_name: 'text.txt') do |io|
|
25
|
+
io.write(decompressed)
|
26
|
+
end
|
27
|
+
result = Zip::File.open(file_name) do |zip_file|
|
28
|
+
zip_file.first.get_input_stream.read
|
42
29
|
end
|
30
|
+
assert_equal decompressed, result
|
43
31
|
end
|
44
32
|
|
33
|
+
it 'stream' do
|
34
|
+
io_string = StringIO.new(''.b)
|
35
|
+
IOStreams::Zip::Writer.open(io_string) do |io|
|
36
|
+
io.write(decompressed)
|
37
|
+
end
|
38
|
+
io = StringIO.new(io_string.string)
|
39
|
+
result = nil
|
40
|
+
begin
|
41
|
+
zin = ::Zip::InputStream.new(io)
|
42
|
+
zin.get_next_entry
|
43
|
+
result = zin.read
|
44
|
+
ensure
|
45
|
+
zin.close if zin
|
46
|
+
end
|
47
|
+
assert_equal decompressed, result
|
48
|
+
end
|
45
49
|
end
|
46
50
|
end
|
47
51
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iostreams
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -31,25 +31,42 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
+
- LICENSE
|
34
35
|
- README.md
|
35
36
|
- Rakefile
|
36
37
|
- lib/io_streams/bzip2/reader.rb
|
37
38
|
- lib/io_streams/bzip2/writer.rb
|
38
|
-
- lib/io_streams/csv/reader.rb
|
39
|
-
- lib/io_streams/csv/writer.rb
|
40
|
-
- lib/io_streams/delimited/reader.rb
|
41
|
-
- lib/io_streams/delimited/writer.rb
|
42
39
|
- lib/io_streams/file/reader.rb
|
43
40
|
- lib/io_streams/file/writer.rb
|
44
41
|
- lib/io_streams/gzip/reader.rb
|
45
42
|
- lib/io_streams/gzip/writer.rb
|
46
43
|
- lib/io_streams/io_streams.rb
|
44
|
+
- lib/io_streams/line/reader.rb
|
45
|
+
- lib/io_streams/line/writer.rb
|
47
46
|
- lib/io_streams/pgp.rb
|
48
47
|
- lib/io_streams/pgp/reader.rb
|
49
48
|
- lib/io_streams/pgp/writer.rb
|
49
|
+
- lib/io_streams/record/reader.rb
|
50
|
+
- lib/io_streams/record/writer.rb
|
51
|
+
- lib/io_streams/row/reader.rb
|
52
|
+
- lib/io_streams/row/writer.rb
|
53
|
+
- lib/io_streams/s3.rb
|
54
|
+
- lib/io_streams/s3/reader.rb
|
55
|
+
- lib/io_streams/s3/writer.rb
|
50
56
|
- lib/io_streams/sftp/reader.rb
|
51
57
|
- lib/io_streams/sftp/writer.rb
|
52
58
|
- lib/io_streams/streams.rb
|
59
|
+
- lib/io_streams/tabular.rb
|
60
|
+
- lib/io_streams/tabular/errors.rb
|
61
|
+
- lib/io_streams/tabular/header.rb
|
62
|
+
- lib/io_streams/tabular/parser/array.rb
|
63
|
+
- lib/io_streams/tabular/parser/base.rb
|
64
|
+
- lib/io_streams/tabular/parser/csv.rb
|
65
|
+
- lib/io_streams/tabular/parser/fixed.rb
|
66
|
+
- lib/io_streams/tabular/parser/hash.rb
|
67
|
+
- lib/io_streams/tabular/parser/json.rb
|
68
|
+
- lib/io_streams/tabular/parser/psv.rb
|
69
|
+
- lib/io_streams/tabular/utility/csv_row.rb
|
53
70
|
- lib/io_streams/version.rb
|
54
71
|
- lib/io_streams/xlsx/reader.rb
|
55
72
|
- lib/io_streams/zip/reader.rb
|
@@ -57,14 +74,11 @@ files:
|
|
57
74
|
- lib/iostreams.rb
|
58
75
|
- test/bzip2_reader_test.rb
|
59
76
|
- test/bzip2_writer_test.rb
|
60
|
-
- test/csv_reader_test.rb
|
61
|
-
- test/csv_writer_test.rb
|
62
|
-
- test/delimited_reader_test.rb
|
63
|
-
- test/delimited_writer_test.rb
|
64
77
|
- test/file_reader_test.rb
|
65
78
|
- test/file_writer_test.rb
|
66
79
|
- test/files/spreadsheet.xlsx
|
67
80
|
- test/files/test.csv
|
81
|
+
- test/files/test.json
|
68
82
|
- test/files/text.txt
|
69
83
|
- test/files/text.txt.bz2
|
70
84
|
- test/files/text.txt.gz
|
@@ -73,9 +87,16 @@ files:
|
|
73
87
|
- test/gzip_reader_test.rb
|
74
88
|
- test/gzip_writer_test.rb
|
75
89
|
- test/io_streams_test.rb
|
90
|
+
- test/line_reader_test.rb
|
91
|
+
- test/line_writer_test.rb
|
76
92
|
- test/pgp_reader_test.rb
|
77
93
|
- test/pgp_test.rb
|
78
94
|
- test/pgp_writer_test.rb
|
95
|
+
- test/record_reader_test.rb
|
96
|
+
- test/record_writer_test.rb
|
97
|
+
- test/row_reader_test.rb
|
98
|
+
- test/row_writer_test.rb
|
99
|
+
- test/tabular_test.rb
|
79
100
|
- test/test_helper.rb
|
80
101
|
- test/xlsx_reader_test.rb
|
81
102
|
- test/zip_reader_test.rb
|
@@ -100,28 +121,29 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
121
|
version: '0'
|
101
122
|
requirements: []
|
102
123
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.7.
|
124
|
+
rubygems_version: 2.7.7
|
104
125
|
signing_key:
|
105
126
|
specification_version: 4
|
106
|
-
summary:
|
107
|
-
Encryption.
|
127
|
+
summary: Input and Output streaming for Ruby.
|
108
128
|
test_files:
|
109
129
|
- test/pgp_reader_test.rb
|
130
|
+
- test/line_reader_test.rb
|
110
131
|
- test/xlsx_reader_test.rb
|
111
|
-
- test/
|
132
|
+
- test/row_writer_test.rb
|
112
133
|
- test/zip_reader_test.rb
|
113
134
|
- test/bzip2_writer_test.rb
|
114
135
|
- test/gzip_writer_test.rb
|
115
|
-
- test/delimited_writer_test.rb
|
116
136
|
- test/file_reader_test.rb
|
137
|
+
- test/record_reader_test.rb
|
117
138
|
- test/pgp_writer_test.rb
|
139
|
+
- test/line_writer_test.rb
|
140
|
+
- test/row_reader_test.rb
|
118
141
|
- test/bzip2_reader_test.rb
|
119
|
-
- test/csv_reader_test.rb
|
120
142
|
- test/zip_writer_test.rb
|
121
|
-
- test/delimited_reader_test.rb
|
122
143
|
- test/files/text.zip
|
123
144
|
- test/files/spreadsheet.xlsx
|
124
145
|
- test/files/test.csv
|
146
|
+
- test/files/test.json
|
125
147
|
- test/files/text.txt.bz2
|
126
148
|
- test/files/text.txt.gz.zip
|
127
149
|
- test/files/text.txt.gz
|
@@ -129,5 +151,7 @@ test_files:
|
|
129
151
|
- test/gzip_reader_test.rb
|
130
152
|
- test/test_helper.rb
|
131
153
|
- test/file_writer_test.rb
|
154
|
+
- test/tabular_test.rb
|
132
155
|
- test/pgp_test.rb
|
133
156
|
- test/io_streams_test.rb
|
157
|
+
- test/record_writer_test.rb
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'csv'
|
2
|
-
module IOStreams
|
3
|
-
module CSV
|
4
|
-
class Reader
|
5
|
-
# Read from a file or stream
|
6
|
-
def self.open(file_name_or_io, options = Hash.new, &block)
|
7
|
-
unless IOStreams.reader_stream?(file_name_or_io)
|
8
|
-
::CSV.open(file_name_or_io, options, &block)
|
9
|
-
else
|
10
|
-
begin
|
11
|
-
csv = ::CSV.new(file_name_or_io, options)
|
12
|
-
block.call(csv)
|
13
|
-
ensure
|
14
|
-
csv.close if csv
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module IOStreams
|
2
|
-
module CSV
|
3
|
-
class Writer
|
4
|
-
# Write to a file / stream, compressing with GZip
|
5
|
-
def self.open(file_name_or_io, options = {}, &block)
|
6
|
-
unless IOStreams.writer_stream?(file_name_or_io)
|
7
|
-
::CSV.open(file_name_or_io, 'wb', options, &block)
|
8
|
-
else
|
9
|
-
begin
|
10
|
-
csv = ::CSV.new(file_name_or_io, options)
|
11
|
-
block.call(csv)
|
12
|
-
ensure
|
13
|
-
csv.close if csv
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
module IOStreams
|
2
|
-
module Delimited
|
3
|
-
class Writer
|
4
|
-
attr_accessor :delimiter
|
5
|
-
|
6
|
-
# Write delimited records/lines to a file or stream
|
7
|
-
def self.open(file_name_or_io, delimiter: $/, encoding: UTF8_ENCODING, strip_non_printable: false)
|
8
|
-
if IOStreams.writer_stream?(file_name_or_io)
|
9
|
-
yield new(file_name_or_io, delimiter: delimiter, encoding: encoding, strip_non_printable: strip_non_printable)
|
10
|
-
else
|
11
|
-
::File.open(file_name_or_io, 'wb') do |io|
|
12
|
-
yield new(io, delimiter: delimiter, encoding: encoding, strip_non_printable: strip_non_printable)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
NOT_PRINTABLE = Regexp.compile(/[^[:print:]]/)
|
18
|
-
|
19
|
-
# A delimited stream writer that will write to the supplied output stream
|
20
|
-
#
|
21
|
-
# The output stream should be binary with no text conversions performed
|
22
|
-
# since `strip_non_printable` will be applied to the binary stream before
|
23
|
-
# converting to UTF-8
|
24
|
-
#
|
25
|
-
# Parameters
|
26
|
-
# output_stream
|
27
|
-
# The output stream that implements #write
|
28
|
-
#
|
29
|
-
# delimiter: [String]
|
30
|
-
# Add the specified delimiter after every record when writing it
|
31
|
-
# to the output stream
|
32
|
-
# Default: OS Specific. Linux: "\n"
|
33
|
-
#
|
34
|
-
# encoding:
|
35
|
-
# Force encoding to this encoding for all data being read
|
36
|
-
# Default: UTF8_ENCODING
|
37
|
-
# Set to nil to disable encoding
|
38
|
-
#
|
39
|
-
# strip_non_printable: [true|false]
|
40
|
-
# Strip all non-printable characters read from the file
|
41
|
-
# Default: false
|
42
|
-
def initialize(output_stream, delimiter: $/, encoding: UTF8_ENCODING, strip_non_printable: false)
|
43
|
-
@output_stream = output_stream
|
44
|
-
@delimiter = delimiter.dup
|
45
|
-
@encoding = encoding
|
46
|
-
@strip_non_printable = strip_non_printable
|
47
|
-
@delimiter.force_encoding(UTF8_ENCODING) if @delimiter
|
48
|
-
end
|
49
|
-
|
50
|
-
# Write a record or line to the output stream
|
51
|
-
def <<(record)
|
52
|
-
chunk = record.to_s
|
53
|
-
# Strip out non-printable characters before converting to UTF-8
|
54
|
-
chunk = chunk.gsub(NOT_PRINTABLE, '') if @strip_non_printable
|
55
|
-
@output_stream.write((@encoding ? chunk.force_encoding(@encoding) : chunk))
|
56
|
-
@output_stream.write(@delimiter)
|
57
|
-
end
|
58
|
-
|
59
|
-
# Write the given string to the underlying stream
|
60
|
-
# Note: Use of this method not recommended
|
61
|
-
def write(string)
|
62
|
-
@output_stream.write(string)
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|