tolq-parsers-csv 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/csv/parser.rb +18 -9
  3. data/lib/csv/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9710a9952e758a16ef163907a4dc149a99de1027
4
- data.tar.gz: e52fb0b7058978b6558768cf2797a4eb76867ac6
3
+ metadata.gz: 94ccec23f2b9ed6aca02230487b8f7066fb43412
4
+ data.tar.gz: 4a26a86c228b7eba43c1c915b80c7142bb1d4103
5
5
  SHA512:
6
- metadata.gz: f8dbb132f786549eeafee7fcbf3577de810631252849d401e5621492bd664689c638f8a333dda0800954702eab613adaa5c1357b55c7289c0bfe93ede7a239c4
7
- data.tar.gz: 2e67d3d8065f4a913299932006ddcce301fd050f05ef1265495a25a1f37553c76ce1db9ceb5dd82ec1eb7f7ebe44379108446ca974ad7e95bc84b8cfcb1a9945
6
+ metadata.gz: 919650c2a4c132df4c99da194ff8414dd07587c8bb197eb9347be49588b2620937102c623cd702996250482c7186bcb5f8ee8f9d97204c5f5be3a18ca143582f
7
+ data.tar.gz: 47e1cb3b4642cb89e82e8033088d7fd712299fe0a2cb161a87fa90b89d50257b4ece2c55850bb5a95a46049b83302df998b11b809d4e0297eb5791051c1a0908
data/lib/csv/parser.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'csv'
2
2
 
3
3
  module Tolq::Parsers::CSV
4
+ class ParseError < StandardError; end
4
5
  class Parser
5
6
 
6
7
  # Helper method to convert a CSV into a annotated hash. Note that format
@@ -11,11 +12,15 @@ module Tolq::Parsers::CSV
11
12
  # @return [Hash] Hash representation of csv, i.e. { 'A1' => 'Hello World'
12
13
  def self.to_hash(csv_io, **parse_opts)
13
14
  rdata = {}
14
- CSV.new(csv_io, parse_opts).each.with_index do |row, rowidx|
15
- row.each.with_index do |col, colidx|
16
- charcol = ColumnHelper.column_to_char(colidx)
17
- rdata["#{charcol}#{rowidx + 1}"] = col if col && col.length > 0
15
+ begin
16
+ CSV.new(csv_io, parse_opts).each.with_index do |row, rowidx|
17
+ row.each.with_index do |col, colidx|
18
+ charcol = ColumnHelper.column_to_char(colidx)
19
+ rdata["#{charcol}#{rowidx + 1}"] = col if col && col.length > 0
20
+ end
18
21
  end
22
+ rescue CSV::MalformedCSVError => e
23
+ raise ParseError.new(e.message)
19
24
  end
20
25
 
21
26
  rdata
@@ -56,12 +61,16 @@ module Tolq::Parsers::CSV
56
61
 
57
62
  def extract_request_data_from_csv(csv_text, parse_opts)
58
63
  rdata = {}
59
- CSV.new(csv_text, parse_opts).each.with_index do |row, rowidx|
60
- row.each.with_index do |col, colidx|
61
- next if excluded?(colidx, rowidx)
62
- charcol = ColumnHelper.column_to_char(colidx)
63
- rdata["#{charcol}#{rowidx + 1}"] = { "text" => col } if col && col.length > 0
64
+ begin
65
+ CSV.new(csv_text, parse_opts).each.with_index do |row, rowidx|
66
+ row.each.with_index do |col, colidx|
67
+ next if excluded?(colidx, rowidx)
68
+ charcol = ColumnHelper.column_to_char(colidx)
69
+ rdata["#{charcol}#{rowidx + 1}"] = { "text" => col } if col && col.length > 0
70
+ end
64
71
  end
72
+ rescue CSV::MalformedCSVError => e
73
+ raise ParseError.new(e.message)
65
74
  end
66
75
 
67
76
  rdata
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.2.1"
4
+ VERSION = "0.2.2"
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.2.1
4
+ version: 0.2.2
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-02-07 00:00:00.000000000 Z
11
+ date: 2017-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler