tolq-parsers-csv 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 398f33db78392da4efe8d163a03fafd6d1f00acb
4
- data.tar.gz: 20d7c6a9fe73f1e763f69f602976fb28cacf01b2
3
+ metadata.gz: 1812d6b67495f8df13b3655c8578d65bf4fd78c6
4
+ data.tar.gz: bf94b7f9246532fe12d4b85a51b2a652cb44418c
5
5
  SHA512:
6
- metadata.gz: 20689cfe169db3d5df8c3f6aa14c836baa5555c5ef502f5424b9417a814ddfab63c12348d60bbb370eafffa5cadc34e284ffd5fe3d66b7be5c8a609ca5d6d749
7
- data.tar.gz: 805009613d66eaea3569656d6f29c13fd789d55c4ebe5c0bba3696307744c47e115c1ce0b20581235401e7eb3b736c1d7e09ad9bdb87263aa631e1328b04a981
6
+ metadata.gz: 289c3ba9652b4397abc6eee17e66dc2a795caf7ab076eeb44ebf4408afb58268536e37812382d38c1226dbd3724fcf7948b0ac98424cb875137938a16fde5727
7
+ data.tar.gz: f1a0b5f9f7d06275f4c4c6672d2322e2265679f67e748a3938f5abe3d7cddab48db3734b5da8c27f7869085610ce6a436200d3cb508a0a5d8606d7bf8f75b036
data/README.md CHANGED
@@ -34,7 +34,7 @@ parser.parse(csv)
34
34
  And to unparse from the tolq format back to csv:
35
35
 
36
36
  ```ruby
37
- unparser = Tolq::Parsers::csv::Unparser.new
37
+ unparser = Tolq::Parsers::CSV::Unparser.new
38
38
  unparser.unparse(tolq_api_response)
39
39
  ```
40
40
  ## Development
@@ -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
- rdata["#{rowidx}:#{colidx}"] = { "text" => col } if col && col.length > 0
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 = k.split(":").map(&:to_i)
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
@@ -1,7 +1,7 @@
1
1
  module Tolq
2
2
  module Parsers
3
3
  module CSV
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  end
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.1.1
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-01-18 00:00:00.000000000 Z
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