tolq-parsers-csv 0.1.1 → 0.2.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/README.md +1 -1
- data/lib/csv/column_helper.rb +32 -0
- data/lib/csv/parser.rb +23 -2
- data/lib/csv/unparser.rb +1 -1
- data/lib/csv/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1812d6b67495f8df13b3655c8578d65bf4fd78c6
|
4
|
+
data.tar.gz: bf94b7f9246532fe12d4b85a51b2a652cb44418c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 289c3ba9652b4397abc6eee17e66dc2a795caf7ab076eeb44ebf4408afb58268536e37812382d38c1226dbd3724fcf7948b0ac98424cb875137938a16fde5727
|
7
|
+
data.tar.gz: f1a0b5f9f7d06275f4c4c6672d2322e2265679f67e748a3938f5abe3d7cddab48db3734b5da8c27f7869085610ce6a436200d3cb508a0a5d8606d7bf8f75b036
|
data/README.md
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Tolq::Parsers::CSV
|
2
|
+
module ColumnHelper
|
3
|
+
# Converts column to char, zero indexed
|
4
|
+
def self.column_to_char(idx)
|
5
|
+
dividend = idx + 1
|
6
|
+
column_name = ""
|
7
|
+
|
8
|
+
while dividend > 0
|
9
|
+
modulo = (dividend - 1) % 26
|
10
|
+
column_name = (65 + modulo).chr + column_name
|
11
|
+
dividend = (dividend - modulo) / 26
|
12
|
+
end
|
13
|
+
|
14
|
+
column_name
|
15
|
+
end
|
16
|
+
|
17
|
+
# Converts char to column, zero indexed
|
18
|
+
def self.char_to_column(char)
|
19
|
+
char
|
20
|
+
.split('')
|
21
|
+
.reverse
|
22
|
+
.map.with_index { |c,idx| (c.ord - 64) * (26**idx)}
|
23
|
+
.inject(&:+) - 1
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.from_char_notation(key)
|
27
|
+
char, row = key.scan(/(\w+)(\d+)/).first
|
28
|
+
return row.to_i - 1, ColumnHelper.char_to_column(char)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
data/lib/csv/parser.rb
CHANGED
@@ -19,9 +19,12 @@ module Tolq::Parsers::CSV
|
|
19
19
|
#
|
20
20
|
# @param csv_io [IO] A string or IO object to parse
|
21
21
|
# @param parse_opts [Hash] Options for the CSV parser (i.e. col_sep, quote_char)
|
22
|
+
# @param exclude [Array] List of ranges to exclude in excel notation, i.e. ["A1:A3"]
|
22
23
|
# @return [Hash] A hash suitable to be converted to json for a Tolq api request
|
23
|
-
def parse(csv_io, **parse_opts)
|
24
|
+
def parse(csv_io, exclude: [], **parse_opts)
|
25
|
+
@exclude = to_numeric_exclude_ranges(exclude)
|
24
26
|
request_data = extract_request_data_from_csv(csv_io, parse_opts)
|
27
|
+
return nil if request_data.empty?
|
25
28
|
|
26
29
|
{
|
27
30
|
"request" => request_data,
|
@@ -37,11 +40,29 @@ module Tolq::Parsers::CSV
|
|
37
40
|
rdata = {}
|
38
41
|
CSV.new(csv_text, parse_opts).each.with_index do |row, rowidx|
|
39
42
|
row.each.with_index do |col, colidx|
|
40
|
-
|
43
|
+
next if excluded?(colidx, rowidx)
|
44
|
+
charcol = ColumnHelper.column_to_char(colidx)
|
45
|
+
rdata["#{charcol}#{rowidx + 1}"] = { "text" => col } if col && col.length > 0
|
41
46
|
end
|
42
47
|
end
|
43
48
|
|
44
49
|
rdata
|
45
50
|
end
|
51
|
+
|
52
|
+
def excluded?(colidx, rowidx)
|
53
|
+
!!@exclude.find do |range|
|
54
|
+
from, to = range
|
55
|
+
colidx >= from.last && colidx <= to.last &&
|
56
|
+
rowidx >= from.first && rowidx <= to.first
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_numeric_exclude_ranges(exclude_arr)
|
61
|
+
exclude_arr.map do |exrange|
|
62
|
+
exrange
|
63
|
+
.split(":")
|
64
|
+
.map { |k| ColumnHelper.from_char_notation(k) }
|
65
|
+
end
|
66
|
+
end
|
46
67
|
end
|
47
68
|
end
|
data/lib/csv/unparser.rb
CHANGED
@@ -21,7 +21,7 @@ module Tolq::Parsers::CSV
|
|
21
21
|
def build_translation_matrix(translations_hash)
|
22
22
|
@maxcol = 0
|
23
23
|
translations_hash.values.first.inject([]) do |matrix, (k,translation)|
|
24
|
-
row,col =
|
24
|
+
row,col = ColumnHelper.from_char_notation(k)
|
25
25
|
matrix[row] ||= []
|
26
26
|
matrix[row][col] = translation
|
27
27
|
@maxcol = col if col > @maxcol
|
data/lib/csv/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tolq-parsers-csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Timon Vonk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- LICENSE.txt
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
|
+
- lib/csv/column_helper.rb
|
97
98
|
- lib/csv/parser.rb
|
98
99
|
- lib/csv/unparser.rb
|
99
100
|
- lib/csv/version.rb
|